题目意思就是中文意思;
有几点注意的:
1.输入的边是从1开始的,但是没有告诉具体点数,所以要统计;
2.第n+1-行给的是和小草家里相连的,所以意思就是从家--->车站--->想去的地方; 因此,,思路就是把家到车站权值变为0,然后从车站开始搜最短路,这两个处理可以用一次dijkstra
6 2 3 1 3 5 1 4 7 2 8 12 3 8 4 4 9 12 9 10 2 1 2 8 9 10
9
AC代码:
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int inf = 1<<30; int T,S,D,n; int map[1111][1111]; int vis[1111],dis[1111]; int s[1111],e[1111]; void Dijkstra() { int i,j,minn,u; memset(vis,0,sizeof(vis)); vis[0] = 1; //*草儿家为0点, for(i = 0; i<=n; i++) dis[i] = map[0][i]; for(i = 1; i<=n; i++)//*搜车站到地点的最短路; { minn = inf; for(j = 1; j<=n; j++) { if(dis[j]<minn && !vis[j]) { u = j; minn = dis[u]; } } vis[u] = 1; for(j = 1; j<=n; j++) { if(dis[u]+map[u][j]<dis[j] && !vis[j]) dis[j] = dis[u]+map[u][j]; } } } int main() { int i,j,x,y,z; while(~scanf("%d%d%d",&T,&S,&D)) { n = 0; for(i =0; i<1111; i++) { for(j = 0; j<1111; j++) map[i][j] = inf; map[i][i] = 0; } for( i =1 ; i<=T;i++ ) { scanf("%d%d%d",&x,&y,&z); n = max(max(n,x),y);//*找点数; if(z<map[x][y]) map[x][y] = map[y][x] = z; } int minn = inf; for(i = 0; i<S; i++) { scanf("%d",&s[i]); map[0][s[i]] = map[s[i]][0] = 0;//*相连的权值为0 } for(i = 0; i<D; i++)//*想去的地方; scanf("%d",&e[i]); Dijkstra(); for(i = 0; i<D; i++) minn = min(minn,dis[e[i]]); printf("%d\n",minn); } return 0; }