poj 3228 Gold Transportation 最小生成树+带权并查集 解题报告
2017-10-12 20:33
351 查看
Description
Recently, a number of gold mines have been discovered in Zorroming State. To protect this treasure, we must transport this gold to the storehouses as quickly as possible. Suppose that the Zorroming State consists of N towns and there are M bidirectional roads among these towns. The gold mines are only discovered in parts of the towns, while the storehouses are also owned by parts of the towns. The storage of the gold mine and storehouse for each town is finite. The truck drivers in the Zorroming State are famous for their bad temper that they would not like to drive all the time and they need a bar and an inn available in the trip for a good rest. Therefore, your task is to minimize the maximum adjacent distance among all the possible transport routes on the condition that all the gold is safely transported to the storehouses.Input
The input contains several test cases. For each case, the first line is integer N(1<=N<=200). The second line is N integers associated with the storage of the gold mine in every towns .The third line is also N integers associated with the storage of the storehouses in every towns .Next is integer M(0<=M<=(n-1)*n/2).Then M lines follow. Each line is three integers x y and d(1<=x,y<=N,0Output
For each case, output the minimum of the maximum adjacent distance on the condition that all the gold has been transported to the storehouses or “No Solution”.Sample Input
43 2 0 0
0 0 3 3
6
1 2 4
1 3 10
1 4 12
2 3 6
2 4 8
3 4 5
0
Sample Output
6思路
配图很清真。。。最小生成树可以保证最小但是求不出具体得数值。
在每一次合并的时候带权并查集,并且判断金矿总量和仓库总量的关系。
代码
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<cmath> #include<vector> using namespace std; const int N=200,M=20000; struct edge { int u,v,w; }ed[M+5]; int n,m,w[N+5],s[N+5],father[N+5],answ,anss,ans; bool cmp(edge a,edge b){return a.w<b.w;} int find(int a){return father[a]==a?a:father[a]=find(father[a]);} bool check() { for (int i=1;i<=n;i++) { int u=find(i); if (s[u]<w[u]) return false; } return true; } void kruskal() { for (int i=1;i<=m;i++) { int u=ed[i].u,v=ed[i].v; u=find(u),v=find(v); if (u==v) continue; father[u]=v; w[v]+=w[u],w[u]=0; s[v]+=s[u],s[u]=0; ans=ed[i].w; if (check()) return ; } } int main() { while(~scanf("%d",&n)) { answ=0,anss=0; if (!n) break; for (int i=1;i<=n;i++) father[i]=i; for (int i=1;i<=n;i++) scanf("%d",&w[i]),answ+=w[i]; for (int i=1;i<=n;i++) scanf("%d",&s[i]),anss+=s[i]; if (answ>anss) {printf("No Solution\n");continue;} scanf("%d",&m); for (int i=1;i<=m;i++) scanf("%d%d%d",&ed[i].u,&ed[i].v,&ed[i].w); if (check()) {printf("0\n");continue;} sort(ed+1,ed+1+m,cmp); kruskal(); if (check()) printf("%d\n",ans); else printf("No Solution\n"); } return 0; }
相关文章推荐
- 最小生成树 prim POJ 2349解题报告
- poj 1679 The Unique MST 判断最小生成树是否唯一 解题报告
- 最小生成树,POJ和HDU几道题目的解题报告(基于自己的模板)
- 最小生成树 POJ 3625Building Roads解题报告
- 【解题报告】 HDU 1102 Constructing Roads -- 并查集 最小生成树 Prime算法
- 最小生成树,POJ和HDU几道题目的解题报告(基于自己写的模板)
- POJ3228 Gold Transportation 解题报告【贪心+并查集=Kruskal?】
- 【原】 POJ 1611 The Suspects 并查集 解题报告
- Poj 食物链 解题报告 (种类并查集)
- poj-3723【图的最小生成树+并查集】
- poj 1325Machine Schedule解题报告-最小顶点覆盖等于最大匹配数
- openoj的一个小比赛(J题解题报告)poj1703(并查集)
- POJ - 1251 Jungle Roads (最小生成树Kruskal、并查集)
- 洛谷 1195 口袋的天空 最小生成树 解题报告
- 解题报告之——hdu1233还是畅通工程(最小生成树)
- POJ3522 Slim Span 解题报告【Kruskal求最小生成树+枚举】
- poj 1251 丛林中的路 最小生成树问题 克鲁斯卡方法 并查集解决
- 【解题报告】 POJ 1611 The Suspects 并查集基础 (一点自己的感悟)
- poj 1733 Parity game 并查集 解题报告
- Codeforces 827 D 最小生成树+倍增 解题报告