标签:des c style class blog code
Time
Limit: 10000/3000 MS (Java/Others) Memory Limit:
65536/32768 K (Java/Others)
Total Submission(s):
1114 Accepted Submission(s):
387
#include <stdio.h> #include <string.h> #include <algorithm> #define min(a, b) ((a) < (b) ? (a) : (b)) #define REP(i, n) for(int i = 1; i <= n; ++i) #define MS0(X) memset(X, 0, sizeof X) #define MS1(X) memset(X, -1, sizeof X) using namespace std; const int maxE = 3000000; const int maxN = 105; 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 cost, flow, s, t; int n, m, S, T; int deg[maxN]; 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){ d[v] = d[u] + edge[i].w; cur[v] = i; a[v] = min(edge[i].c, a[u]); if(!inq[v]){ 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 flow; } void work(){ int u, v, a, b, sum = 0; scanf("%d%d%d%d", &n, &m, &S, &T); MS1(adj); MS0(deg); l = 0; while(m--){ scanf("%d%d%d%d", &u, &v, &a, &b); if(a > b){ addedge(v, u, 1, a - b); sum += b; } else{ addedge(u, v, 1, b - a); sum += a; ++deg[u]; --deg[v]; } } ++deg[T]; --deg[S]; s = 0; t = n + 1; int nflow = 0; REP(i, n){ if(deg[i] > 0){ addedge(s, i, deg[i], 0); nflow += deg[i]; } if(deg[i] < 0){ addedge(i, t, -deg[i], 0); } } MCMF(); if(nflow == flow){ printf("%d\n", cost + sum); } else printf("impossible\n"); } int main(){ int T, cas; for(scanf("%d", &T), cas = 1; cas <= T; cas++){ printf("Case %d: ", cas); work(); } return 0; }
HDU 4067 Random Maze 费用流,布布扣,bubuko.com
标签:des c style class blog code
原文地址:http://www.cnblogs.com/ac-luna/p/3759717.html