题目传送点这里
题意:给n个车站,m条路径,s为终点
给w个起点,问起点至终点s的最短路径
注意:from station p to station q (单向边)
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int inf = 10000000;
const int maxv = 1010;
int n,m,s,G[maxv][maxv];
int d[maxv];
int p,q,t;
int w,x;
bool vis[maxv];
void Dijkstra(int s){
memset(vis,false,sizeof(vis));
fill(d,d+maxv,inf);//这里别用memset,用了很容易出错
d[s] = 0;//超级源点
for(int i = 0;i <= n; i++){
int u = -1,min = inf;
for(int j = 0;j <= n; j++){
if(!vis[j]&&d[j]<min){
u = j;
min = d[j];
}
}
if(u==-1) return;
vis[u] = true;
for(int v = 0;v <= n; v++){
if(!vis[v]&&d[u]+G[u][v]<d[v]){
d[v] = d[u]+G[u][v];
}
}
}
}
int main(){
while(scanf("%d%d%d",&n,&m,&s)!=EOF){
fill(G[0],G[0]+maxv*maxv,inf);
while(m--){
scanf("%d%d%d",&p,&q,&t);
if(t<G[p][q]){
G[p][q] = t;
}
}
scanf("%d",&w);
while(w--){
scanf("%d",&x);
G[0][x] = 0;//这里超级源点就是0,也就是让0到各起点的距离为0,
} //然后求0到终点的最短距离即可
Dijkstra(0);
if(d[s]==inf){
printf("-1\n");
}else{
printf("%d\n",d[s]);
}
}
return 0;
}