1. 程式人生 > >hdu 2480 貪心+簡單並查集

hdu 2480 貪心+簡單並查集

Steal the Treasure

Time Limit: 10000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 775    Accepted Submission(s): 213

Problem Description The alliance of thieves decides to steal the treasure from country A. There are n cities in country A. Cities are connected by directional or bidirectional road. To avoid the risk, the king of country A divides his treasure and hides them in some place on the road.   The alliance has found out the secret of the king. They get a map of country A which shows the location and the quantity of treasure on each road. In order to make the maximum profit and reduce the least loss, the alliance determines to send n thieves respectively to each city (one city one thief). At the appointed time, each thief chooses one road (if there is a road and notice that the road may have direction) to get to its corresponding city. Then he can steal the treasure on that road. After stealing, all the thieves return back to their base immediately.   The heads of the alliance wonder to know the quantity of the treasure they can steal at most.
Input There are multiple cases. Input is terminated by EOF.   For each case, the first line contains two integers n (1<=n<=1000) and m (0<=m<=n*(n-1)/2), representing the number of cities and the number of roads in country A. The following m lines, each line contains four integers x, y (1<=x, y<=n, x≠y), d (0<=d<=1), w (0<=w<=1000), which means that there is a road from city x to city y, d=0 shows this road is bidirectional and d=1 shows it is directional and x the starting point, w is the quantity of treasure on the road.   We guarantee that the road (x, y) and (y, x) will never appear together in the same case. Output For each case, output the maximum quantity of treasure the alliance can get. Sample Input 2 1 1 2 0 10 5 5 1 2 1 0 1 3 1 10 2 3 0 20 3 4 0 30 4 2 1 40 Sample Output 10 100  題目大意:有n個城市,這些城市由m條道路連通,每一條道路都有著一定的權值(財富),這些道路有的是可以雙向連通的,有的是單向的,只能從一個點出發,現在每一個城市都有著一個小偷,在特定時刻,這些小偷可以從這個城市前往任一條道路(如果可以),並拿走這條道路上的財富,現在問你最多能拿到多少財富。 思路分析:首先肯定是貪心,將邊按照邊權從大到小排序,到底能不能拿這個路上的財富是由他的端點城市決定的,對於有向邊,如果起點未被標記,那麼就拿走財富,標記起點,對於無向邊,如果有一個點沒被標記,就標記那個點,否則就用並查集將其縮為一點。 程式碼:
#include <iostream>
#include
<cstdio> #include <algorithm> #include <cstring> using namespace std; const int maxn=1000+10; struct node { int x,y; int d,w; }; node edge[maxn*maxn/2]; int fa[maxn]; int root(int x) { return (x==fa[x])?x:fa[x]=root(fa[x]); } bool cmp(node a,node b) { return
a.w>b.w; } bool vis[maxn]; int n,m; int main() { while(scanf("%d%d",&n,&m)!=EOF) { memset(vis,false,sizeof(vis)); for(int i=1;i<=n;i++) fa[i]=i; for(int i=0;i<m;i++) { scanf("%d%d%d%d",&edge[i].x,&edge[i].y,&edge[i].d,&edge[i].w); } sort(edge,edge+m,cmp); int ans=0; for(int i=0;i<m;i++) { int fx=root(edge[i].x); int fy=root(edge[i].y); if(vis[fx]&&vis[fy]) continue; if(edge[i].d==1&&vis[fx]) continue; ans+=edge[i].w; if(edge[i].d==1) vis[fx]=true; else { if(fx==fy) vis[fx]=true; else if(vis[fx]) vis[fy]=true; else if(vis[fy]) vis[fx]=true; else fa[fx]=fy;//縮點 } } printf("%d\n",ans); } }

相關推薦

hdu 2480 貪心+簡單

Steal the Treasure Time Limit: 10000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 775    Accepted Submission(s)

HDU 1232 暢通工程 簡單應用

暢通工程 某省調查城鎮交通狀況,得到現有城鎮道路統計表,表中列出了每條道路直接連通的城鎮。省政府“暢通工程”的目標是使全省任何兩個城鎮間都可以實現交通(但不一定有直接的道路相連,只要互相間接通過道路可達即可)。問最少還需要建設多少條道路?  Input測試輸入

hdu 1213 How Many Tables(簡單

題意就是 給出n對關係,假如A和B認識,B和C認識,那麼A和B認識。現在互相認識的人可以在一桌吃飯,不互相認識的人可以一桌吃飯,問需要多少張桌子 #pragma GCC optimize(2) #include<stdio.h> #include<a

HDU 小希的迷宮 (簡單

如圖所示,給定n個點,判斷圖中是否有環。 Input 輸入包含多組資料,每組資料是一個以0 0結尾的整數對列表,表示了一條通道連線的兩個房間的編號。房間的編號至少為1,且不超過100000。每兩組資料之間有一個空行。 整個檔案以兩個-1結尾。

HDU 3635 Dragon Balls()

quest text hdu ring form sse limit ould expect Dragon Balls Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Oth

D - How Many Answers Are Wrong HDU - 3038 帶權

following follow tar ndt wrong get gin k60 man w2csi眾美8uk唾嚎http://www.facebolw.com/space/2103369/following Y47炮K兌B巧約鐐39http://www.facebo

簡單歸納

數據結構 pac 簡單的 .cn 並集 包含 集合 nbsp ace 並查集就是一種一邊查找一邊並集的數據結構,簡單的並查集經常應用於朋友圈等題目,即:x和y是朋友,y和z是朋友,則x和z是朋友,下面給出一組數據表示xx和yy是朋友,最後問一共有多少個朋友圈。這類問題一般用

HDU 3018 Ant Trip (求連通塊數+歐拉回路)

http 道路 遇到 連通塊 ems ble define ant trip 註意 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 題目大意:有n個點,m條邊,人們希望走完所有的路,且每條道路只能走一遍。至少要將人們

HDU - 3635 Dragon Balls

題解 log col ++ 暫時 include turn 數據 集合 題意:1~N個龍珠,放在1~N個城市,有兩種操作:T A B 將A所再城市的所有球轉移到B所在的城市。 Q X 詢問X所在的城市pls,該城市中龍珠數量nm,以及龍珠轉移次數trs 題解:並查集模板題,

hdu-1232 暢通工程---

sca turn 鏈接 -s span i++ 就是 %d 之間 題目鏈接: http://acm.hdu.edu.cn/showproblem.php?pid=1232 題目大意: 中文題 解題思路: 直接並查集,判斷有多少不同的根節點,答案就是根節點的數目-1,因為還需

HDU 1213(裸)(無變形)

line table #define The mar .cn tput KS ast 題目鏈接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS

HDU - 4496 City (逆向)

span 個數 return 離線處理 line lin mat 我們 type 題意:有N個點M條邊的無向圖,每次刪除一條邊直到刪完為止,求每一次刪邊操作之後,連通塊的個數。 M<=1e5,N<=1e4。如果每次刪邊之後暴力求連通塊肯定超時。換個思路,對一個N

hdu 1863 暢通工程 ( 、 kruskal)

temp 編號 set queue bits 統計表 script pri with 暢通工程Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S

HDU-5441 Travel 離線-

names total mathjax cit not com scrip std php Travel Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others

HDU 1232 -暢通工程(

題目 http://acm.hdu.edu.cn/showproblem.php?pid=1232 程式碼 #include <iostream> #include <algorithm> #include <cstring> using na

HDU-1558,Segment set,+線段相交模擬

Segment set Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5033 &

Pseudoforest 【HDU - 3367】【+(可以忽略的離散化)】

題目連結   翻譯的有點不準確,還是靠自我的理解吧,吶,具體是這樣的:第一行 N, M,  接下來有M行,N代表有幾個字母組成, M代表有多少個可操作區間。   然後講一下幾個測試樣例: 樣例一: 表示只有一個字母,這個字母可以是a.....z, 1

How Many Answers Are Wrong HDU - 3038 (帶權

How Many Answers Are Wrong HDU - 3038 TT and FF are ... friends. Uh... very very good friends -________-b  FF is a bad boy, he is always wooing

BZOJ3296:Learning Languages(簡單

3296: [USACO2011 Open] Learning Languages Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 436  Solved: 239[Subm

HDU 1232——暢通工程 入門題

題意 :給定你n個村莊以及m條路,每條路連線兩個村莊,求還需要最少多少條路使得任意兩個村莊之間互相可以到達(直接間接都可以)。 思路 : 這是一道並查集經典的入門題,可以看這些村莊有多少個連通分量,然後連通分量的個數- 1即為答案 如圖所示,這是第一個樣例