Til the Cows Come Home
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.
Input
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.
* 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.
Source
题目类型:Dijkstra算法
算法分析:直接使用Dijkstra求解即可,注意输入数据可能出现重边的情况,此时贪心的将最小边权保留!!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; const int INF = 0x7FFFFFFF; const int MOD = 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1000 + 66; bool vis[maxn];//表示节点的最短路是否已经得到 //分别表示邻接矩阵、节点最短路的长度和顶点的个数 int edge[maxn][maxn], dis[maxn], n; void Init () { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { if (i == j) edge[i][j] = 0; else edge[i][j] = INF; } } void Dijkstra (int s) { memset (vis, false, sizeof (vis));; vis[s] = true; for (int i = 1; i <= n; i++)//初始化源点相邻接的节点的最短距离 dis[i] = edge[s][i]; dis[s] = 0;//源点到自己的最短路是0,十分重要!!! for (int i = 2; i <= n; i++) { int minval = INF, minpos = s; for (int j = 1; j <= n; j++)//遍历将还未确定最短路的节点中具有最小距离的的节点找到 if (j != s && !vis[j] && minval > dis[j]) { minval = dis[j]; minpos = j; } vis[minpos] = true;//访问该节点 for (int j = 1; j <= n; j++)//更新由于minpos节点确定最短路之后导致的最小距离变化 if (j != s && !vis[j] && edge[minpos][j] < INF && edge[minpos][j] + dis[minpos] < dis[j]) dis[j] = edge[minpos][j] + dis[minpos]; } } int main() { // ifstream cin ("aaa.txt"); int t; while (cin >> t >> n) { Init (); for (int i = 1; i <= t; i++) { int u, v, w; cin >> u >> v >> w; if (edge[u][v] > w) edge[u][v] = edge[v][u] = w; } Dijkstra (n); cout << dis[1] << endl; } return 0; } |
- « 上一篇:poj2352
- poj2406:下一篇 »