把终点当起点,起点当终点,存边的时候反着存就可以了。
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2680
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int MAXN = 1010;
const int INF = 0x3f3f3f3f;
bool vis[MAXN];
int pre[MAXN];
int cost[MAXN][MAXN];
int lowcost[MAXN];
void Init(){
memset(vis,0,sizeof(vis));
memset(pre,-1,sizeof(pre));
memset(cost,0x3f,sizeof(cost));
memset(lowcost,0x3f,sizeof(lowcost));
}
void Dijkstra(int n,int beg){
lowcost[beg] = 0;
for(int i=0;i<n;i++){
int k = -1;
int Min = INF;
for(int j=0;j<n;j++){
if(!vis[j]&&lowcost[j]<Min){
Min = lowcost[j];
k = j;
}
}
if(k==-1)break;
vis[k] = true;
for(int j=0;j<n;j++){
if(!vis[j]&&lowcost[j]>lowcost[k]+cost[k][j]){
lowcost[j] = lowcost[k] + cost[k][j];
pre[j] = k;
}
}
}
}
void Input(){
int n,m,s;
while(~scanf("%d %d %d",&n,&m,&s)){
Init();
int tempa,tempb,tempr;
for(int i=0;i<m;i++){
scanf("%d %d %d",&tempa,&tempb,&tempr);
tempa --; tempb --;
if(cost[tempb][tempa]>tempr){
cost[tempb][tempa] = tempr;
}
}
Dijkstra(n,s-1);
int w;
int ans = INF;
scanf("%d",&w);
for(int i=0;i<w;i++){
scanf("%d",&tempa);
tempa --;
if(lowcost[tempa]<ans)
ans = lowcost[tempa];
}
if(ans==INF)
puts("-1");
else
printf("%d\n",ans);
}
}
void File(){
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
}
int main(void){
// File();
Input();
return 0;
}