蒟蒻初学网络流,dicnic最大流模板求助
查看原帖
蒟蒻初学网络流,dicnic最大流模板求助
421265
eastcloud楼主2022/12/23 18:12
// Problem: P1343 地震逃生
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1343
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#define inf (1<<31)+1
#define ll long long
using namespace std;
int edg[100001],to[100001],head[100001],nex[100001];
int d[100001];
int tot;
queue<int> t;
void add(int u,int v,int c){
	edg[++tot]=c;to[tot]=v;nex[tot]=head[u];head[u]=tot;
	edg[++tot]=0;to[tot]=u;nex[tot]=head[v];head[v]=tot;
}
int n;
bool bfs(int s,int e){
	while(t.size()) t.pop();
	for(int i=1;i<=n;i++) d[i]=0;
	t.push(s);d[s]=1;
	while(!t.empty()){
		int u=t.front();t.pop();
		for(int i=head[u];i;i=nex[i]){
			int v=to[i];
			if(edg[i] && !d[v]){
				d[v]=d[u]+1;
				t.push(v);
				if(v==e) return true;
			}
		}
	}
	return false;
}
inline int dfs(int x,int flow){
	if(x==n) return flow;
	int rest=flow,k;
	for(int i=head[x];i && rest;i=nex[i]){
		if(edg[i] && d[to[i]]==d[x]+1){
			k=dfs(to[i],min(rest,edg[i]));
			if(!k) d[to[i]]=0;
			edg[i]-=k;
			edg[i^1]+=k;
			rest-=k;
		}
	}
	return flow-rest;
}
int main(){
	int m,x,u,v,c;
	cin>>n>>m>>x;
	for(int i=1;i<=m;i++){
		cin>>u>>v>>c;
		add(u,v,c);
	}
	int maxflow=0,flag=0;
	while(bfs(1,n)){
		flag=1;
		while(1){
			int flow=dfs(1,inf);
			if(!flow) break;
			maxflow+=flow;
		}
	}
	if(!flag) cout<<"Orz Ni Jinan Saint Cow!";
	else cout<<maxflow<<' '<<(x%maxflow==0?x/maxflow:x/maxflow+1);
}

错误数据:

5 10 642
4 2 131
1 5 30
1 2 18
2 5 96
3 5 138
1 4 135
2 5 147
4 3 164
1 3 159
4 2 124

2022/12/23 18:12
加载中...