C/C++
#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;
}
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;
}
下一篇:金木研33333
声明:以上文章均为用户自行添加,仅供打字交流使用,不代表本站观点,本站不承担任何法律责任,特此声明!如果有侵犯到您的权利,请及时联系我们删除。
文章熱度:★☆☆☆☆
文章難度:☆☆☆☆☆
文章質量:★☆☆☆☆
說明:系統根據文章的熱度、難度、質量自動認證,已認證的文章將參與打字排名!
本文打字排名TOP20
- 1游客18545289 09-16 22:52181 KPM
- 2游客16464272 02-28 16:34128 KPM
- 3游客35050945 05-21 00:16127 KPM
- 4typingtodead 11-13 19:36120 KPM
- 5游客13157895... 03-23 18:14120 KPM
- 6游客14579776... 04-04 19:20108 KPM
- 7游客20802493... 12-20 16:03107 KPM
- 8游客12251314... 11-20 21:28104 KPM
- 9游客20864835... 12-25 14:44100 KPM
- 10游客20675506... 12-12 10:3890 KPM
- 11游客11116132... 05-22 10:4588 KPM
- 12游客23804673 08-21 17:3887 KPM
- 13游客13139054... 02-18 21:4984 KPM
- 14游客13087233... 03-23 00:3284 KPM
- 15柳叶刀 08-26 09:1083 KPM
- 16游客31831309 03-22 21:0981 KPM
- 17游客17933796 03-31 17:1679 KPM
- 18zlgan 09-29 19:2878 KPM
- 19DraculaAug... 07-25 16:0876 KPM
- 20游客20793078... 12-19 16:0876 KPM
用户更多文章推荐
- 一人我饮酒醉2017-02-27