POJ2387 Til the Cows Come Home
2016-08-12 16:40
363 查看
题目链接:http://poj.org/problem?id=2387
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.
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.
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
Description
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 NLines 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 51 2 20
2 3 30
3 4 20
4 5 20
1 5 100
Sample Output
90Hint
INPUT DETAILS:There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
题目大意
给出N个点和T条路,输出1到n的最短路。解题思路
Dijkstra算法模板题。代码
#include <set> #include <map> #include <stack> #include <cmath> #include <queue> #include <cstdio> #include <bitset> #include <string> #include <vector> #include <iomanip> #include <cstring> #include <iostream> #include <algorithm> #include <functional> #define eps 1e-8 #define PI acos(-1.0) #define inf 0x3f3f3f3f #define file() freopen("D:/1.txt", "r", stdin); \ freopen("D:/2.txt", "w", stdout) #define debug(x) cout<<"--- "<<x<<" ---"<<endl typedef long long ll; using namespace std; const int NV = 1005; const int NE = 1005 * 2; int dis[NV]; int pre[NV], vis[NV], head[NV], ecnt; struct Edge { int v, next, l; } E[NE]; void AddEdge(int u, int v, int l) { E[++ecnt].v = v; E[ecnt].l = l; E[ecnt].next = head[u]; head[u] = ecnt; } void init(int n, int m, int s) { ecnt = 0; memset(pre, 0, sizeof(pre)); memset(vis, 0, sizeof(vis)); memset(head, -1, sizeof(head)); for (int i = 0; i <= n; i++) { dis[i] = inf; } dis[s] = 0; for (int i = 1; i <= m; i++) { int u, v, l; scanf("%d%d%d", &u, &v, &l); AddEdge(u, v, l); AddEdge(v, u, l); } } struct Point { int u, l; Point(int u, int l): u(u), l(l) { } bool operator < (const Point &p) const { return l > p.l; } }; void dijkstra(int s) { priority_queue<Point> q; q.push(Point(s, 0)); while (!q.empty()) { Point p = q.top(); q.pop(); int u = p.u; if (vis[u]) continue; vis[u] = 1; for (int i = head[u]; i != -1; i = E[i].next) { if (!vis[E[i].v] && p.l + E[i].l < dis[E[i].v]) { dis[E[i].v] = dis[u] + E[i].l; pre[E[i].v] = u; q.push(Point(E[i].v, dis[E[i].v])); } } } } int main() { int n, m; while (~scanf("%d%d", &m, &n)) { init(n, m, 1); dijkstra(1); printf("%d\n", dis ); } return 0; }
相关文章推荐
- POJ2387-Til the Cows Come Home
- poj2387- Til the Cows Come Home
- POJ2387 Til the Cows Come Home
- POJ2387 Til the Cows Come Home
- poj2387 Til the Cows Come Home
- poj2387 Til the Cows Come Home
- POJ2387--Til the Cows Come Home--Dijkstra算法裸题
- Poj2387- Til the Cows Come Home
- POJ2387---Til the Cows Come Home
- poj2387 Til the Cows Come Home
- POJ2387 Til the Cows Come Home
- POJ2387 Til the Cows Come Home
- poj2387- Til the Cows Come Home
- POJ2387 Til the Cows Come Home
- [poj2387] Til the Cows Come Home
- POJ2387-Til the Cows Come Home-最短路
- poj Til the Cows Come Home(dijkstra,bellman_ford,spfa)
- poj 2387 Til the Cows Come Home
- POJ-2387 Til the Cows Come Home
- 【图论--Dijkstra】POJ 2387 Til the Cows Come Home