Til the Cows Come Home (最短路问题, 模板)

版权声明:wjwjwjwjwj https://blog.csdn.net/qq_41818544/article/details/83821241

题目: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;
}


 

没有更多推荐了,返回首页