Re on #5
查看原帖
Re on #5
534562
liheyang123楼主2024/11/25 22:35

测试信息:

Runtime Error.
Received signal 11: Segmentation fault with invalid memory reference.

ISAP

代码如下:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf=0x3f3f3f3f3f3f3f3f;
const int N=250;
const int M=10010;
int n,m,s,t;

struct node {
	int from,to,next;
	ll val;
} a[M];
int pre[N],tot=1;
inline void add(int u,int v,ll w) {
	a[++tot]= {u,v,pre[u],w};
	pre[u]=tot;
}
ll f[N][N];
struct net {
	int now[M],r[M];
	int dep[M],gap[M];
	inline void bfs() {
		for(int i=1; i<=tot; i++)gap[i]=dep[i]=0;
		dep[t]=1;
		queue<int>q;
		q.push(t);
		while(!q.empty()) {
			int p=q.front();
			q.pop();
			gap[dep[p]]++;
			for(int i=pre[p]; i; i=a[i].next) {
				int to=a[i].to;
				if(a[i^1].val&&dep[to]==0) {
					dep[to]=dep[p]+1;
					q.push(to);
				}
			}
		}
	}
	inline ll augment() {
		ll p=t,flow=inf;
		while(p!=s) {
			int u=r[p];
			if(a[u].val<flow)flow=a[u].val;
			p=a[u].from;
		}
		p=t;
		while(p!=s) {
			int u=r[p];
			a[u].val-=flow;
			a[u^1].val+=flow;
			p=a[u].from;
		}
		return flow;
	}
	inline void ISAP() {
		bfs();
		int u=s;
		ll flow=0;
		memcpy(now,pre,sizeof(pre));
		while(dep[s]<=n) {
			if(u==t) {
				flow+=augment();
				u=s;
			}
			bool flag=0;
			for(int i=now[u]; i; i=a[i].next) {
				int to=a[i].to;
				if(a[i].val&&dep[to]+1==dep[u]) {
					flag=1;
					r[to]=i;
					now[u]=i;
					u=to;
					break;
				}
			}
			if(!flag) {
				if(!--gap[dep[u]])break;
				int mindep=99999999;
				for(int i=pre[u]; i; i=a[i].next) {
					int to=a[i].to;
					if(dep[to]<mindep&&a[i].val) {
						mindep=dep[to];
					}
				}
				dep[u]=mindep+1;
				gap[dep[u]]++;
				now[u]=pre[u];
				if(u!=s)u=a[r[u]].from;
			}
		}
		cout<<flow;
	}
} net;

int main() {
	cin>>n>>m>>s>>t;
	for(int i=1; i<=m; i++) {
		int x,y,l;
		cin>>x>>y>>l;
		if(!f[x][y]) {
			add(x,y,l);
			add(y,x,0);
			f[x][y]=tot-1;
		} else {
			a[f[x][y]].val+=l;
		}
	}
	net.ISAP();
	return 0;
}

2024/11/25 22:35
加载中...