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 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.

题解:这个基本是可以套用dijkstra算法,并且需要注意双向赋值,别的基本没什么坑点

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm> using namespace std; const int Inf = 0x3f3f3f3f ;
const int MAXN = 2005;
int dis[MAXN];
int map[MAXN][MAXN];//用来存储图
bool vis[MAXN];//用来标记,避免重复搜
int n,m ;//n个点,m条边
// u 为单源点
void dijkstra(int u)//dijkstra的算法
{
int t = u;
dis[t] = 0 ;
vis[t] = true ;
for ( int i = 1 ; i <= n ; i ++ )
{
for ( int j = 1 ; j <= n ; j ++ )
{
if ( !vis[j] && map[t][j] + dis[t] < dis[j] )//判断直接近,还是间接近
{
dis[j] = map[t][j] + dis[t] ;
}
}
int mini = Inf ;
for ( int j = 1 ; j <= n ; j ++ )
{
if ( !vis[j] && dis[j] < mini )
{
mini = dis[j] ;
t=j;
}
} vis[t] = true ;
}
} void init()
{
memset(vis,false,sizeof(vis)) ; //初始化标记数组
for ( int i = 1 ; i <= n ; i ++ )
{
dis[i] = Inf ;
for ( int j = 1 ; j <= n ; j ++ )
{
map[i][j] = Inf ;
}
}
return ;
} int main()
{
while (scanf("%d%d",&m,&n)!=EOF)
{
init();
memset(map,Inf,sizeof(map));//初始化图
for ( int i = 0 ; i < m ; i ++ )
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);//表示 u 到 v的距离为 w
if ( map[u][v] > w )
{
map[v][u] = map[u][v] = w ;
}
}
dijkstra(1);
cout<<dis[n]<< endl ;
}
return 0 ;
}

Til the Cows Come Home (dijkstra算法)的更多相关文章

  1. poj 2387 Til the Cows Come Home(dijkstra算法)

    题目链接:http://poj.org/problem?id=2387 题目大意:起点一定是1,终点给出,然后求出1到所给点的最短路径. 注意的是先输入边,在输入的顶点数,不要弄反哦~~~ #incl ...

  2. POJ 2387 Til the Cows Come Home Dijkstra求最短路径

    Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to get as much s ...

  3. POJ - Til the Cows Come Home(Dijkstra)

    题意: 有N个点,给出从a点到b点的距离,当然a和b是互相可以抵达的,问从1到n的最短距离 分析: 典型的模板题,但是一定要注意有重边,因此需要对输入数据加以判断,保存较短的边,这样才能正确使用模板. ...

  4. Poj 2387 Til the Cows Come Home(Dijkstra 最短路径)

    题目:从节点N到节点1的求最短路径. 分析:这道题陷阱比较多,首先是输入的数据,第一个是表示路径条数,第二个是表示节点数量,在 这里WA了四次.再有就是多重边,要取最小值.最后就是路径的长度的最大值不 ...

  5. POJ 2387 Til the Cows Come Home (Dijkstra)

    传送门:http://poj.org/problem?id=2387 题目大意: 给定无向图,要求输出从点n到点1的最短路径. 注意有重边,要取最小的. 水题..对于无向图,从1到n和n到1是一样的. ...

  6. poj2387 Til the Cows Come Home 最短路径dijkstra算法

    Description Bessie is out in the field and wants to get back to the barn to get as much sleep as pos ...

  7. Til the Cows Come Home(poj 2387 Dijkstra算法(单源最短路径))

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32824   Accepted: 11098 Description Bes ...

  8. 怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)

    Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33015   Accepted ...

  9. (原创)最短路径-Dijkstra算法,以Til the Cows Come Home为例

    (1)首先先解释一下单源最短路径: 1)容易的解释:指定一个点(源点)到其余各个顶点的最短路径,也叫做“单源最短路径” 2)官方解释:给定一个带权有向图G=(V,E),其中每条边的权是一个实数.另外, ...

随机推荐

  1. Stun方式的P2P实现原理(转)

    转帖地址:http://www.cppblog.com/peakflys/archive/2013/01/25/197562.html   二.STUN方式的P2P实现 STUN是RFC3489规定的 ...

  2. OpenCV4Android 不需要安装OpenCVManager,就可以运行的方法

    http://blog.csdn.net/yanzi1225627/article/details/27863615 OpenCV4Android释疑: 透析Android以JNI调OpenCV的三种 ...

  3. springmvc 处理器方法返回的是modelandview 重定向到页面

  4. java线程的三种实现方式

    线程实现的三种种方式: 一个是继承Thread类,实现run()方法: 一个是实现Runnable接口,实现run()方法: 一个是实现Callable接口,实现call()方法:该方式和实现Runn ...

  5. 使用git将代码传到github

    廖雪峰git教程:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 注:add加入 ...

  6. NLTK词性标注解释

    1.      CC      Coordinating conjunction 连接词2.     CD     Cardinal number  基数词3.     DT     Determin ...

  7. codeforces 1064D 双端队列BFS

    双端队列BFS解决的就是路径权值可能为0的图最短路问题,权值为0插入队头,否则插入队尾. 对于这个题,可以看作上下移动的路径的权值为0,左右移动权值为1,而且不能超过规定的步数. 直接广搜求覆盖的点的 ...

  8. vray学习笔记(2)vray工作流程

    在bilibili上面搜索到了一个vray的教程,虽然是英语的,细节方面可能听不太懂,但可以了解整个工作流程,工作流程太重要了,先看下视频的目录: 第1节到第9节都是建模的内容. 第10节和第13节是 ...

  9. 安装postman工具模拟请求

    扩展工具->: 注意,在创建快捷方式后,第一次打开这个工具,会让你注册信息,忽略它.关闭,重新打开postman工具,然后如下所示:

  10. ubuntu 15.04默认root用户登陆

    1:给root用户设置密码 sudo passwd root 2:修改/etc/lightdm/lightdm.conf [SeatDefaults]autologin-guest=falseauto ...