题目:http://acm.hdu.edu.cn/showproblem.php?pid=2066
代码:
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int inf=1<<30; int T,S,D,n; int maps[1111][1111]; int vis[1111]; int cast[1111]; int s[1111],e[1111]; void Dijkstra() { int i,j,minn,pos; memset(vis,0,sizeof(vis)); vis[0]=1; for(i=0;i<=n;i++) cast[i]=maps[0][i]; for(i=1;i<=n;i++) { minn=inf; for(j=1;j<=n;j++) { if(cast[j]<minn&&!vis[j]) { pos=j; minn=cast[j]; } } vis[pos]=1; for(j=1;j<=n;j++) { if(cast[pos]+maps[pos][j]<cast[j]&&!vis[j]) cast[j]=cast[pos]+maps[pos][j]; } } } int main() { int i,j,x,y,z,start,end; while(~scanf("%d%d%d",&T,&S,&D)) { n = 0; for(i = 0; i<1111; i++) { for(j = 0; j<1111; j++) maps[i][j] = inf; maps[i][i] = 0; } while(T--) { scanf("%d%d%d",&x,&y,&z); n = max(max(n,x),y); if(z<maps[x][y]) maps[x][y] = maps[y][x] = z; } int minn = inf; for(i = 0; i<S; i++) { scanf("%d",&s[i]); maps[0][s[i]] = maps[s[i]][0] = 0; } for(i = 0; i<D; i++) scanf("%d",&e[i]); Dijkstra(); for(i = 0; i<D; i++) minn = min(minn,cast[e[i]]); printf("%d\n",minn); } }
在Dijk。智商-2.