博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3228 Gold Transportation (二分+最大流) (Dinic + 二分 或 EK)
阅读量:6679 次
发布时间:2019-06-25

本文共 6231 字,大约阅读时间需要 20 分钟。

Gold Transportation
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 2407   Accepted: 858

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,0<d<=10000), means that there is a road between x and y for distance of d. N=0 means end of the input.

Output

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 00 0 3 361 2 41 3 101 4 122 3 62 4 83 4 50

Sample Output

6

Source

 
 
题意:有N座town 每座town都有一定数量gold和仓库 仓库的容量是有限的 有M条双向路径 求把所有的gold 运到仓库最小的最大距离是多少。
思路:跟前几天做的那道    的思想是一样的,建图时,超级源点与gold相连 容量为gold的数量,超级汇点与仓库相连,容量为仓库的容量,其余边为无穷。用二分枚举求出最小的最距离
 
#include
#include
#include
#include
using namespace std;const int VM=220;const int EM=50010;const int INF=0x3f3f3f3f;int n,m,src,des,map[VM][VM],dis[VM][VM];int total,gold[VM],store[VM],dep[VM];void buildgraph(int x){ memset(map,0,sizeof(map)); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(dis[i][j]<=x) map[i][j]=INF; for(int i=1;i<=n;i++){ map[src][i]=gold[i]; map[i][des]=store[i]; }}int BFS(){ queue
q; while(!q.empty()) q.pop(); memset(dep,-1,sizeof(dep)); dep[src]=0; q.push(src); while(!q.empty()){ int u=q.front(); q.pop(); for(int v=src;v<=des;v++) if(map[u][v]>0 && dep[v]==-1){ dep[v]=dep[u]+1; q.push(v); } } return dep[des]!=-1;}int DFS(int u,int minx){ if(u==des) return minx; int tmp; for(int v=src;v<=des;v++) if(map[u][v]>0 && dep[v]==dep[u]+1 && (tmp=DFS(v,min(minx,map[u][v])))){ map[u][v]-=tmp; map[v][u]+=tmp; return tmp; } dep[u]=-1; return 0;}int Dinic(){ int ans=0,tmp; while(BFS()){ while(1){ tmp=DFS(src,INF); if(tmp==0) break; ans+=tmp; } } return ans;}int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d",&n) && n){ memset(dis,0x3f,sizeof(dis)); total=0; src=0, des=n+1; for(int i=1;i<=n;i++){ scanf("%d",&gold[i]); total+=gold[i]; //宝藏的总数 } for(int i=1;i<=n;i++) scanf("%d",&store[i]); scanf("%d",&m); int u,v,w; while(m--){ scanf("%d%d%d",&u,&v,&w); dis[u][v]=dis[v][u]=w; } int l=0,r=100010; int ans=-1,tmp; while(l<=r){ int mid=(l+r)>>1; buildgraph(mid); tmp=Dinic(); if(tmp==total){ ans=mid; r=mid-1; }else l=mid+1; } if(ans==-1) printf("No Solution\n"); else printf("%d\n",ans); } return 0;}

 

 
#include
#include
#include
using namespace std;const int INF=0x3f3f3f3f;const int VM=250;const int EM=20010;int pre[VM],mat[VM][VM],dis[VM][VM];int gold[VM],stor[VM];int n,m,total,src,des;void build(int num){ //在当前距离下 给通过的路径 memset(mat,0,sizeof(mat)); int i,j; for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(dis[i][j]<=num) mat[i][j]=INF; for(i=1;i<=n;i++){ mat[src][i]=gold[i]; mat[i][des]=stor[i]; }}bool BFS(){ int q[VM+5]; memset(pre,0xff,sizeof(pre)); //-1 int front=0,rear=0; pre[src]=0; q[rear++]=src; while(front!=rear){ int u=q[front++]; front=front%VM; for(int v=1;v<=des;v++){ if(pre[v]!=-1 || mat[u][v]==0) continue; pre[v]=u; if(v==des) return 1; q[rear++]=v; rear=rear%VM; } } return 0;}int EK(){ int res=0; while(BFS()){ int tmp=INF; for(int i=des;i!=src;i=pre[i]) tmp=min(tmp,mat[pre[i]][i]); res+=tmp; for(int i=des;i!=src;i=pre[i]){ mat[pre[i]][i]-=tmp; mat[i][pre[i]]+=tmp; } } return res;}int main(){ //freopen("input.txt","r",stdin); int u,v,w; while(~scanf("%d",&n) && n){ memset(dis,0x3f,sizeof(dis)); total=0; //total是总的gold数 src=0,des=n+1; for(int i=1;i<=n;i++){ scanf("%d",&gold[i]); total+=gold[i]; } for(int i=1;i<=n;i++) scanf("%d",&stor[i]); scanf("%d",&m); while(m--){ scanf("%d%d%d",&u,&v,&w); dis[u][v]=w; dis[v][u]=w; } int l=0,r=10001; int ans=-1; while(l<=r){ int mid=(l+r)>>1; build(mid); int sum=EK(); if(sum>=total){ ans=mid; r=mid-1; }else l=mid+1; } if(ans==-1) printf("No Solution\n"); else printf("%d\n",ans); } return 0;}

 

转载地址:http://jmnao.baihongyu.com/

你可能感兴趣的文章
RHEL7 Connection closed by foreign host.
查看>>
Nodejs开发框架之Loopback介绍
查看>>
微信小程序下拉刷新使用整理
查看>>
ubuntu12.04禁用客人会话
查看>>
我的友情链接
查看>>
JVM垃圾收集器与内存分配策略
查看>>
分析Linux 文件系统访问控制列表
查看>>
Confluence WIKI 安装、破解及添加汉化包(Windows)
查看>>
一起入门Citrix_XenDesktop7系列 二-- 跟着图片通过XenDesktop7交付(发布)应用与共享桌面...
查看>>
MyBatis学习手记(一)MaBatis入门
查看>>
SCTF-2014 writeup(部分)
查看>>
Elasticsearch 连接查询
查看>>
Retrofit入门
查看>>
设置Exchange 通讯组接收外部组织邮件
查看>>
观点:正在消逝的运维江湖
查看>>
istio 监控,遥测 (理论)
查看>>
Oracle insert 多条记录
查看>>
Python学习-baseNo.2
查看>>
spring data mongo 复合索引
查看>>
修改Windows Server 2008远程桌面连接端口
查看>>