神秘问题求问
查看原帖
神秘问题求问
368204
ShanQing楼主2024/3/1 21:44
//writer:Oier_szc

#include <bits/stdc++.h>
//#include <windows.h>
#define TS cerr<<"I AK IOI"<<endl;
#define cr(x) cerr<<x<<endl;
#define cr2(x,y) cerr<<x<<" "<<y<<endl;
#define cr3(x,y,z) cerr<<x<<" "<<y<<" "<<z<<endl;
//#define int long long
using namespace std;
const int N=45,M=105,VN=1e5+5,VM=4e5+5,INF=2e9,mod=1e9+7;
int n,m,s,t,ans=0;
int p[N];
int a[N][M];
int head[VN],tot=1,idx=0;
struct edge
{
	int ne,to,w,c;
}e[VM];
void add(int u,int v,int w,int c)
{
	e[++tot]={head[u],v,w,c};
	head[u]=tot;
	e[++tot]={head[v],u,0,-c};
	head[v]=tot;
}
int dis[VN],vis[VN],incf[VN],pre[VN],id1[VN],id2[VN];
bool spfa()
{
	for(int i=0;i<=idx;++i)
	{
		dis[i]=incf[i]=INF,vis[i]=false,pre[i]=-1;
	}
	dis[t]=incf[t]=INF,vis[t]=false,pre[t]=-1;
	queue<int> q;
	q.push(s),dis[s]=0;
	while(!q.empty())
	{
		int now=q.front(),to;
		q.pop();
		vis[now]=false;
		for(int i=head[now];i;i=e[i].ne)
		{
			to=e[i].to;
			if(e[i].w&&dis[now]+e[i].c<dis[to])
			{
				dis[to]=dis[now]+e[i].c;
				incf[to]=min(incf[now],e[i].w);
				pre[to]=i;
				if(!vis[to])
				{
					vis[to]=true;
					q.push(to);
				}
			}
		}
	}
	return dis[t]!=INF;
}
void MCMF()
{
	while(spfa())
	{
		int x=t,ls;
		ans+=incf[t]*dis[t];
		while(x!=s)
		{
			ls=pre[x];
			e[ls].w-=incf[t];
			e[ls^1].w+=incf[t];
			if(x!=t&&e[ls^1].to==s) 
			{
				add(s,++idx,1,0);
				id1[idx]=id1[x],id2[idx]=id2[x]+1;
				for(int j=1;j<=n;++j)
				{
					add(idx,j,1,a[id1[idx]][j]*id2[idx]);
				}
			}
			x=e[ls^1].to;
		}
	}
}
int main()
{
	//freopen("P2050_2.in","r",stdin);
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i)
	{
		scanf("%d",&p[i]);
	}
	for(int i=1;i<=n;++i)
	{
		for(int j=1;j<=m;++j)
		{
			scanf("%d",&a[j][i]);
		}
	}
	s=0,t=1e3;
	idx=n;
	//cout<<head[1]<<endl;
	for(int i=1;i<=n;++i) 
	{
		add(i,t,p[i],0);
	}
	for(int i=1;i<=m;++i)
	{
		add(s,++idx,1,0);
		id1[idx]=i,id2[idx]=1;
		for(int j=1;j<=n;++j)
		{
			add(idx,j,1,a[i][j]);
		}
	}
	MCMF();
	printf("%d",ans);
	return 0;
}

这份代码RE50pts,经排查发现有的e[i].ne连成了一个环,非常的逆天。继续调试发现head[i]的初始值竟然不是0,比如输出head[1]显示456而不是0,而在这之前没有任何加边操作,头一次见,非常奇怪。求问原因,急.

2024/3/1 21:44
加载中...