蒟蒻WA80pts最后两个点 用的dinic 求助
查看原帖
蒟蒻WA80pts最后两个点 用的dinic 求助
271642
Evier楼主2021/2/24 18:22
#include<bits/stdc++.h>
using namespace std;
#define inf 2e9
inline int read(){
	int X=0; bool flag=1; char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();}
	while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
	if(flag) return X;
	return ~(X-1);
}
int d[5005],head[5005],now[5005];
int p[50005],c[50005];
int sum;
struct{
	int next,to,w;
}e[400000];
int n,m,s,t;
int tot=1;
int maxflow;
inline void add(int u,int v,int dis){
	e[++tot].next=head[u];
	e[tot].to=v;
	e[tot].w=dis;
	head[u]=tot;
	e[++tot].to=u;
	e[tot].w=0;
	e[tot].next=head[v];
	head[v]=tot;
}
queue<int> q;
inline bool bfs(){
	memset(d,0,sizeof(d));
	while(!q.empty())q.pop();
	d[s]=1;q.push(s);now[s]=head[s];
	while(!q.empty()){
		int x=q.front();q.pop();
		for(int i=head[x];i;i=e[i].next){
			int y=e[i].to;
			if(e[i].w&&!d[y]){
				q.push(y);
				d[y]=d[x]+1;
				now[y]=head[y];
				if(y==t)return 1;
			}
		}
	}
	return 0;
}
int dinic(int x,int flow){
	if(x==t)return flow;
	int rest=flow,k,i;
	for(i=head[x];i&&rest;i=e[i].next)
		if(e[i].w&&d[e[i].to]==d[x]+1){
			k=dinic(e[i].to,e[i].w>rest?rest:e[i].w);
			if(!k)d[e[i].to]=0;
			e[i].w-=k;
			e[i^1].w+=k;
			rest-=k;
		}
	now[x]=i;
	return flow-rest;
}
signed main(){
	n=read();m=read();
	s=0,t=n+m+1;
	for(int i=1;i<=n;i++){
		p[i]=read();
	}
	for(int i=1;i<=m;i++){
		int a,b;
		a=read();b=read();c[i]=read();
		add(s,i,c[i]);add(i,m+a,inf);add(i,m+b,inf);
		sum+=c[i];
	}
	for(int i=1;i<=n;i++)add(i+m,t,p[i]);
	while(bfs()){
		maxflow+=dinic(s,inf);
	}
	cout<<sum-maxflow;
}
2021/2/24 18:22
加载中...