Description
Input
Output
Sample Input
6 xiasha westlake xiasha station 60 xiasha ShoppingCenterofHangZhou 30 station westlake 20 ShoppingCenterofHangZhou supermarket 10 xiasha supermarket 50 supermarket westlake 10 -1
Sample Output
50
解题思路:
题目唯一难点就是如何去表示地点,用map映射可以轻松搞定,一个地点名对应一个值,剩下的就是求最短路径。下面给出三个代码,第一个是用朴素Dijkstra写的,第二个用了Floyd,第三个用了Dijkstra + heap。感觉上运行效率差不多。。。。。
AC代码:
Dijkstra:
#include <iostream> #include <cstdio> #include <cstring> #include <map> using namespace std; const int maxn = 200, INF = 9999999; int route[maxn][maxn], dist[maxn]; int Dijkstra(int n,int start,int end) { bool visited[maxn]; int pos = start, min; for(int i = 1; i <= n; i++) { dist[i] = route[start][i]; visited[i] = 0; } dist[start] = 0; visited[start] = 1; for(int i = 1; i < n; i++) { min = INF; for(int j = 1; j <= n; j++) if((!visited[j]) && dist[j] < min) { pos = j; min = dist[pos]; } if(min == INF) break; visited[pos] = 1; for(int j = 1; j <= n; j++) if(!visited[j] && dist[pos] + route[pos][j] < dist[j]) { dist[j] = dist[pos] + route[pos][j]; } } return dist[end]; } int main() { int n, ans, len; map<string,int> station; char start[50], end[50], str_1[50], str_2[50]; while(scanf("%d", &n)) { if(n == -1) break; station.clear(); for(int i = 0; i < maxn; i++) for(int j = 0; j < maxn; j++) route[i][j] = INF; scanf("%s %s", start, end); int count = 1; station[start] = 1; if(!station[end]) station[end] = ++count; for(int i = 0; i < n; i++) { scanf("%s %s %d", str_1, str_2, &len); if(!station[str_1]) station[str_1] = ++count; if(!station[str_2]) station[str_2] = ++count; if(len < route[station[str_1]][station[str_2]]) route[station[str_1]][station[str_2]] = route[station[str_2]][station[str_1]] = len; } ans = Dijkstra(count,station[start], station[end]); if(ans == INF) printf("-1\n"); else printf("%d\n",ans); } return 0; }Floyd:
#include <iostream> #include <cstdio> #include <cstring> #include <map> using namespace std; const int maxn = 200, INF = 9999999; int dist[maxn][maxn]; int Floyd(int n,int start,int end) { for(int k = 1; k <= n; k++) for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) if(i != k && i != j && j != k) dist[i][j] = dist[i][j] < (dist[i][k] + dist[k][j]) ? dist[i][j] : (dist[i][k] + dist[k][j]); if(start == end) return 0; return dist[start][end]; } int main() { int n, ans, len; map<string,int> station; char start[50], end[50], str_1[50], str_2[50]; while(scanf("%d", &n)) { if(n == -1) break; station.clear(); for(int i = 0; i < maxn; i++) for(int j = 0; j < maxn; j++) dist[i][j] = INF; scanf("%s %s", start, end); int count = 1; station[start] = 1; if(!station[end]) station[end] = ++count; for(int i = 0; i < n; i++) { scanf("%s %s %d", str_1, str_2, &len); if(!station[str_1]) station[str_1] = ++count; if(!station[str_2]) station[str_2] = ++count; if(len < dist[station[str_1]][station[str_2]]) dist[station[str_1]][station[str_2]] = dist[station[str_2]][station[str_1]] = len; } ans = Floyd(count,station[start], station[end]); if(ans == INF) printf("-1\n"); else printf("%d\n",ans); } return 0; }Dijkstra + heap:
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <map> using namespace std; const int maxn = 200, INF = 9999999; int route[maxn][maxn], dist[maxn]; struct node { int pos, dist; friend bool operator < (node a, node b) { return a.dist > b.dist; } }; int Dijkstra(int n,int start,int end) { bool visited[maxn]; int pos; node tmp; tmp.pos = start; tmp.dist = 0; priority_queue<node> q; for(int i = 1; i <= n; i++) { visited[i] = 0; dist[i] = INF; } dist[start]= 0; q.push(tmp); while(!q.empty()) { tmp = q.top(); q.pop(); pos = tmp.pos; if(tmp.dist == INF) return INF; if(visited[pos]) continue; visited[pos] = 1; for(int i = 1; i <= n; i++) if(!visited[i] && tmp.dist + route[pos][i] < dist[i]) { dist[i] = tmp.dist + route[pos][i]; node t; t.pos = i; t.dist = dist[i]; q.push(t); } } return dist[end]; } int main() { int n, ans, len; map<string,int> station; char start[50], end[50], str_1[50], str_2[50]; while(scanf("%d", &n)) { if(n == -1) break; station.clear(); for(int i = 0; i < maxn; i++) for(int j = 0; j < maxn; j++) route[i][j] = INF; scanf("%s %s", start, end); int count = 1; station[start] = 1; if(!station[end]) station[end] = ++count; for(int i = 0; i < n; i++) { scanf("%s %s %d", str_1, str_2, &len); if(!station[str_1]) station[str_1] = ++count; if(!station[str_2]) station[str_2] = ++count; if(len < route[station[str_1]][station[str_2]]) route[station[str_1]][station[str_2]] = route[station[str_2]][station[str_1]] = len; } ans = Dijkstra(count,station[start], station[end]); if(ans == INF) printf("-1\n"); else printf("%d\n",ans); } return 0; }
HDU Today(三种写法)(最短路),布布扣,bubuko.com
原文:http://blog.csdn.net/userluoxuan/article/details/38173269