1 bool spfa() 2 { 3 fill(vs,vs+n+2,false); 4 fill(d,d+n+2,inf); 5 fill(father,father+n+2,-1); 6 queue<int>q; 7 d[0]=0; 8 q.push(0); 9 while (!q.empty()) 10 { 11 int u=q.front(); 12 q.pop(); 13 vs[u]=false; 14 for (int i=head[u];i!=-1;i=eage[i].next) 15 { 16 int v=eage[i].v; 17 if (eage[i].cap&&d[v]>d[u]+eage[i].cost) 18 { 19 d[v]=d[u]+eage[i].cost; 20 father[v]=i; 21 if (!vs[v]) 22 { 23 vs[v]=true; 24 q.push(v); 25 } 26 } 27 } 28 } 29 if (father[n+1]==-1) return false; 30 return true; 31 } 32 33 int solve() 34 { 35 int ans=0; 36 while (spfa()) 37 { 38 ans+=d[n+1]; 39 int u=n+1; 40 while (u!=0) 41 { 42 int i=father[u]; 43 eage[i].cap--; 44 eage[i^1].cap++; 45 u=eage[i].u; 46 } 47 } 48 return ans; 49 }
原文:http://www.cnblogs.com/pblr/p/5719635.html