标签:
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1266
先跑一遍最短路,然后将最短路上的边跑一遍最小割就得到答案
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <queue> 6 #define rep(i,l,r) for(int i=l; i<=r; i++) 7 #define clr(x,y) memset(x,y,sizeof(x)) 8 #define travel(x) for(Edge *p=last[x]; p; p=p->pre) 9 typedef long long ll; 10 using namespace std; 11 const int INF = 0x3f3f3f3f; 12 const int maxn = 510; 13 struct Edge{ 14 Edge *pre,*rev; int to,cost; 15 }edge[250010]; 16 Edge *last[maxn],*cur[maxn],*pt; 17 int n,m,ans=0,a[124760],b[124760],c[124760],t[124760],d[2][maxn]; 18 bool isin[maxn]; 19 queue <int> q; 20 inline int read(){ 21 int ans = 0, f = 1; 22 char c = getchar(); 23 while (!isdigit(c)){ 24 if (c == ‘-‘) f = -1; 25 c = getchar(); 26 } 27 while (isdigit(c)){ 28 ans = ans * 10 + c - ‘0‘; 29 c = getchar(); 30 } 31 return ans * f; 32 } 33 inline void addedge(int x,int y,ll z){ 34 pt->pre = last[x]; pt->to = y; pt->cost = z; last[x] = pt++; 35 } 36 inline void add(int x,int y,ll z){ 37 addedge(x,y,z); addedge(y,x,0); last[x]->rev = last[y]; last[y]->rev = last[x]; 38 } 39 void spfa(int x,int t){ 40 clr(d[t],INF); d[t][x] = 0; 41 clr(isin,0); isin[x] = 1; q.push(x); 42 while (!q.empty()){ 43 int now = q.front(); q.pop(); isin[now] = 0; 44 travel(now){ 45 if (d[t][p->to] > d[t][now] + p->cost){ 46 d[t][p->to] = d[t][now] + p->cost; 47 if (!isin[p->to]){ 48 isin[p->to] = 1; 49 q.push(p->to); 50 } 51 } 52 } 53 } 54 } 55 bool bfs(){ 56 while (!q.empty()) q.pop(); 57 clr(d[0],-1); d[0][1] = 0; q.push(1); 58 while (!q.empty()){ 59 int now = q.front(); q.pop(); 60 travel(now){ 61 if (d[0][p->to] == -1 && p->cost > 0){ 62 d[0][p->to] = d[0][now] + 1; 63 q.push(p->to); 64 if (p->to == n) return 1; 65 } 66 } 67 } 68 return 0; 69 } 70 int dfs(int x,int flow){ 71 if (x == n || (!flow)) return flow; int w = 0; 72 for(Edge *p=cur[x]; p && w < flow; p=p->pre){ 73 if (d[0][p->to] == d[0][x] + 1 && p->cost > 0){ 74 int delta = dfs(p->to,min(p->cost,flow-w)); 75 p->cost -= delta; 76 p->rev->cost += delta; 77 w += delta; 78 if (p->cost) cur[x] = p; 79 } 80 } 81 if (w < flow) d[0][x] = -1; 82 return w; 83 } 84 int main(){ 85 n = read(); m = read(); clr(last,0); pt = edge; 86 rep(i,1,m){ 87 a[i] = read(); b[i] = read(); t[i] = read(); c[i] = read(); 88 addedge(a[i],b[i],t[i]); addedge(b[i],a[i],t[i]); 89 } 90 spfa(1,0); spfa(n,1); 91 printf("%d\n",d[0][n]); 92 clr(last,0); pt = edge; 93 rep(i,1,m){ 94 if (d[0][a[i]] + t[i] + d[1][b[i]] == d[0][n]){ 95 add(a[i],b[i],c[i]); 96 } 97 if (d[0][b[i]] + t[i] + d[1][a[i]] == d[0][n]){ 98 add(b[i],a[i],c[i]); 99 } 100 } 101 while (bfs()){ 102 rep(i,1,n) cur[i] = last[i]; 103 ans += dfs(1,INF); 104 } 105 printf("%d\n",ans); 106 return 0; 107 }
标签:
原文地址:http://www.cnblogs.com/jimzeng/p/bzoj1266.html