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 }

原文地址:https://www.cnblogs.com/ZJXXCN/p/11504887.html

时间: 2024-10-07 03:20:42

pat 1003 Universal Travel Sites (35 分)的相关文章

PAT 1003 Emergency (25)(25 分)

1003 Emergency (25)(25 分) As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road betwe

1030 Travel Plan (30 分)

1030 Travel Plan (30 分) A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting ci

PTA 7-1 畅通工程之局部最小花费问题(35 分)

7-1 畅通工程之局部最小花费问题(35 分) 某地区经过对城镇交通状况的调查,得到现有城镇间快速道路的统计数据,并提出"畅通工程"的目标:使整个地区任何两个城镇间都可以实现快速交通(但不一定有直接的快速道路相连,只要互相间接通过快速路可达即可).现得到城镇道路统计表,表中列出了任意两城镇间修建快速路的费用,以及该道路是否已经修通的状态.现请你编写程序,计算出全地区畅通需要的最低成本. 输入格式: 输入的第一行给出村庄数目N (1≤N≤100):随后的N(N?1)/2行对应村庄间道路的

2018/3/19 模拟赛 35分

T1 不会计算几何弃疗了. T2 写了个bitset结果还不如不优化(手动滑稽),因为测样例开小了空间忘了开回去所以0分. 正解是FFT,不会FFT.. T3 暴力35分,正解倍增floyd,学长还讲过但是还是错了,又多了一个要学的知识点. 原文地址:https://www.cnblogs.com/137shoebills/p/8602386.html

7-1 畅通工程之局部最小花费问题 (35 分)

7-1 畅通工程之局部最小花费问题 (35 分) 某地区经过对城镇交通状况的调查,得到现有城镇间快速道路的统计数据,并提出"畅通工程"的目标:使整个地区任何两个城镇间都可以实现快速交通(但不一定有直接的快速道路相连,只要互相间接通过快速路可达即可).现得到城镇道路统计表,表中列出了任意两城镇间修建快速路的费用,以及该道路是否已经修通的状态.现请你编写程序,计算出全地区畅通需要的最低成本. 输入格式: 输入的第一行给出村庄数目N (1≤N≤100):随后的N(N?1)/2行对应村庄间道路

pat 顶级 1001 Battle Over Cities - Hard Version (35 分)

It is vitally important to have all the cities connected by highways in a war. If a city is conquered by the enemy, all the highways from/toward that city will be closed. To keep the rest of the cities connected, we must repair some highways with the

pat 1005 Programming Pattern (35 分)

Programmers often have a preference among program constructs. For example, some may prefer if(0==a), while others may prefer if(!a). Analyzing such patterns can help to narrow down a programmer's identity, which is useful for detecting plagiarism. No

pat考试1002 Business (35 分)

As the manager of your company, you have to carefully consider, for each project, the time taken to finish it, the deadline, and the profit you can gain, in order to decide if your group should take this project. For example, given 3 projects as the

PAT 1003.我要通过!

1003. 我要通过!(20) "答案正确"是自动判题系统给出的最令人欢喜的回复.本题属于PAT的"答案正确"大派送 -- 只要读入的字符串满足下列条件,系统就输出"答案正确",否则输出"答案错误". 得到"答案正确"的条件是: 1. 字符串中必须仅有P, A, T这三种字符,不可以包含其它字符:2. 任意形如 xPATx 的字符串都可以获得"答案正确",其中 x 或者是空字符串,或者是