#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 100100
#define M 1000010
using namespace std;
int n,m,k,cnt;
int vip[N];
int head[N];
int v[N];
int val[N];
int dis[N];
int sec_dis[N];
int pre[N];
struct node
{
int from,to,val,next;
}edge[M<<1];
void init()
{
memset(dis,0x3f,sizeof(dis));
memset(sec_dis,0x3f,sizeof(sec_dis));
memset(head,-1,sizeof(head));
cnt=1;
}
void edgeadd(int from,int to,int val)
{
edge[cnt].from=from,edge[cnt].to=to,edge[cnt].val=val;
edge[cnt].next=head[from];
head[from]=cnt++;
}
void bfs()
{
queue<int>q;
for(int i=1;i<=k;i++)
{
int x;
scanf("%d",&x);x++;
dis[x]=sec_dis[x]=0;
q.push(x);
v[x]=1;
}
while(!q.empty())
{
int u=q.front();
q.pop();
v[u]=0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int to=edge[i].to;
if(sec_dis[u]+edge[i].val<=dis[to])
{
if(pre[to]!=u)
sec_dis[to]=dis[to];
dis[to]=sec_dis[u]+edge[i].val;
pre[to]=u;
if(!v[to])
{
q.push(to);
v[to]=1;
}
}else if(sec_dis[u]+edge[i].val<sec_dis[to])
{
sec_dis[to]=sec_dis[u]+edge[i].val;
if(!v[to])
{
q.push(to);
v[to]=1;
}
}
}
}
}
int main()
{
init();
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
x++,y++;
edgeadd(x,y,z);
edgeadd(y,x,z);
}
bfs();
printf("%d\n",sec_dis[1]);
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
BZOJ 2622 [2012国家集训队测试]深入虎穴 SPFA
原文地址:http://blog.csdn.net/wzq_qwq/article/details/47997617