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. DAY17-Django之model增删改

    添加表记录 普通字段 #方式1 publish_obj=Publish(name="人民出版社",city="北京",email="renMin@16 ...

  2. js在浏览器下的区别小结(部分)

    1.初始化数组: document.write([1,2,3,].length); IE:4//把数组中最后一个逗号后面的当做了undefined元素 FF.Opera.Safari:3 2.join ...

  3. 数据从HDFS-->HIVE-->HBASE 执行过程

    1.数据已经load进去hdfs 2.hive.hbase已经安装成功(我用的是hadoop 2.4 hbase 0.98.12 hive 1.2.1) 3.开始! 4.在hive建立表同时生成对应的 ...

  4. Python之路:面向对象及相关

    其他相关 一.isinstance(obj, cls) 检查是否obj是否是类 cls 的对象 class Foo(object):     pass   obj = Foo()   isinstan ...

  5. linux设置自动获取IP地址

    右键单击,选择设置 勾选桥接模式

  6. solr的查询语法、查询参数、检索运算符

    转载自:http://martin3000.iteye.com/blog/1328931 1.查询语法 solr的一些查询语法 1.1. 首先假设我的数据里fields有:name, tel, add ...

  7. ES6中的Set与Map数据结构

    本文实例讲述了ES6学习笔记之Set和Map数据结构.分享给大家供大家参考,具体如下: 一.Set ES6提供了新的数据结构Set.类似于数组,只不过其成员值都是唯一的,没有重复的值. Set本身是一 ...

  8. MarkdownPad 2 安装和破解

    MarkdownPad 2 安装和破解 下载:http://markdownpad.com/ 下载下面这个: 破解:http://w3cboy.com/post/2014/10/MarkdownPad ...

  9. tr td th是什么的缩写

    tr是 table row 表格的行 td是table data th是table heading表格标题 ,一般表格第一行的数据都是table heading

  10. 1、awk打开多个文件的方法

    转载:http://www.cnblogs.com/Berryxiong/p/6209324.html 1.当awk读取的文件只有两个的时候,比较常用的有三种方法(1)awk 'NR==FNR{... ...