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. java GC是在什么时候,对什么东西,做了什么事情?

    1.新生代有一个Eden区和两个survivor区,首先将对象放入Eden区,如果空间不足就向其中的一个survivor区上放,如果仍然放不下就会引发一次发生在新生代的minor GC,将存活的对象放 ...

  2. python的面向对象编程

    面向对象编程是一种程序的范式,它把程序看成是对不同对象的相互调用,对现实世界建立的一种模型. 面向对象编程的基本思想,类和实例.类用于定义抽象对象,实例根据类的定义被创建出来. 在python当中我们 ...

  3. python+requests+excel 接口测试

    1.EXCEL文件接口保存方式,如图. 2.然后就是读取EXCEL文件中的数据方法,如下: import xlrd class readExcel(object): def __init__(self ...

  4. 使用自定义线程池优化EchoServer

    在上一篇文章中http://www.cnblogs.com/gosaint/p/8494423.html 我自定义了线程池ThreadPool.现在在我的EchoServer中使用自定义线程池去负责和 ...

  5. 项目一:第十二天 1、常见权限控制方式 2、基于shiro提供url拦截方式验证权限 3、在realm中授权 5、总结验证权限方式(四种) 6、用户注销7、基于treegrid实现菜单展示

    1 课程计划 1. 常见权限控制方式 2. 基于shiro提供url拦截方式验证权限 3. 在realm中授权 4. 基于shiro提供注解方式验证权限 5. 总结验证权限方式(四种) 6. 用户注销 ...

  6. PS中图层混合模式的计算方法

    https://zhuanlan.zhihu.com/p/23905865 长久以来一直用中文版本的PS,对于软件中的一些专业名字都是顾名思义,容易误入歧途,但当你真正看到英文版本的名字的时候才有豁然 ...

  7. Linux tee命令

    一.简介 tee以标准输入作为输入,标准输出和文件作为输出.   二.语法 Usage: tee [OPTION]... [FILE]... Copy standard input to each F ...

  8. Luogu 3934 Nephren Ruq Insania

    和Ynoi2016 炸脖龙重题了. BZOJ 5394. 首先是扩展欧拉定理: 一开始傻掉了……递归的层数和区间长度无关……也就是说我们每一次直接暴力递归求解子问题一定不会超过$logP$层,因为当模 ...

  9. Javascript的对象分类

    返回索引 按W3CSchool分类 1.JS内置对象 在W3CShool中对应JavaScript对象  参考

  10. meta标签使用

    META标签分两大部分:HTTP标题信息(HTTP-EQUIV)和页面描述信息(NAME). ★HTTP-EQUIV HTTP-EQUIV类似于HTTP的头部协议,它回应给浏览器一些有用的信息,以帮助 ...