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. Anaconda 安装教程(Win10环境) Tensorflow安装

    序 Python易用,但用好却不易,其中比较头疼的就是包管理和Python不同版本的问题,特别是当你使用Windows的时候.为了解决这些问题,有不少发行版的Python,比如WinPython.An ...

  2. re.findall(?: ) ?:取消优先获取组的权限

  3. js语法和数据类型

    基础知识(Basics) JavaScript 的很多语法借鉴自 Java,但也受 Awk,Perl 和 Python 影响. JavaScript 是大小写敏感的,使用 Unicode 字符集. 在 ...

  4. java 中的三种引用,强引用,软引用,弱引用

    StrongReference   前引用,不会被系统GC回收,系统宁愿跑出OOM异常也不会回收强引用 SoftReference  软引用,在系统内存不足的时候,会被GC回收 WeakReferen ...

  5. PLSQL语法

    Procedural Language和SQL的结合体.通过增加变量.控制语句,使我们可以写些逻辑更加复杂的数据库操作 语句框架组成 declare – 可选 声明各种变量或游标的地方. begin ...

  6. C++标准库vector以及迭代器

    今天看C++的书,出现了一个新的概念,容器vector以及容器迭代器. vector是同一种对象的集合,每个对象都有一个对应的整数索引值.和string对象一样,标准库将负责管理与存储元素相关的类存. ...

  7. c# 调用系统默认图片浏览器打开图片

    private void OpenImage(string fileName) { try { Process.Start(fileName); } catch (Exception ex) { // ...

  8. WOJ 7 智商

    感觉Dasin去年的毒瘤题质量都挺好的,果然还是我太菜了. 以下假设划横线部分都相等,字符$c$代表一个小写字母. 分类讨论: $#1$ 先考虑$n == m$的情况 : $#1.1 :$ A:   ...

  9. php 函数追踪扩展 phptrace

    php 函数追踪扩展 phptrace 介绍 phptrace 是一个低开销的用于跟踪.分析 php 运行情况的工具. 它可以跟踪 php 在运行时的函数调用.请求信息.执行流程.并且提供有过滤器.统 ...

  10. 基于PhpStorm对Yii框架进行的单元测试一【PhpUnit环境搭建】

    1.下载phpunit.phar 2.在phpstorm中配置phpunit库 3.不同版本phpunit 需要依赖的php解释器也不一样,如果运行时报错 可以适当调整php解释器的版本 至此进行ph ...