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.#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) { returna.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即為答案 如圖所示,這是第一個樣例