1. 程式人生 > >Til the Cows Come Home (最短路問題, 模板)

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 100

Sample Output

90

Hint

INPUT DETAILS:

There are five landmarks.

OUTPUT DETAILS:

Bessie can get home by following trails 4, 3, 2, and 1.

 

最短路模板:

題意:一個人從n位置到1位置,輸出最短路,其權值是多少?

分析:要充分理解最短路,圖的概念。當從1位置到1位置時,此時的權值為0。其他無法到達的位置可以設為無窮大 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;    //假設所有位置都無法到達 
		}
		val[i][i] = 0;         //在原地打轉,其權值為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;
}

記錄路徑:用一個數組來記錄路徑,每次記錄當前節點的前驅。然後反著輸出。

而此題的最後一個節點是1,因此還原的時候從最後一個節點開始。

 

#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;     //記錄每一個節點的前驅 
			} 
		}
	}
}

//路徑還原 
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;    //假設所有位置都無法到達 
		}
		val[i][i] = 0;         //在原地打轉,其權值為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;
}