呆码求调
查看原帖
呆码求调
726870
Rainber楼主2024/3/24 19:52
#include<cstdio>
#include<queue>
#include<utility>
//using std::priority_queue;
using std::queue;
using std::pair;
using std::make_pair;
using std::vector;
using std::greater;
int next[200000];
int to[200000];
int weight[200000];
int head[50000];
int n,m,ans=2e9;
//priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;
queue<pair<int,int> > q;
int dis[6][50000];
//bool vis[6][50000];
void spfa(int s,int idx)
{
	int i,p;
	dis[idx][s]=0;
	q.push(make_pair(0,s));
	while(!q.empty())
	{
		p=q.front().second;
		q.pop();
		for(i=head[p];i!=-1;i=next[i])
		{
			if(dis[idx][p]+weight[i]<dis[idx][to[i]])
			{
//				printf("%d %d\n",dis[idx][p]+weight[i],dis[idx][to[i]]);
				dis[idx][to[i]]=dis[idx][p]+weight[i];
				q.push(make_pair(dis[idx][to[i]],to[i]));
			}
		}
	}
}
bool counted[6];
void dfs(int dep,int r,int fat)
{
	if(dep==5)
	{
		ans=(ans<r?ans:r);
		return;
	}
	int i;
	for(i=1;i<6;++i)
	{
		if(counted[i])
		{
			continue;
		}
		counted[i]=true;
//		printf("%d ",dis[i][fat]);
		if(dis[i][fat]!=1e9)
		{
			dfs(dep+1,r+dis[i][fat],i);
		}
		counted[i]=false;
	}
}
int main()
{
	int i,j,a,b,c,d,e,r,t,w;
	scanf("%d%d",&n,&m);
	for(i=0;i<n;++i)
	{
		head[i]=-1;
	}
	for(i=0;i<m;++i)
	{
		for(j=0;j<6;++j)
		{
			dis[j][i]=1e9;
		}
	}
	scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
	a--;
	b--;
	c--;
	d--;
	e--;
	for(i=0;i<m;++i)
	{
		scanf("%d%d%d",&r,&t,&w);
		r--;
		t--;
		next[i*2]=head[r];
		to[i*2]=t;
		weight[i*2]=w;
		head[r]=i*2;
		
		next[i*2+1]=head[t];
		to[i*2+1]=r;
		weight[i*2+1]=w;
		head[t]=i*2+1;
	}
	spfa(0,0);
	for(i=0;i<n;++i)
	{
		printf("%d ",dis[0][i]);
	}
	spfa(a,1);
	spfa(b,2);
	spfa(c,3);
	spfa(d,4);
	spfa(e,5);
	counted[0]=true;
	dfs(0,0,0);
	printf("%d",ans);
	return 0;
}

记录

样例过了,数组加大试过,嘛用没有。RE好像是因为错误的内存引用(看评测信息)

2024/3/24 19:52
加载中...