为什么建图的时候要把ctt置为0不会wa会t
查看原帖
为什么建图的时候要把ctt置为0不会wa会t
292029
幽理家的男人楼主2021/3/3 16:30
#include<bits/stdc++.h>
using namespace std;
const int maxn=5005;
const int maxm=50005*2;
const int inf=2147483647;
int read()
{
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();} 
	return x*f;
}
typedef struct a{
	int to,flow,cost,next;
}per;
int n,m,s,t,u,v,w,c,ctt=1,last[maxn],head[maxn],vis[maxn],flow[maxn],cost[maxn],pre[maxn];//ctt在这呢,就是建图的时候用的 
long long maxflow,mincost;
per si[maxm];
queue<int> que;
void add(int u,int v,int flow,int cost){
	si[++ctt].to=v;
	si[ctt].next=head[u];
	si[ctt].cost=cost;
	si[ctt].flow=flow;
	head[u]=ctt;
}
int spfa(){
	for(int i=1;i<=n;++i){
		vis[i]=0;
		cost[i]=inf;
	}	
	flow[s]=inf; 
	que.push(s);vis[s]=1,pre[t]=-1,cost[s]=0;
	while(!que.empty()){
		int now=que.front();
		que.pop();
		vis[now]=0;
		for(int i=head[now];i!=-1;i=si[i].next){
			if(si[i].flow>0&&cost[si[i].to]>cost[now]+si[i].cost){
				cost[si[i].to]=cost[now]+si[i].cost;
				pre[si[i].to]=now;
				last[si[i].to]=i;
				flow[si[i].to]=min(flow[now],si[i].flow);
				if(!vis[si[i].to]){
					vis[si[i].to]=1;
					que.push(si[i].to);
				}
			}
		}
	}
	return pre[t]!=-1; 
}
int main(){
	memset(head,-1,sizeof(head));
	cin>>n>>m>>s>>t;
	for(int i=1;i<=m;++i){
		u=read();v=read();w=read();c=read();
		add(u,v,w,c);add(v,u,0,-c);
	}
	while(spfa()){
		maxflow+=flow[t];
		mincost+=flow[t]*cost[t];
		int now=t;
		while(now!=s){
			si[last[now]].flow-=flow[t];
			si[last[now]^1].flow+=flow[t];
			now=pre[now];
		}
	}
	cout<<maxflow<<' '<<mincost;
	return 0;
} 

ctt如果是0的话,再用异或的方法难道不会wa么,为什么t了四个点呢。

2021/3/3 16:30
加载中...