C/C++

貢獻者:游客16464272 類別:代码 時間:2017-02-28 16:28:40 收藏數:55 評分:0.5
返回上页 舉報此文章
请选择举报理由:




收藏到我的文章 改錯字
#include<bits/stdc++.h>
using namespace std;
const int maxn=650;
const int INF=0x3f3f3f3f;
struct Edge{
int from,to,cap,flow;
};
struct Dinic{
int n,m,s,t;
vector<Edge>edges;
vector<int>G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
void AddEdge(int from,int to,int cap){
edges.push_back((Edge){from,to,cap,0});
edges.push_back((Edge){to,from,0,0});
m=edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool BFS(){
int x,i;
memset(vis,0,sizeof(vis));
queue<int>Q;
Q.push(s);
d[s]=0;
vis[s]=1;
while(!Q.empty()){
x=Q.front(),Q.pop();
for(i=0;i<G[x].size();i++){
Edge & e =edges[G[x][i]];
if(!vis[e.to]&&e.cap>e.flow){
vis[e.to]=1;
d[e.to]=d[x]+1;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x,int a){
if(x==t||a==0)
return a;
int flow=0,f;
for(int &i=cur[x];i<G[x].size();i++){
Edge & e=edges[G[x][i]];
if(d[x]+1==d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>0){
e.flow+=f;
edges[G[x][i]^1].flow-=f;
flow+=f;
a-=f;
if(a==0)
break;
}
}
return flow;
}
int Maxflow(int s,int t){
this->s=s,this->t=t;
int flow=0;
while(BFS()){
memset(cur,0,sizeof(cur));
flow+= DFS(s,INF);
}
return flow;
}
};
int main(){
int n,m,ss,tt,i,j,k,a,b,c,res;
while(scanf("%d%d",&n,&m)!=EOF){
Dinic tmp;
for(i=0;i<m;i++){
scanf("%d%d%d",&a,&b,&c);
tmp.AddEdge(a,b,c);
}
res=tmp.Maxflow(0,n);
printf("%d\n",res);
}
return 0;
}
声明:以上文章均为用户自行添加,仅供打字交流使用,不代表本站观点,本站不承担任何法律责任,特此声明!如果有侵犯到您的权利,请及时联系我们删除。
文章熱度:
文章難度:
文章質量:
說明:系統根據文章的熱度、難度、質量自動認證,已認證的文章將參與打字排名!

本文打字排名TOP20

用户更多文章推荐