【SPFA】60分求助
查看原帖
【SPFA】60分求助
481330
sunyizhe还是MC大佬楼主2023/3/11 16:21
//程序算法:SPFA最短路,链式前向星 
#include <bits/stdc++.h>
using namespace std;
const int N=510,P=810,M=1460;

int cow[N];
long long dist[P];
int c,n,m;
int h[M],e[M],ne[M],w[M],idx;
bool st[P];

void add(int a,int b,int c)
{
	idx++;
	e[idx]=b,ne[idx]=h[a],w[idx]=c,h[a]=idx;
}

void spfa(int S) 
{
	memset(dist,0x3f,sizeof(dist));
	memset(st,false,sizeof(st));
	queue<int> q;
	q.push(S);
	st[S]=true;
	dist[S]=0;
	
	while(!q.empty())
	{
		int t=q.front();
		q.pop();
		st[t]=false;
		for(int i=h[t];i;i=ne[i])
		{
			int j=e[i];
			if(dist[j]>dist[t]+w[i])
			{
				dist[j]=dist[t]+w[i];
				if(!st[j])
				{
					q.push(j);
					st[j]=true;
				}
			}
		}
	}
}
int main()
{
	scanf("%d %d %d",&c,&n,&m);
	
	for(int i=1;i<=c;i++)scanf("%d",&cow[i]);
	while(m--)
	{
		int a,b,c;
		scanf("%d %d %d",&a,&b,&c);
		add(a,b,c);
		add(b,a,c);
	}
	
	long long ans=0x3f3f3f3f3f3f3f3f;
	for(int i=1;i<=n;i++)//枚举黄油放置地点 
	{
		long long tot=0;
		spfa(i);
		for(int j=1;j<=c;j++)
		    tot+=1LL*dist[cow[j]];
		ans=min(ans,tot);
	}
	
	printf("%lld\n",ans);
	return 0;
}
/*
  	3 4 5
	2
	3
	4
	1 2 1
	1 3 5
	2 3 7
	2 4 3
	3 4 5
*/
2023/3/11 16:21
加载中...