Til the Cows Come Home (最短路问题, 模板)
題目:https://vjudge.net/problem/POJ-2387?
Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.
Farmer John's field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.
Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.
Input
* Line 1: Two integers: T and N
* Lines 2..T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1..100.
Output
* Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
Sample Input
5 5 1 2 20 2 3 30 3 4 20 4 5 20 1 5 100Sample Output
90Hint
INPUT DETAILS:
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
?
最短路模板:
題意:一個(gè)人從n位置到1位置,輸出最短路,其權(quán)值是多少?
分析:要充分理解最短路,圖的概念。當(dāng)從1位置到1位置時(shí),此時(shí)的權(quán)值為0。其他無法到達(dá)的位置可以設(shè)為無窮大 INF
?
ac代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<string>using namespace std;const int maxn = 2*1e3+5; const int INF = 1e9+7;int val[maxn][maxn]; int d[maxn]; bool used[maxn]; int T, N;int pre[maxn];//dijkstra模板 void dijkstra(int s){fill(d, d+N+1, INF);fill(used, used+N+1, false);fill(pre, pre+N+1, -1);d[s] = 0;while(true){int v = -1;for(int u = 1; u <= N; u++){if(!used[u]&&(v == -1||d[u] < d[v])) v=u;}if(v == -1) break;used[v] = true;for(int u = 0; u <= N; u++){d[u] = min(d[u], d[v] + val[v][u]);}} }int main() {scanf("%d%d", &T, &N);for(int i = 0; i <= N; i++){for(int j = 0; j <= N; j++){val[i][j] = INF; //假設(shè)所有位置都無法到達(dá) }val[i][i] = 0; //在原地打轉(zhuǎn),其權(quán)值為0 }for(int i = 0; i < T; i++){int u, v, w;scanf("%d%d%d", &u, &v, &w);if(w < val[u][v]){val[u][v] = w; //該圖為無向圖 val[v][u] = w;} }dijkstra(N);printf("%d\n", d[1]);return 0; }記錄路徑:用一個(gè)數(shù)組來記錄路徑,每次記錄當(dāng)前節(jié)點(diǎn)的前驅(qū)。然后反著輸出。
而此題的最后一個(gè)節(jié)點(diǎn)是1,因此還原的時(shí)候從最后一個(gè)節(jié)點(diǎn)開始。
?
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<string> #include<vector>using namespace std;const int maxn = 2*1e3+5; const int INF = 1e9+7;int val[maxn][maxn]; int d[maxn]; bool used[maxn]; int T, N;int pre[maxn];//dijkstra模板 void dijkstra(int s){fill(d, d+N+1, INF);fill(used, used+N+1, false);fill(pre, pre+N+1, -1);d[s] = 0;while(true){int v = -1;for(int u = 1; u <= N; u++){if(!used[u]&&(v == -1||d[u] < d[v])) v = u;}if(v == -1) break;used[v] = true;for(int u = 0; u <= N; u++){if(d[u] > d[v] + val[v][u]){d[u] = d[v] + val[v][u];pre[u] = v; //記錄每一個(gè)節(jié)點(diǎn)的前驅(qū) } }} }//路徑還原 vector<int> get_path(int t){vector<int>path;for(; t != -1; t = pre[t]){path.push_back(t);} reverse(path.begin(), path.end());return path; }int main() {scanf("%d%d", &T, &N);for(int i = 0; i <= N; i++){for(int j = 0; j <= N; j++){val[i][j] = INF; //假設(shè)所有位置都無法到達(dá) }val[i][i] = 0; //在原地打轉(zhuǎn),其權(quán)值為0 }for(int i = 0; i < T; i++){int u, v, w;scanf("%d%d%d", &u, &v, &w);if(w < val[u][v]){val[u][v] = w; //該圖為無向圖 val[v][u] = w;} }dijkstra(N);printf("%d\n", d[1]);//路徑輸出 vector<int>path;path = get_path(1);for(int i = 0; i < path.size(); i++){printf("%d ", path[i]);} return 0; }?
總結(jié)
以上是生活随笔為你收集整理的Til the Cows Come Home (最短路问题, 模板)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 连通图的判断(并查集, DFS, BFS
- 下一篇: 转载文章,感觉真的很心酸