标签:des c style class blog code
Time
Limit: 5000/1000 MS (Java/Others) Memory Limit:
32768/32768 K (Java/Others)
Total Submission(s):
2123 Accepted Submission(s):
642
#include <stdio.h> #include <string.h> #include <memory.h> #define min(a, b) ((a) < (b) ? (a) : (b)) #define REP(i, n) for(int i = 0; i < n; ++i) const int maxE = 1000000; const int maxN = 305; const int oo = 0x3f3f3f3f; struct Edge{ int v, c, w, n; }; Edge edge[maxE]; int adj[maxN], l; int d[maxN], cur[maxN], a[maxN]; int inq[maxN], Q[maxE], head, tail; int n, m, k, p; int cost, flow, s, t; void addedge(int u, int v, int c, int w){ edge[l].v = v; edge[l].c = c; edge[l].w = w; edge[l].n = adj[u]; adj[u] = l++; edge[l].v = u; edge[l].c = 0; edge[l].w = -w; edge[l].n = adj[v]; adj[v] = l++; } int SPFA(){ memset(d, oo, sizeof d); memset(inq, 0, sizeof inq); head = tail = 0; d[s] = 0; a[s] = oo; cur[s] = -1; Q[tail++] = s; while(head != tail){ int u = Q[head++]; inq[u] = 0; for(int i = adj[u]; ~i; i = edge[i].n){ int v = edge[i].v; if(!edge[i].c || d[v] <= d[u] + edge[i].w) continue; d[v] = d[u] + edge[i].w; cur[v] = i; a[v] = min(edge[i].c, a[u]); if(inq[v]) continue; inq[v] = 1; Q[tail++] = v; } } if(d[t] == oo) return 0; flow += a[t]; cost += a[t] * d[t]; for(int i = cur[t]; ~i; i = cur[edge[i ^ 1].v]){ edge[i].c -= a[t]; edge[i ^ 1].c += a[t]; } return 1; } int MCMF(){ flow = cost = 0; while(SPFA()); return cost; } void work(){ int u, v, w; while(~scanf("%d%d%d%d", &n, &m, &k, &p)){ memset(adj, -1, sizeof adj); l = 0; s = 0; t = n + m + 1; REP(i, n){ scanf("%d", &u); addedge(s, u, 1, 0); } REP(i, n){ addedge(m + 1 + i, t, 1, 0); } REP(i, k){ scanf("%d%d%d", &u, &v, &w); addedge(u, v, oo, w); addedge(v, u, oo, w); } REP(i, p){ scanf("%d%d%d", &u, &v, &w); addedge(v, u + m, 1, w); } printf("%d\n", MCMF()); } } int main(){ work(); return 0; }
HDU 2448 Mining Station on the Sea 费用流,布布扣,bubuko.com
HDU 2448 Mining Station on the Sea 费用流
标签:des c style class blog code
原文地址:http://www.cnblogs.com/ac-luna/p/3757797.html