标签:tor imei accept ted problem ems direct add family
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 78642 | Accepted: 30681 |
Description
Input
Output
Sample Input
5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
Sample Output
50
Source
1 #include "bits/stdc++.h" 2 using namespace std; 3 typedef long long LL; 4 const int MAX=205; 5 int n,m,s,t; 6 int tot,head[MAX],adj[MAX<<1],wei[MAX<<1],next[MAX<<1]; 7 int cur[MAX],deep[MAX]; 8 void addedge(int u,int v,int w){ 9 tot++;adj[tot]=v,wei[tot]=w,next[tot]=head[u],head[u]=tot; 10 } 11 bool bfs(){ 12 int i,j; 13 memset(deep,127,sizeof(deep)); 14 deep[s]=0; 15 queue <int> q; q.push(s); 16 while (!q.empty()){ 17 int u=q.front();q.pop(); 18 for (i=head[u];i;i=next[i]){ 19 if (deep[adj[i]]>1e9 && wei[i]>0){ 20 deep[adj[i]]=deep[u]+1; 21 q.push(adj[i]); 22 } 23 } 24 } 25 return deep[t]<1e9; 26 } 27 int dfs(int x,int flo){ 28 if (x==t || flo==0) return flo; 29 int j; 30 for (int &i=cur[x];i;i=next[i]){ 31 if (deep[adj[i]]==deep[x]+1 && wei[i]>0){ 32 j=dfs(adj[i],min(flo,wei[i])); 33 if (j){ 34 wei[i]-=j; 35 wei[i^1]+=j; 36 return j; 37 } 38 } 39 } 40 return 0; 41 } 42 int main(){ 43 freopen ("ditches.in","r",stdin);freopen ("ditches.out","w",stdout); 44 int i,j,u,v,w; 45 while (~scanf("%d%d",&m,&n)){ 46 tot=1;s=1,t=n; 47 memset(head,0,sizeof(head)); 48 for (i=1;i<=m;i++){ 49 scanf("%d%d%d",&u,&v,&w); 50 addedge(u,v,w),addedge(v,u,0); 51 } 52 int flow=0,dd; 53 while (bfs()){ 54 for (i=1;i<=n;i++) cur[i]=head[i]; 55 while (dd=dfs(s,1e9)) flow+=dd; 56 } 57 printf("%d\n",flow); 58 } 59 return 0; 60 }
POJ-1273 Drainage Ditches (网络流入门题)
标签:tor imei accept ted problem ems direct add family
原文地址:http://www.cnblogs.com/keximeiruguo/p/7749626.html