Mining Station on the Sea HDU - 2448(費用流 || 最短路 && hc)
阿新 • • 發佈:2018-11-10
Mining Station on the Sea
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3565 Accepted Submission(s): 1108
Due to the fact that some mining stations can not communicate with each other directly, for the safety of the navigation for ships, ships are only allowed to sail between mining stations which can communicate with each other directly.
The mining is arduous and people do this job need proper rest (that is, to allow the ship to return to the port). But what a coincidence! This time, n vessels for mining take their turns to take a rest at the same time. They are scattered in different stations and now they have to go back to the port, in addition, a port can only accommodate one vessel. Now all the vessels will start to return, how to choose their navigation routes to make the total sum of their sailing routes minimal.
Notice that once the ship entered the port, it will not come out!
Sample Input 3 5 5 6 1 2 4 1 3 3 1 4 4 1 5 5 2 5 3 2 4 3 1 1 5 1 5 3 2 5 3 2 4 6 3 1 4 3 2 2
Sample Output 13
Source 2008 Asia Regional Harbin
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <cctype> #include <set> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <cmath> #include <bitset> #define rap(i, a, n) for(int i=a; i<=n; i++) #define rep(i, a, n) for(int i=a; i<n; i++) #define lap(i, a, n) for(int i=n; i>=a; i--) #define lep(i, a, n) for(int i=n; i>a; i--) #define rd(a) scanf("%d", &a) #define rlld(a) scanf("%lld", &a) #define rc(a) scanf("%c", &a) #define rs(a) scanf("%s", a) #define rb(a) scanf("%lf", &a) #define rf(a) scanf("%f", &a) #define pd(a) printf("%d\n", a) #define plld(a) printf("%lld\n", a) #define pc(a) printf("%c\n", a) #define ps(a) printf("%s\n", a) #define MOD 2018 #define LL long long #define ULL unsigned long long #define Pair pair<int, int> #define mem(a, b) memset(a, b, sizeof(a)) #define _ ios_base::sync_with_stdio(0),cin.tie(0) //freopen("1.txt", "r", stdin); using namespace std; const int maxn = 300000, INF = 0x3f3f3f3f, LL_INF = 0x7fffffffffffffff; int n, m, k, q, s, t; int head[3000], d[3000], vis[3000], p[3000], f[3000], inc[3000], nex[maxn]; int flow, value, cnt; struct node { int u, v, w, c; }Node[maxn]; void add_(int u, int v, int w, int c) { Node[cnt].u = u; Node[cnt].v = v; Node[cnt].w = w; Node[cnt].c = c; nex[cnt] = head[u]; head[u] = cnt++; } void add(int u, int v, int w, int c) { add_(u, v, w, c); add_(v, u, -w, 0); } int spfa() { deque<int> Q; mem(vis, 0); mem(p, -1); mem(d, INF); d[s] = 0; Q.push_front(s); vis[s] = 1; p[s] = 0, f[s] = INF; while(!Q.empty()) { int u = Q.front(); Q.pop_front(); vis[u] = 0; for(int i = head[u]; i != -1; i = nex[i]) { node e = Node[i]; if(d[e.v] > d[u] + Node[i].w && Node[i].c > 0) { d[e.v] = d[u] + Node[i].w; p[e.v] = i; f[e.v] = min(f[u], Node[i].c); if(!vis[e.v]) { if(Q.empty()) Q.push_front(e.v); else { if(d[e.v] < d[Q.front()]) Q.push_front(e.v); else Q.push_back(e.v); } vis[e.v] = 1; } } } } if(p[t] == -1) return 0; flow += f[t]; value += f[t] * d[t]; for(int i = t; i != s; i = Node[p[i]].u) { Node[p[i]].c -= f[t]; Node[p[i] ^ 1].c += f[t]; } return 1; } void max_flow() { value = flow = 0; while(spfa()); pd(value); } void init() { mem(head, -1); cnt = 0; } int main() { while(scanf("%d%d%d%d", &n, &m, &k, &q) != EOF) { init(); s = 0; t = m + n + 1; int u, v, w, tmp; for(int i = 1; i <= n; i++) { add(i + m, t, 0, 1); scanf("%d", &tmp); add(s, tmp, 0, 1); } for(int i = 1; i <= k; i++) { scanf("%d%d%d", &u, &v, &w); add(u, v, w, INF); add(v, u, w, INF); } for(int i = 1; i <= q; i++) { scanf("%d%d%d", &u, &v, &w); add(v, m + u, w, 1); } max_flow(); } return 0; }
Mining Station on the Sea
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3565 Accepted Submission(s): 1108
Due to the fact that some mining stations can not communicate with each other directly, for the safety of the navigation for ships, ships are only allowed to sail between mining stations which can communicate with each other directly.
The mining is arduous and people do this job need proper rest (that is, to allow the ship to return to the port). But what a coincidence! This time, n vessels for mining take their turns to take a rest at the same time. They are scattered in different stations and now they have to go back to the port, in addition, a port can only accommodate one vessel. Now all the vessels will start to return, how to choose their navigation routes to make the total sum of their sailing routes minimal.
Notice that once the ship entered the port, it will not come out!
Input There are several test cases. Every test case begins with four integers in one line, n (1 = <n <= 100), m (n <= m <= 200), k and p. n indicates n vessels and n ports, m indicates m mining stations, k indicates k edges, each edge corresponding to the link between a mining station and another one, p indicates p edges, each edge indicating the link between a port and a mining station. The following line is n integers, each one indicating one station that one vessel belongs to. Then there follows k lines, each line including 3 integers a, b and c, indicating the fact that there exists direct communication between mining stations a and b and the distance between them is c. Finally, there follows another p lines, each line including 3 integers d, e and f, indicating the fact that there exists direct communication between port d and mining station e and the distance between them is f. In addition, mining stations are represented by numbers from 1 to m, and ports 1 to n. Input is terminated by end of file.
Output Each test case outputs the minimal total sum of their sailing routes.
Sample Input 3 5 5 6 1 2 4 1 3 3 1 4 4 1 5 5 2 5 3 2 4 3 1 1 5 1 5 3 2 5 3 2 4 6 3 1 4 3 2 2
Sample Output 13
Source 2008 Asia Regional Harbin