PAT (Top Level) Practise 1003 Universal Travel Sites (35)
来源:互联网 发布:jesse mccartney知乎 编辑:程序博客网 时间:2024/06/06 07:20
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:
sourcei destinationi capacityi
where sourcei and destinationi are the names of the satellites and the two involved planets, and capacityi > 0 is the maximum number of passengers that can be transported at one pass from sourcei to destinationi. 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 11EAR AAA 300EAR BBB 400AAA BBB 100AAA CCC 400AAA MAR 300BBB DDD 400AAA DDD 400DDD AAA 100CCC MAR 400DDD CCC 200DDD MAR 300Sample Output:
700
这个题意有点难懂,不过其实就是个最裸的网络最大流,效率低的ek算法也能过,这里我用的是dinic
#include<cstdio>#include<cstring>#include<cmath>#include<queue>#include<vector>#include<map>#include<iostream>#include<string>#include<stack>#include<algorithm>#include<bitset>#include<functional>using namespace std;typedef unsigned long long ull;typedef long long LL;const int maxn = 1e5 + 10;const int INF = 0x7FFFFFFF;map<string, int> M;char x[maxn], y[maxn];int n, z, tot;struct MaxFlow{ const static int maxe = 1e6 + 10; //边数 const static int maxp = 1e5 + 10; //点数 const static int INF = 0x7FFFFFFF; struct Edges { int x, f; Edges(){} Edges(int x, int f) :x(x), f(f){} }edge[maxe]; int first[maxp], next[maxe], dis[maxp], tot, work[maxp], n; void clear(int x){ n = x; tot = 0; for (int i = 0; i <= n; i++) first[i] = -1; } void AddEdge(int s, int t, int f) { edge[tot] = Edges(t, 0); next[tot] = first[s]; first[s] = tot++; edge[tot] = Edges(s, f); next[tot] = first[t]; first[t] = tot++; } bool bfs(int s, int t) { for (int i = 0; i <= n; i++) dis[i] = -1; queue<int> p; p.push(s); dis[s] = 0; while (!p.empty()) { int q = p.front(); p.pop(); for (int i = first[q]; i != -1; i = next[i]) { if (edge[i ^ 1].f&&dis[edge[i].x] == -1) { p.push(edge[i].x); dis[edge[i].x] = dis[q] + 1; if (dis[t] != -1) return true; } } } return false; } int dfs(int s, int t, int low) { if (s == t) return low; for (int &i = work[s], x; i >= 0; i = next[i]) { if (dis[s] + 1 == dis[edge[i].x] && edge[i ^ 1].f && (x = dfs(edge[i].x, t, min(low, edge[i ^ 1].f)))) { edge[i].f += x; edge[i ^ 1].f -= x; return x; } } return 0; } int dinic(int s, int t) { int maxflow = 0, inc = 0; while (bfs(s, t)) { for (int i = 0; i <= n; i++) work[i] = first[i]; while (inc = dfs(s, t, INF)) maxflow += inc; } return maxflow; }}solve;int main(){ while (scanf("%s%s%d", x, y, &n) != EOF) { solve.clear(2 * n + 2); M[x] = 1; M[y] = 2; tot = 3; while (n--) { scanf("%s%s%d", x, y, &z); if (!M[x]) M[x] = tot++; if (!M[y]) M[y] = tot++; solve.AddEdge(M[x], M[y], z); } printf("%d\n", solve.dinic(1, 2)); } return 0;}
- PAT (Top Level) Practise 1003 Universal Travel Sites (35)
- pat-top 1003. Universal Travel Sites (35)
- PAT 1003. Universal Travel Sites (35)
- 1003. Universal Travel Sites (35)
- PAT (Top Level) Practise 1002Business (35)
- PAT (Top Level) Practise 1018Subnumbers (35)
- 1003. Universal Travel Sites (35)解题报告
- PAT (Top Level) Practise 1005Programming Pattern (35)
- PAT (Top Level) Practise 1006 Tree Traversals - Hard Version (35)
- PAT (Top Level) Practise 1007 Red-black Tree (35)
- PAT (Top Level) Practise 1008Airline Routes (35)
- PAT (Top Level) Practise 1009 Triple Inversions (35)
- PAT (Top Level) Practise 1010Lehmer Code (35)
- PAT (Top Level) Practise 1011 Cut Rectangles (35)
- PAT (Top Level) Practise 1012Greedy Snake (35)
- PAT (Top Level) Practise 1013 Image Segmentation (35)
- PAT (Top Level) Practise 1014 Circles of Friends (35)
- PAT (Top Level) Practise 1015. Letter-moving Game (35)
- 机器学习系列(7)_机器学习路线图(附资料)
- 成为黑客需要学习什么技能?
- Linux进程的查看和调度分别使用什么命令?
- 关联式容器map/multimap之元素大小比较(键值比较和实值比较)
- ViewPager 详解(五)-----使用Fragment实现ViewPager滑动
- PAT (Top Level) Practise 1003 Universal Travel Sites (35)
- maven的目录结构
- lua之table数据结构分析(一)
- fork Function
- python将HTML转PDF
- PAT (Top Level) Practise 1004To Buy or Not to Buy - Hard Version (35)
- wordpress添加Auto Highslide图片灯箱效果
- 102.Lowest Common Ancestor of a Binary Tree
- Mysql表结构为InnoDB类型从ibd文件恢复数据