pat 1003 Universal Travel Sites (35 分)
Posted zjxxcn
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pat 1003 Universal Travel Sites (35 分)相关的知识,希望对你有一定的参考价值。
After finishing her tour around the Earth, CYLL is now planning a universal travel sites development project. After a careful investigation, she has a list of capacities of all the satellite transportation stations in hand. To estimate a budget, she must know the minimum capacity that a planet station must have to guarantee that every space vessel can dock and download its passengers on arrival.
Input Specification:
Each input file contains one test case. For each case, the first line contains the names of the source and the destination planets, and a positive integer N
(≤500). Then N
lines follow, each in the format: source[i] destination[i] capacity[i]
where source[i]
and destination[i]
are the names of the satellites and the two involved planets, and capacity[i]
> 0 is the maximum number of passengers that can be transported at one pass from source[i]
to destination[i]
. Each name is a string of 3 uppercase characters chosen from A-Z, e.g., ZJU.
Note that the satellite transportation stations have no accommodation facilities for the passengers. Therefore none of the passengers can stay. Such a station will not allow arrivals of space vessels that contain more than its own capacity. It is guaranteed that the list contains neither the routes to the source planet nor that from the destination planet.
Output Specification:
For each test case, just print in one line the minimum capacity that a planet station must have to guarantee that every space vessel can dock and download its passengers on arrival.
Sample Input:
EAR MAR 11
EAR AAA 300
EAR BBB 400
AAA BBB 100
AAA CCC 400
AAA MAR 300
BBB DDD 400
AAA DDD 400
DDD AAA 100
CCC MAR 400
DDD CCC 200
DDD MAR 300
Sample Output:
700
网络流裸题。
1 #include<bits/stdc++.h> 2 using namespace std; 3 int const N=1000+10; 4 int const oo=1e9; 5 struct edge 6 int to,nt,capa,fl; 7 e[N<<1]; 8 map<string,int> mat; 9 string a,b; 10 int n,m,s,t,q[N],dist[N],cnt,h[N],work[N]; 11 void add(int a,int b,int c) 12 e[cnt]=(edge)b,h[a],c,0;h[a]=cnt++; 13 e[cnt]=(edge)a,h[b],0,0;h[b]=cnt++; 14 15 int bfs() 16 int cl=1; 17 memset(dist,-1,sizeof(dist)); 18 q[0]=s; dist[s]=0; 19 for(int i=0;i<cl;i++) 20 int x=q[i]; 21 for(int j=h[x];j!=-1;j=e[j].nt) 22 int v=e[j].to; 23 if(e[j].capa>e[j].fl && dist[v]==-1) 24 dist[v]=dist[x]+1,q[cl++]=v; 25 26 27 return dist[t]>=0; 28 29 int dfs(int x,int flow) 30 if(x==t) return flow; 31 for(int &i=work[x];i!=-1;i=e[i].nt) 32 int v=e[i].to,tmp; 33 if(e[i].capa>e[i].fl && dist[v]==dist[x]+1 && (tmp=dfs(v,min(flow,e[i].capa-e[i].fl)))) 34 e[i].fl+=tmp; 35 e[i^1].fl-=tmp; 36 return tmp; 37 38 39 return 0; 40 41 42 int main() 43 cin>>a>>b>>n; 44 mat[a]=1; 45 mat[b]=2; 46 s=1; 47 t=m=2; 48 memset(h,-1,sizeof(h)); 49 while (n--) 50 int x; 51 cin>>a>>b>>x; 52 if(mat.find(a)==mat.end()) mat[a]=++m; 53 if(mat.find(b)==mat.end()) mat[b]=++m; 54 add(mat[a],mat[b],x); 55 56 int ans=0,tmp; 57 while (bfs()) 58 memcpy(work,h,sizeof(h)); 59 while ((tmp=dfs(s,oo))) ans+=tmp; 60 61 cout<<ans<<endl; 62 return 0; 63
以上是关于pat 1003 Universal Travel Sites (35 分)的主要内容,如果未能解决你的问题,请参考以下文章