最大流:
EK算法:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
const int inf=0x7fffffff;
const int maxn=10010;
struct node{
int u,v,f,next;
}edge[300050];
int s,t,cnt,maxflow,head[maxn],pre[maxn];
bool vis[maxn];
int n,m,tot,a[101][101],b[101][101],pos[101][101];
void add(int u,int v,int f){
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].f=f;
edge[cnt].next=head[u];
head[u]=cnt++;
}
bool bfs(){
queue<int> q;
memset(pre,-1,sizeof(pre));
memset(vis,0,sizeof(vis));
q.push(s);
vis[s]=true;
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].v;
int f=edge[i].f;
if(f>0&&!vis[v]){
vis[v]=true;
pre[v]=i;
if(v==t) return 1;
q.push(v);
}
}
}
return 0;
}
void EK(){
int flow;
while(bfs()){
flow=inf;
int x=pre[t];
while(x!=-1){
flow=min(flow,edge[x].f);
x=pre[edge[x].u];
}
x=pre[t];
while(x!=-1){
edge[x].f-=flow;
edge[x^1].f+=flow;
x=pre[edge[x].u];
}
maxflow+=flow;
}
}
int main(){
while(cin>>m>>n){
memset(head,-1,sizeof(head));cnt=0;
for(int i=1;i<=m;i++){
int x,y,f;
cin>>x>>y>>f;
add(x,y,f);
add(y,x,0);
}
s=1