Universal Travel Sites

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 #define _CRT_SECURE_NO_WARNINGS
  2 #include <cstdio>
  3 #include <cstdlib>
  4 #include <cstring>
  5 #include <vector>
  6 #include <queue>
  7 #include <algorithm>
  8 #include <climits>
  9 using namespace std;
 10  
 11 struct edge {
 12     char source[4], dest[4];
 13     int capacity, s, d;
 14     int dindex(void) {
 15         int index = 0;
 16         index = ((dest[0] - 'A') * 26 + dest[1] - 'A') * 26 + dest[2] - 'A';
 17         return index;
 18     }
 19     int sindex(void) {
 20         int index = 0;
 21         index = ((source[0] - 'A') * 26 + source[1] - 'A') * 26 + source[2] - 'A';
 22         return index;
 23     }
 24     void read(void) {
 25         scanf("%s %s %d", source, dest, capacity);
 26         return;
 27     }
 28 };
 29  
 30 const int MAX = 26 * 26 * 26;
 31  
 32 bool BFS(vector<edge> arr[], int route[], int source, int dest);
 33 int Edmonds_Karp(vector<edge> arr[], int source, int dest);
 34  
 35 int main(void) {
 36     int i, j, n, sindex, dindex;
 37     edge tmp;
 38     vector<edge> *arr;
 39     setvbuf(stdin, new char[1 << 20], _IOFBF, 1 << 20);
 40     scanf("%s %s %d", tmp.source, tmp.dest, &n);
 41     sindex = tmp.sindex();
 42     dindex = tmp.dindex();
 43     arr = new vector<edge>[MAX];
 44     for (i = 0; i < n; i++) {
 45         scanf("%s %s %d", tmp.source, tmp.dest, &tmp.capacity);
 46         tmp.s = tmp.sindex();
 47         tmp.d = tmp.dindex();
 48         arr[tmp.s].push_back(tmp);
 49     }
 50     
 51     printf("%d", Edmonds_Karp(arr, sindex, dindex));
 52     delete[] arr;
 53     return 0;
 54 }
 55  
 56 bool BFS(vector<edge> arr[], int route[], int source, int dest) {
 57     bool *visit = (bool *)calloc(MAX, sizeof(bool));
 58     int i, j;
 59     queue<int> q;
 60     q.push(source);
 61     visit[source] = true;
 62     while (!q.empty()) {
 63         i = q.front();
 64         q.pop();
 65         if (i == dest) {
 66             break;
 67         }
 68         for (auto vit : arr[i]) {
 69             j = vit.d;
 70             if (!visit[j] && vit.capacity > 0) {
 71                 q.push(j);
 72                 route[j] = i;
 73                 visit[j] = true;
 74             }
 75         }
 76     }
 77     free(visit);
 78     return i == dest ? true : false;
 79 }
 80  
 81 int Edmonds_Karp(vector<edge> arr[], int source, int dest) {
 82     int c, *route, i, j, max, f;
 83     vector<edge>::iterator vit;
 84     route = new int[MAX];
 85     max = 0;
 86     for (i = 0; i < MAX; i++) {
 87         route[i] = -1;
 88     }
 89     while (BFS(arr, route, source, dest)) {
 90         f = INT_MAX;
 91         for (i = dest; i != source;) {
 92             j = route[i];
 93             for (vit = arr[j].begin(); vit != arr[j].end(); vit++) {
 94                 if (vit->d == i) {
 95                     f = min(f, vit->capacity);
 96                     break;
 97                 }
 98             }
 99             i = j;
100         }
101         for (i = dest; i != source;) {
102             j = route[i];
103             for (vit = arr[j].begin(); vit != arr[j].end(); vit++) {
104                 if (vit->d == i) {
105                     vit->capacity -= f;
106                     break;
107                 }
108             }
109             i = j;
110         }
111         max += f;
112         for (i = 0; i < MAX; i++) {
113             route[i] = -1;
114         }
115     }
116     delete[] route;
117     return max;
118 }

 

posted @ 2020-02-09 00:01  一斜星辰酱  阅读(929)  评论(0编辑  收藏  举报