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

时间:2022-01-19 10:46:58

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&lpar;SPFA &plus; dijkstra &plus; BallemFord 模板&rpar;

    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 &lpar;最短路 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. android AES 加密

    import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;import jav ...

  2. Android-动画简介

    Android中动画分为3种: ween Animation:通过对场景里的对象不断做图像变换(平移.缩放.旋转)产生动画效果,即是一种渐变动画: 也称View动画:也叫渐变动画,针对View的动画, ...

  3. 关于当一个C&num;工程移植到另一台机子上(win7)上时,程序报错。dll没有被指定在Windows上运行,或者它包含错误。请尝试使用原始安装媒体重新安装程序。。。。。。

    , 解决方法:通过从网上重新下载dll文件 拷贝到报错的目录下,替换掉原有的dll,可以正确运行.

  4. 在一个Label上设置多种颜色字体

    #import "AppDelegate.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL ...

  5. Angular4&period;0&period;0发布总览文章

    翻译自angular.io上的关于4.0.0版本发布的文章,内容主要是介绍了4.0.0版本下的改进以及接下来还会有的其他更新,4.0.0其实已经出来好多天了,截止目前都已经到了4.0.1版本了,这也是 ...

  6. 705&period; New Distinct Substrings spoj(后缀数组求所有不同子串)

    705. New Distinct Substrings Problem code: SUBST1 Given a string, we need to find the total number o ...

  7. C&num; 以管理员权限删除文件

    前言 通过后台,想删除C盘下”C:\\Windows\\winsxs\\Backup“的缓存文件. 然后提示对路径“C:\\Windows\\winsxs\\Backup\\amd64_hid-use ...

  8. CCF CSP 201409-1 相邻数对

    题目链接:http://118.190.20.162/view.page?gpid=T16 问题描述 试题编号: 201409-1 试题名称: 相邻数对 时间限制: 1.0s 内存限制: 256.0M ...

  9. luogu3203 弹飞绵羊 &lpar;LCT&rpar;

    新建一个N+1的点,飞出去的连到这个上,记size,每次统计x和N+1的链长就可以. 别忘了编号是从0开始的 #include<cstdio> #include<cstring&gt ...

  10. 复制MIFARE Classic卡

    Mifare Classic 1K智能卡介绍及nfc-tools的使用 [原创]RFID安全之——ACR122U菜鸟初体验-『智能设备』-看雪安全论坛 复制MIFARE Classic小区门禁卡记录 ...