标签:content strong cos inf contains target review ann each
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2098 Accepted Submission(s): 616
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define inf 0x3f3f3f3f 4 #define pii pair<int,int> 5 #define mp make_pair 6 struct Edge 7 { 8 int v,cap,flow,next; 9 }e[200020]; 10 vector<int> g[1010]; 11 int first[1010],d[1010],cur[1010],tot,N; 12 bool vis[1010]; 13 void add(int u,int v,int cap){ 14 e[tot]=Edge{v,cap,0,first[u]}; 15 first[u]=tot++; 16 } 17 int dij(){ 18 memset(d,inf,sizeof(d)); 19 memset(vis,0,sizeof(vis)); 20 d[1]=0; 21 priority_queue<pii,vector<pii>,greater<pii> >q; 22 q.push(mp(0,1)); 23 while(!q.empty()){ 24 int u=q.top().second;q.pop(); 25 if(vis[u]) continue; 26 vis[u]=1; 27 for(int i=0;i<g[u].size();++i){ 28 if(d[g[u][i]]>d[u]+1){ 29 d[g[u][i]]=d[u]+1; 30 q.push(mp(d[g[u][i]],g[u][i])); 31 } 32 } 33 } 34 return d[N]; 35 } 36 bool bfs(){ 37 memset(d,0,sizeof(d)); 38 memset(vis,0,sizeof(vis)); 39 queue<int>q; 40 q.push(1); 41 d[1]=0; 42 vis[1]=1; 43 while(!q.empty()){ 44 int u=q.front();q.pop(); 45 for(int i=first[u];~i;i=e[i].next){ 46 if(!vis[e[i].v] && e[i].cap-e[i].flow>0){ 47 vis[e[i].v]=1; 48 d[e[i].v]=d[u]+1; 49 q.push(e[i].v); 50 } 51 } 52 } 53 return vis[N]; 54 } 55 int dfs(int u,int a){ 56 if(u==N || a==0) return a; 57 int f,ans=0; 58 for(int &i=cur[u];~i;i=e[i].next){ 59 if(d[e[i].v]==d[u]+1 && (f=dfs(e[i].v,min(a,e[i].cap-e[i].flow)))>0){ 60 e[i].flow+=f; 61 e[i^1].flow-=f; 62 a-=f; 63 ans+=f; 64 if(!a) break; 65 } 66 } 67 return ans; 68 } 69 void solve(){ 70 int ans=0; 71 while(bfs()){ 72 for(int i=1;i<=N;++i) cur[i]=first[i]; 73 ans+=dfs(1,inf); 74 } 75 printf("%d\n",ans); 76 } 77 int main(){ 78 int M,t,i,j,k; 79 int u[10100],v[10100],w[10100]; 80 cin>>t; 81 while(t--){ 82 scanf("%d%d",&N,&M); 83 memset(first,-1,sizeof(first)); 84 tot=0; 85 for(i=1;i<=N;++i) g[i].clear(); 86 for(i=1;i<=M;++i){ 87 scanf("%d%d%d",u+i,v+i,w+i); 88 g[u[i]].push_back(v[i]); 89 g[v[i]].push_back(u[i]); 90 } 91 dij(); 92 for(i=1;i<=M;++i){ 93 if(d[u[i]]+1==d[v[i]]){ 94 add(u[i],v[i],w[i]),add(v[i],u[i],0); 95 96 } 97 if(d[v[i]]+1==d[u[i]]){ 98 add(v[i],u[i],w[i]),add(u[i],v[i],0); 99 } 100 } 101 solve(); 102 } 103 return 0; 104 }
标签:content strong cos inf contains target review ann each
原文地址:https://www.cnblogs.com/zzqc/p/9398606.html