求助,WA0pts
查看原帖
求助,WA0pts
520056
luoyx楼主2023/8/6 07:51

之前的帖子都看了,还是没有找到错误。

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,m;
const int N=1e6+5,M=105;
int a[M][M];
int s,t;
int x[N],y[N],z[N];
int head[N],ecnt=1;
struct edge{
	int v,nxt,w;
}e[N];
int dis[N];
void add2(int u,int v,int w){
	e[++ecnt].v=v;
	e[ecnt].nxt=head[u];
	e[ecnt].w=w;
	head[u]=ecnt;
}
void add(int u,int v,int w){
	e[++ecnt].v=v;
	e[ecnt].nxt=head[u];
	e[ecnt].w=w;
	head[u]=ecnt;
	swap(u,v);
	e[++ecnt].v=v;
	e[ecnt].nxt=head[u];
	e[ecnt].w=0;
	head[u]=ecnt;
}
int dep[N],vis[N],maxflow;

int bfs(){
	queue<int> q;
	memset(vis,0,sizeof(vis));
	memset(dep,0x3f,sizeof(dep));
	dep[s]=0;
	q.push(s);
	while(!q.empty()){
		int u=q.front();
		q.pop();
		vis[u]=0;
		for(int i=head[u];i;i=e[i].nxt){
			int v=e[i].v;
			if(dep[v]>dep[u]+1&&e[i].w){
				dep[v]=dep[u]+1;
				if(vis[v]==0){
					vis[v]=1;
					q.push(v);
				}
			}
		}
	}
	return dep[t]<0x3f3f3f3f;
}
int cur[N];
int dfs(int u,int flow){
	int rflow=0;
	if(u==t) return flow;
	for(int i=cur[u];i;i=e[i].nxt){
		cur[u]=i;
		int v=e[i].v;
		if(e[i].w&&dep[v]==dep[u]+1){
			if(rflow=dfs(v,min(flow,e[i].w))){
				e[i].w-=rflow;
				e[i^1].w+=rflow;
				return rflow;
			}
		}
	}
	return 0;
}
void dinic(){
	int lowflow=0;
	while(bfs()){
		for(int i=1;i<=2e5;i++){
			cur[i]=head[i];
		}
		while(lowflow=dfs(s,1e18)) maxflow+=lowflow;
	}
}

void spfa(){
	queue<int> q;
	for(int i=1;i<1e6;i++) dis[i]=1e18;
	dis[s]=0;
	q.push(s);
	while(!q.empty()){
		int u=q.front();
		q.pop();
		for(int i=head[u];i;i=e[i].nxt){
			int v=e[i].v;
			if(dis[v]>dis[u]+e[i].w){
				dis[v]=dis[u]+e[i].w;
				q.push(v);
			}
		}
	}
}

signed main(){
	cin>>n>>m;
	s=1,t=n+n;
	int u,v,w;
	for(int i=1;i<=m;i++){
		cin>>u>>v>>w;
		x[i]=u,y[i]=v,z[i]=w;
		add2(u,v,w),add2(v,u,w);
	}
	spfa();
	memset(e,0,sizeof(e));
	memset(head,0,sizeof(head));
	ecnt=1;
	for(int i=1;i<=m;i++){
		if(dis[y[i]]==dis[x[i]]+z[i]){
			add(x[i]+n,y[i],1e18),add(y[i]+n,x[i],1e18);
		}
	}
	for(int i=1;i<=n;i++){
		cin>>u;
		if(i==1||i==n) add(i,i+n,1e18);
		else add(i,i+n,u);
	}
	dinic();
	cout<<maxflow;
}
2023/8/6 07:51
加载中...