Til the Cows Come Home
来源:互联网 发布:淘宝体检中心进不去 编辑:程序博客网 时间:2024/06/10 01:18
最短路径的一道题,因为路的长度只可能是正整数,所以用dijkstra算法
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.
* 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.
* Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
5 51 2 202 3 303 4 204 5 201 5 100
90
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.
#include<stdio.h>#include<queue>#include<vector>#include<algorithm>using namespace std;const int maxn=1005;const int inf=0x3f3f3f3f; void dijkstra(int s);struct list{int des;int weight;list(){};list(int a,int b){des=a;weight=b;}friend bool operator <(struct list c,struct list d){if(c.weight==d.weight)return c.des<d.des;elsereturn c.weight>d.weight;}};vector<struct list> an[maxn];int dis[maxn],n;bool book[maxn];int main(){int t;scanf("%d%d",&t,&n);while(t--){int a,b,c;scanf("%d%d%d",&a,&b,&c);an[a].push_back(list(b,c));an[b].push_back(list(a,c));}dijkstra(n);printf("%d",dis[1]);return 0;}void dijkstra(int s){fill(dis,dis+n+1,inf);priority_queue<struct list> q;dis[s]=0;book[s]=true;q.push(list(s,dis[s]));while(!q.empty()){struct list Q;Q=q.top();q.pop();for(int i=0;i<an[Q.des].size();i++){struct list p;p=an[Q.des][i];if(dis[p.des]>dis[Q.des]+p.weight)dis[p.des]=dis[Q.des]+p.weight;if(book[p.des]==false){book[p.des]=true;q.push(list(p.des,dis[p.des]));}}}}
阅读全文
0 0
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- PKU2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- Linux 下服务自启动的设置
- java单例模式的七种写法
- [HDU]6053 TrickGCD
- NOIP2017模拟赛(十四)总结
- ACM题集以及各种总结大全!
- Til the Cows Come Home
- Password
- 组合数求模
- OPNET LINK : fatal error LNK1181如何解决
- Fox And Two Dots
- hdu 2642 (summerIII J) 二维树状数组
- 【GRE】做题记录和总结
- 楼房重建
- PRML:二元变量分布