作者:xjoliemonicane_934 | 来源:互联网 | 2023-10-11 21:22
题目:https:vjudge.netproblemPOJ-2387Bessieisoutinthefieldandwantstogetbacktothebarnto
题目: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&#39;s field has N (2 <&#61; N <&#61; 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 <&#61; T <&#61; 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&#43;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.
最短路模板&#xff1a;
题意&#xff1a;一个人从n位置到1位置&#xff0c;输出最短路&#xff0c;其权值是多少&#xff1f;
分析&#xff1a;要充分理解最短路&#xff0c;图的概念。当从1位置到1位置时&#xff0c;此时的权值为0。其他无法到达的位置可以设为无穷大 INF
ac代码&#xff1a;
#include
#include
#include
#include
#includeusing namespace std;const int maxn &#61; 2*1e3&#43;5;
const int INF &#61; 1e9&#43;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&#43;N&#43;1, INF);fill(used, used&#43;N&#43;1, false);fill(pre, pre&#43;N&#43;1, -1);d[s] &#61; 0;while(true){int v &#61; -1;for(int u &#61; 1; u <&#61; N; u&#43;&#43;){if(!used[u]&&(v &#61;&#61; -1||d[u] }int main()
{scanf("%d%d", &T, &N);for(int i &#61; 0; i <&#61; N; i&#43;&#43;){for(int j &#61; 0; j <&#61; N; j&#43;&#43;){val[i][j] &#61; INF; //假设所有位置都无法到达 }val[i][i] &#61; 0; //在原地打转&#xff0c;其权值为0 }for(int i &#61; 0; i }
记录路径&#xff1a;用一个数组来记录路径&#xff0c;每次记录当前节点的前驱。然后反着输出。
而此题的最后一个节点是1&#xff0c;因此还原的时候从最后一个节点开始。
#include
#include
#include
#include
#include
#includeusing namespace std;const int maxn &#61; 2*1e3&#43;5;
const int INF &#61; 1e9&#43;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&#43;N&#43;1, INF);fill(used, used&#43;N&#43;1, false);fill(pre, pre&#43;N&#43;1, -1);d[s] &#61; 0;while(true){int v &#61; -1;for(int u &#61; 1; u <&#61; N; u&#43;&#43;){if(!used[u]&&(v &#61;&#61; -1||d[u] d[v] &#43; val[v][u]){d[u] &#61; d[v] &#43; val[v][u];pre[u] &#61; v; //记录每一个节点的前驱 } }}
}//路径还原
vector get_path(int t){vectorpath;for(; t !&#61; -1; t &#61; pre[t]){path.push_back(t);} reverse(path.begin(), path.end());return path;
}int main()
{scanf("%d%d", &T, &N);for(int i &#61; 0; i <&#61; N; i&#43;&#43;){for(int j &#61; 0; j <&#61; N; j&#43;&#43;){val[i][j] &#61; INF; //假设所有位置都无法到达 }val[i][i] &#61; 0; //在原地打转&#xff0c;其权值为0 }for(int i &#61; 0; i path;path &#61; get_path(1);for(int i &#61; 0; i }