Til the Cows Come Home
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 67405 | Accepted: 22659 |
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.
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.
* 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.
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
Source
题目意思是有一头母牛要从点 n 回到点 1,让求最短路径,这一道题可以用dijkstra算法求出。相当于一个模板题了
第一行 t 和 n,下面是t行代表有t条无向的路径和权值
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef pair<int,int> P;
const int MAXE=1e4+3;
const int MAXV=1e4+3;
struct edge{
int to,cost;
edge(){ }
edge(int _to,int _cost)
{
to=_to;
cost=_cost;
}
};
vector<edge> G[MAXE];
int d[MAXE];
int dijkstra(int s)
{
memset(d,0x3f,sizeof(d));
priority_queue<P,vector<P>,greater<P> > que;
d[s]=0;
que.push(P(0,s));
while(que.size())
{
P p=que.top();que.pop();
int v=p.second;// v 是需下一个到的点,p.first是从起点到现在所在的点的最小花费
if(d[v]<p.first) continue;
for(int i=0;i<G[v].size();i++)//从v点开始广搜哦,搜索距离v点只需要一条路径就能到达的点
{
edge e=G[v][i];
if(d[e.to]>d[v]+e.cost)
{
d[e.to]=d[v]+e.cost;
que.push(P(d[e.to],e.to));
}
}
}
return d[1];
}
int main()
{
int t,n;
while(~scanf("%d %d",&t,&n))
{
for(int i=0;i<t;i++)
{
int x,y,z;
scanf("%d %d %d",&x,&y,&z);
G[x].push_back(edge(y,z));
G[y].push_back(edge(x,z));
}
printf("%d\n",dijkstra(n));
}
return 0;
}
本文介绍了一道经典的最短路径问题,通过Dijkstra算法解决一头名为Bessie的母牛从草地返回牧场的问题。文章提供了完整的代码实现,并详细解释了如何使用Dijkstra算法找到从点N到点1的最短路径。
359

被折叠的 条评论
为什么被折叠?



