Description
One day , Kiki wants to visit one of her friends. As she is liable to carsickness , she wants to arrive at her friend’s home as soon as possible . Now give you a map of the city’s traffic route, and the stations which are near Kiki’s home so that she can take. You may suppose Kiki can change the bus at any station. Please find out the least time Kiki needs to spend. To make it easy, if the city have n bus stations ,the stations will been expressed as an integer 1,2,3…n.
Input
There are several test cases.
Each case begins with three integers n, m and s,(n<1000,m<20000,1=
#include<iostream>
#include<stdio.h>
#include<queue>
#include<stack>
#include<algorithm>
#include<string.h>
#include<string>
#include<math.h>
using namespace std;
int maze[1005][1005];
int dis[1005];
int vis[1005];
#define INF 0x7f7f7f
int main()
{
int n,m,s;
while(scanf("%d%d%d",&n,&m,&s)!=EOF)
{
memset(maze,INF,sizeof(maze));
memset(vis,false,sizeof(vis));
int st,en,len;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&st,&en,&len);
if(maze[st][en]>len)
{
maze[st][en]=len;
}
}
int w;
scanf("%d",&w);
for(int i=1;i<=w;i++)
{
scanf("%d",&st);
maze[0][st]=0;
}
for(int i=1;i<=n;i++)
dis[i]=INF;
dis[0]=0;
for(int i=0;i<=n;i++)
{
int x,m=INF;
for(int y=0;y<=n;y++)
{
if(!vis[y]&&m>=dis[y])
{
m=dis[y];
x=y;
}
}
vis[x]=true;
for(int y=0;y<=n;y++)
{
if(dis[y]>dis[x]+maze[x][y])
{
dis[y]=dis[x]+maze[x][y];
}
}
}
if(dis[s]==INF)
printf("-1\n");
else
printf("%d\n",dis[s]);
}
return 0;
}