记录编号 526430 评测结果 AAAAAAAAAAAA
题目名称 草地排水 最终得分 100
用户昵称 GravatarShirry 是否通过 通过
代码语言 C++ 运行时间 0.007 s
提交时间 2018-12-29 20:50:09 内存使用 3.16 MiB
显示代码纯文本
#include<cstdio>
#include<queue>
#include<vector>
#include<cstring>
#include<algorithm>
#define inf 0x3f3f3f
using namespace std;
const int maxn=300;
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];
	bool Bfs(){
		memset(vis,0,sizeof(vis));
		queue<int>q;
		q.push(s);
		d[s]=0,vis[s]=1;
		while(!q.empty()){
			int x=q.front();q.pop();
			for(int i=0;i<(int)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];
	}
	void Addedge(int from,int to,int cap){
		edges.push_back((Edge){from,to,cap,0});
		edges.push_back((Edge){to,from,0,0});
		int m=edges.size();
		G[from].push_back(m-2);
		G[to].push_back(m-1); 
	}
	int Dfs(int x,int a){
		if(x==t||a==0)return a;
		int flow=0,f;
		for(int i=0;i<(int)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 a,int b){
		this->s=a;
		this->t=b;
		int flow=0;
		while(Bfs()){
			flow+=Dfs(s,inf);
		}
		return flow;
	}
};
int main(){
	freopen("ditch.in","r",stdin);
	freopen("ditch.out","w",stdout);
	int N,M;
	Dinic A;
	scanf("%d%d",&N,&M);
	for(int i=1;i<=N;i++){
		int S,E,C;
		scanf("%d%d%d",&S,&E,&C);
		A.Addedge(S,E,C); 
	}
	int ans=A.Maxflow(1,M);
	printf("%d",ans);
	return 0;
}