Description

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

dijkstra算法模板题;
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#define inf 0xfffffff
using namespace std;
int p[][],flag[],dis[],mmin,n,t;
int dijkstra()
{
memset(flag,,sizeof(flag));
for(int i=;i<=n;i++)
{
dis[i]=p[][i];
}
flag[]=;
int pre;
for(int i=;i<=n;i++)
{
mmin=inf;
for(int j=;j<=n;j++)
{
if(flag[j]==&&dis[j]<mmin)
{
mmin=dis[j];
pre=j;
}
}
if(mmin==inf)break;
flag[pre]=;
for(int j=;j<=n;j++)
{
if(flag[j]==&&dis[pre]+p[pre][j]<dis[j])
{
dis[j]=dis[pre]+p[pre][j];
}
}
}
}
int main()
{
int a,b,l;
while(scanf("%d%d",&t,&n)!=EOF)
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
p[i][j]=inf;
}
}
for(int i=;i<t;i++)
{
scanf("%d%d%d",&a,&b,&l);
if(l<p[a][b])
{
p[a][b]=p[b][a]=l;
}
}
dijkstra();
printf("%d\n",dis[n]);
}
return ;
}
 

poj2387 Til the Cows Come Home 最短路径dijkstra算法的更多相关文章

  1. POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)

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

  2. poj2387 Til the Cows Come Home(Dijkstra)

    题目链接 http://poj.org/problem?id=2387 题意 有n个路标,编号1~n,输入路标编号及路标之间相隔的距离,求从路标n到路标1的最短路径(由于是无向图,所以也就是求从路标1 ...

  3. POJ2387 Til the Cows Come Home 【Dijkstra】

    题目链接:http://poj.org/problem?id=2387 题目大意; 题意:给出两个整数T,N,然后输入一些点直接的距离,求N和1之间的最短距离.. 思路:dijkstra求单源最短路, ...

  4. POJ2387 Til the Cows Come Home (最短路 dijkstra)

    AC代码 POJ2387 Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to ...

  5. 网络最短路径Dijkstra算法

    最近在学习算法,看到有人写过的这样一个算法,我决定摘抄过来作为我的学习笔记: <span style="font-size:18px;">/* * File: shor ...

  6. 单源最短路径Dijkstra算法,多源最短路径Floyd算法

    1.单源最短路径 (1)无权图的单源最短路径 /*无权单源最短路径*/ void UnWeighted(LGraph Graph, Vertex S) { std::queue<Vertex&g ...

  7. 最短路径-Dijkstra算法与Floyd算法

    一.最短路径 ①在非网图中,最短路径是指两顶点之间经历的边数最少的路径. AE:1    ADE:2   ADCE:3   ABCE:3 ②在网图中,最短路径是指两顶点之间经历的边上权值之和最短的路径 ...

  8. 数据结构实验之图论七:驴友计划 ( 最短路径 Dijkstra 算法 )

    数据结构实验之图论七:驴友计划 Time Limit: 1000 ms           Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...

  9. 最短路径——Dijkstra算法以及二叉堆优化(含证明)

    一般最短路径算法习惯性的分为两种:单源最短路径算法和全顶点之间最短路径.前者是计算出从一个点出发,到达所有其余可到达顶点的距离.后者是计算出图中所有点之间的路径距离. 单源最短路径 Dijkstra算 ...

随机推荐

  1. PHP学习笔记:利用时间和mt_rand函数获取随机名字

    这个知识会在文件上传等场合用到,还没学面向对象,现在用函数形式呈献给各位,代码都做了备注,有不懂得可以在线提问. <?php /** * Created by PhpStorm. * User: ...

  2. Javascript中void操作符

    Javascript中void是一个操作符,该操作符指定要计算一个表达式但是不返回值. void操作符用法格式如下:1.javascript:void (expression)2.javascript ...

  3. linux怎么模糊查找一个文件

    linux如何模糊查找一个文件 在当前目录下搜索指定文件: find . -name test.txt 在当前目录下模糊搜索文件: find . -name '*.txt' 在当前目录下搜索特定属性的 ...

  4. Python for循环内部实现的一个sample

    #!/usr/bin/env python # -*- coding: utf-8 -*- it = iter([1,2,3,4,5]) while True: try: x = next(it) p ...

  5. Android Studio使用教程(二)

    以下是本次Google I/O大会发布的IDE Android Studio使用教程第二篇: 在Android Studio使用教程(一)中简要介绍了Android Studio的基本使用,包括安装. ...

  6. HTML · 图片热点,网页划区,拼接,表单

    图片热点: 规划出图片上的一个区域,可以做出超链接,直接点击图片区域就可以完成跳转的效果. 网页划区: 在一个网页里,规划出一个区域用来展示另一个网页的内容. 网页的拼接: 在一个网络页面内,规划出多 ...

  7. jQuery高级技巧——DOM操作篇

      页面加载之DOMReady事件 所谓domReady,也就是文档就绪,我们都知道,在操作dom时必须要在dom树加载完成后才能进行操作.如何检测DOM树已经构建完成,以下是一些实现的方式: 1.使 ...

  8. android Android-PullToRefresh 下拉刷新

    1.github下载地址 原作者:  https://github.com/chrisbanes/Android-PullToRefresh 我自己的:  https://github.com/zyj ...

  9. Python: 解决pip安装源被墙的问题

    pip install <package> -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.c ...

  10. UnityShader之固定管线Fixed Function Shader【Shader资料3】

    Fixed function shader简介:  属于固定渲染管线 Shader, 基本用于高级Shader在老显卡无法显示时的情况.使用的是ShaderLab语言,语法与微软的FX files 或 ...