日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【POJ - 3723】Conscription (最大生成树,最小生成树MST变形)

發布時間:2023/12/10 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【POJ - 3723】Conscription (最大生成树,最小生成树MST变形) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

Windy has a country, and he wants to build an army to protect his country. He has picked up?N?girls and?M?boys and wants to collect them to be his soldiers. To collect a soldier without any privilege, he must pay 10000 RMB. There are some relationships between girls and boys and Windy can use these relationships to reduce his cost. If girl?x?and boy?y?have a relationship?d?and one of them has been collected, Windy can collect the other one with 10000-d?RMB. Now given all the relationships between girls and boys, your assignment is to find the least amount of money Windy has to pay. Notice that only one relationship can be used when collecting one soldier.

Input

The first line of input is the number of test case.
The first line of each test case contains three integers,?N,?M?and?R.
Then?R?lines followed, each contains three integers?xi,?yi?and?di.
There is a blank line before each test case.

1 ≤?N,?M?≤ 10000
0 ≤?R?≤ 50,000
0 ≤?xi?<?N
0 ≤?yi?<?M
0 <?di?< 10000

Output

For each test case output the answer in a single line.

Sample Input

25 5 8 4 3 6831 1 3 4583 0 0 6592 0 1 3063 3 3 4975 1 3 2049 4 2 2104 2 2 7815 5 10 2 4 9820 3 2 6236 3 1 8864 2 4 8326 2 0 5156 2 0 1463 4 1 2439 0 4 4373 3 4 8889 2 4 3133

Sample Output

71071 54223

題目大意:

Windy有一個國家,他想建立一支軍隊來保護他的國家。他收留了N個女孩和M個男孩,想把她們收留成他的士兵。征兵無特權,必須交納一萬元。女孩和男孩之間有一些關系,溫迪可以利用這些關系來降低他的成本。如果X女孩和Y男孩有D關系,并且其中一個已經被收集,Windy可以用10000-D人民幣收集另一個。現在考慮到男孩和女孩之間的所有關系,你的任務是找到風必須支付的最少的錢。注意,收集一個士兵時只能使用一個關系。

解題報告:

? 首先問題轉化成:為了招到每個人 都先給每個人10000元,但是因為一些選擇的關系,使得可以再還回來一些錢。

? 建圖,這題把R個關系轉換成R條無向邊,每個士兵只能用一個關系說明圖中選擇某些邊,但是邊構成的圖不能有環(不難想到是棵樹,至于為什么是一棵樹而不能形成回路 比如a--b--c--d四人 a參軍影響b b參軍影響c?c參軍影響d?此時a已參軍 d無法影響a 構不成回路)。然后要還回來的錢最多,所以要選擇邊權最大的邊來構成這棵樹。于是轉化成最大生成樹問題,不過這里要注意不一定是選擇的這棵樹包含所有的頂點,因為只要是樹就行唄,不一定非連通所有的頂點啊,沒有連通的頂點我就付給他10000元就是了。所以最后用預付的每個人10000 減去? 最大生成樹的權值? 就是我們支付的最少的錢了。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair using namespace std; const int MAX = 2e5 + 5; int f[MAX]; int n,m,all,R; struct Edge {int u,v;ll w; } e[MAX]; bool cmp(Edge a,Edge b) {return a.w > b.w; } int getf(int v) {return v == f[v] ? v : f[v] = getf(f[v]); } void merge(int u,int v) {int t1 = getf(u);int t2 = getf(v);f[t2] = t1; } int main() {int t;cin>>t;while(t--) {cin>>n>>m;//n女孩 m男孩 all = n+m;for(int i = 1; i<=all; i++) f[i] = i;cin>>R;//R個關系 for(int x,y,i = 1; i<=R; i++) {ll d;scanf("%d%d%lld",&x,&y,&d);x++,y++;y+=n;e[i].u = x;e[i].v = y;e[i].w = d;}ll ans = 0;sort(e+1,e+R+1,cmp);for(int u,v,i = 1; i<=R; i++) {u = e[i].u,v = e[i].v;if(getf(u) != getf(v)) {merge(u,v);ans += e[i].w;}}printf("%lld\n",all*10000LL - ans); }return 0 ; } /* 25 5 8 4 3 6831 1 3 4583 0 0 6592 0 1 3063 3 3 4975 1 3 2049 4 2 2104 2 2 781*/

注意轉化關系,每個人的關系只能使用一次,類似的問題,可以轉化成每個頂點的入度?出度?這題中是轉化成不能有環的問題,其實題目中表達的不是很清晰。

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的【POJ - 3723】Conscription (最大生成树,最小生成树MST变形)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。