标签:
思路:枚举所有起点城市然后比较每个起点所去喜欢城市的最小距离
1 #include<cstdio> 2 #include<cmath> 3 #include<cstring> 4 const int qq=1000+10; 5 const int MAX=1e7; 6 int s[qq],e[qq]; 7 int dist[qq]; 8 int vis[qq],tar[qq]; 9 int city[qq][qq]; 10 int t,a,b; 11 void Dijkstra() 12 { 13 int poor=MAX; 14 for(int l=1;l<=a;++l){ 15 int tot=MAX; 16 for(int i=1;i<qq;++i){ 17 vis[i]=0; 18 dist[i]=city[s[l]][i]; 19 } 20 vis[s[l]]=1; 21 for(int i=1;i<qq;++i){ 22 int minx=MAX; 23 int u=s[l]; 24 for(int j=1;j<qq;++j) 25 if(!vis[j] && dist[j] < minx) 26 minx=dist[u=j]; 27 if(minx==MAX) break; 28 vis[u]=1; 29 if(tar[u]!=0){ 30 tot=dist[u]; 31 break; 32 } 33 for(int j=1;j<qq;++j) 34 if(!vis[j] && city[u][j] < MAX) 35 if(dist[u]+city[u][j]<dist[j]) 36 dist[j] = dist[u] + city[u][j]; 37 } 38 if(tot<poor) 39 poor=tot; 40 } 41 printf("%d\n",poor); 42 } 43 int main() 44 { 45 while(scanf("%d %d %d",&t,&a,&b)!=EOF){ 46 for(int j,i=1;i<qq;++i) 47 for(j=1;j<qq;++j) 48 city[i][j]=MAX; 49 for(int i=1;i<=t;++i){ 50 int x,y,cost; 51 scanf("%d %d %d",&x,&y,&cost); 52 if(cost<city[x][y]) 53 city[x][y]=city[y][x]=cost; 54 } 55 for(int i=1;i<=a;++i) 56 scanf("%d",&s[i]); 57 memset(tar,0,sizeof(tar)); 58 for(int i=1;i<=b;++i){ 59 int ans;scanf("%d",&ans); 60 tar[ans]=1; 61 } 62 Dijkstra(); 63 } 64 }
标签:
原文地址:http://www.cnblogs.com/sasuke-/p/5225841.html