hdu-2066 一个人的旅行---模板题
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=2066
题目大意:
求到目标点集合中的最短距离
解题思路:
dijkstra算法求出每个点到该点的最短路径,直接求其中最短即可(重边)
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn = 1000 + 10; 5 const int INF = 0x3f3f3f3f; 6 int Map[maxn][maxn]; 7 int v[maxn], d[maxn]; 8 int a[maxn]; 9 int n, m, s, t; 10 void dijkstra(int s) 11 { 12 memset(v, 0, sizeof(v)); 13 for(int i = 0; i <= n; i++)d[i] = (i == s ? 0 : INF); 14 for(int i = 0; i <= n; i++) 15 { 16 int x = -1, m = INF; 17 for(int i = 0; i <= n; i++)if(!v[i] && d[i] <= m)m = d[x = i]; 18 if(x == -1)break; 19 v[x] = 1; 20 for(int i = 0; i <= n; i++) 21 { 22 if(d[i] > d[x] + Map[x][i]) 23 { 24 d[i] = d[x] + Map[x][i]; 25 } 26 } 27 } 28 int ans = INF; 29 for(int i = 0; i < t; i++) 30 { 31 ans = min(ans, d[a[i]]); 32 } 33 cout<<ans<<endl; 34 } 35 36 int main() 37 { 38 while(scanf("%d%d%d", &m, &s, &t) != EOF) 39 { 40 int u, v, w; 41 memset(Map, INF, sizeof(Map)); 42 while(m--) 43 { 44 scanf("%d%d%d", &u, &v, &w); 45 n = max(n, max(u, v)); 46 if(Map[u][v] > w) 47 { 48 Map[u][v] = Map[v][u] = w; 49 } 50 } 51 while(s--) 52 { 53 scanf("%d", &u); 54 Map[0][u] = Map[u][0] = 0; 55 } 56 for(int i = 0; i < t; i++)cin >> a[i]; 57 dijkstra(0); 58 } 59 }
越努力,越幸运