poj 3228 Gold Transportation 并查集
来源:互联网 发布:手机挂机软件 编辑:程序博客网 时间:2024/05/04 03:50
题目链接:http://poj.org/problem?id=3228
题目大意:有N座城市,每座城市都有a的金子和容量为b的仓库,有M条路,其中u,v,w表示从u到v的花费为w,现在问你把所有金子放进仓库的最小的最长路的长度。
解题思路:网上好多人用二分+网络流,鄙人不才,只能用并查集,首先把所有路径从小到大排列,然后依次添加路径,并记录金子的总运输量,当总运输量等于需要的运输量时,输出此时的路径,如果所有路径遍历完后不能把金子全运完,输出No Solution。
代码如下:
#include <cstdio>#include<iostream>#include <cstring>#include <cstdlib>#include<cmath>#include<algorithm>using namespace std;typedef double type;#define INF 2000000000#define N 205struct edge //图的结构体{ int u,v,w;}e[25005];int m,n,sum[N],f[N],SUM,gold,store;void init(){ SUM=0,gold=0,store=0; for(int i=0;i<N;i++) f[i]=i; memset(sum,0,sizeof(sum));}int find(int x){ if(x!=f[x]) f[x]=find(f[x]); return f[x];}bool merge(edge a){ bool flog=false; int x=find(a.u); int y=find(a.v); if(x!=y) { f[x]=y; if(sum[x]*sum[y]<0) { if(sum[x]+sum[y]>0) gold-=min(sum[x],sum[y]); else gold+=max(sum[x],sum[y]); } sum[y]+=sum[x]; if(gold==SUM) flog=true; } return flog;}bool cmp(edge a,edge b){ return a.w<b.w;}int main(){ while(~scanf("%d",&n)&&n) { int t; init(); for(int i=1;i<=n;i++) { scanf("%d",&t); sum[i]+=t; } for(int i=1;i<=n;i++) { scanf("%d",&t); sum[i]-=t; } for(int i=1;i<=n;i++) if(sum[i]>0) SUM+=sum[i]; scanf("%d",&m); for(int i=1;i<=m;i++) scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w); sort(e+1,e+m+1,cmp); int i; for(i=1;i<=m;i++) { if(merge(e[i])) { printf("%d\n",e[i].w); break; } } if(i>m) printf("No Solution\n"); } return 0;}
0 0
- poj 3228 Gold Transportation 并查集
- POJ 3228 Gold Transportation 并查集
- poj 3228 Gold Transportation 并查集
- 【POJ】3228 Gold Transportation 二分+最大流or并查集
- poj 3228 Gold Transportation 最小生成树+带权并查集 解题报告
- poj3228 Gold Transportation 并查集
- poj 3228 Gold Transportation
- POJ-3228-Gold Transportation
- POJ 3228 Gold Transportation
- poj(3228)Gold Transportation
- poj 3228 Gold Transportation
- poj3228 Gold Transportation(并查集)值得回顾
- poj 3228 Gold Transportation 二分+最大流
- POJ 3228 -- Gold Transportation【二分 && 最大流】
- POJ3228 Gold Transportation 解题报告【贪心+并查集=Kruskal?】
- POJ 1797Heavy Transportation Kruskal并查集求解
- POJ 1797 Heavy Transportation(二分+并查集/kruskal)
- poj 3228 Gold Transportation(二…
- java对象的复制
- 【BUG解决】WebView cookies清理
- 使用文本文件(.txt)进行数据存取的技巧总结(相当的经典)
- ubuntu9.10 iBus 输入法
- cocostudio 九宫格拉伸
- poj 3228 Gold Transportation 并查集
- My vim Config
- linux 路由交换原理学习中需要关注的几个方面
- Eclipse如何解决启动慢?
- zoj 1337
- Android核心基础四
- cookie测试点
- Socket相关程序:从Windows移植到Linux
- 【模拟】poj1247 Magnificent Meatballs