[Vjudge]一个人的旅行
作者:互联网
题目描述如图
单源最短路裸题,只需要在所有节点之外建一个起点,让它指向草儿家附近的城市且路长为0,然后以这个点为起点跑一边最短路就可以了
代码
//By AcerMo
#include<queue>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define lli long long int
using namespace std;
const int M=1050;
int t,s,d;
int dis[M],vis[M];
int to[M*3],nxt[M*3],head[M*3],val[M*3],cnt;
inline void read(int &x)
{
x=0;char c=getchar();
while (!isdigit(c)) c=getchar();
while (isdigit(c)) x=x*10+c-'0',c=getchar();
return ;
}
inline void in_it()
{
cnt=0;
memset(dis,0x7f,sizeof(dis));
memset(head,0,sizeof(head));
memset(nxt,0,sizeof(nxt));
return ;
}
inline void add(int x,int y,int w)
{
to[++cnt]=y;nxt[cnt]=head[x];val[cnt]=w;head[x]=cnt;
to[++cnt]=x;nxt[cnt]=head[y];val[cnt]=w;head[y]=cnt;
return ;
}
inline void SPFA(int s)
{
queue<int>q;q.push(s);
vis[s]=1;dis[s]=0;
while (q.size())
{
int x=q.front();
q.pop();vis[x]=0;
for (int i=head[x];i;i=nxt[i])
{
if (dis[to[i]]>dis[x]+val[i])
{
dis[to[i]]=dis[x]+val[i];
if (vis[to[i]]==0) q.push(to[i]);
vis[to[i]]=1;
}
}
}
return ;
}
signed main()
{
while (scanf("%d %d %d",&t,&s,&d)!=EOF)
{
in_it();
for (int i=1;i<=t;i++)
{
int x;read(x);
int y;read(y);
int w;read(w);
add(x,y,w);
}
for (int i=1;i<=s;i++)
{
int x;read(x);
add(1001,x,0);
}
SPFA(1001);int ans=2147483647;
for (int i=1;i<=d;i++)
{
int x;read(x);
ans=min(ans,dis[x]);
}
cout<<ans<<endl;
}
return 0;
}
标签:旅行,cnt,一个,Vjudge,head,int,read,include,dis 来源: https://blog.csdn.net/ACerAndAKer/article/details/121261161