problem

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 <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
using namespace std;
int m,n;
const int inf = 0x3f3f3f3f;
int dis[1005];
int gra[1005][1005];
int vis[1005];
void dj()
{
memset(vis,0,sizeof(vis));
int minn,v;
for(int i = 1; i <= n; i ++) dis[i] = gra[1][i];
for(int i = 1; i <= n; i ++)
{
minn = inf;
for(int j = 1; j <= n; j ++)
{
if(!vis[j] && dis[j] < minn)
{
minn = dis[j];
v = j;
}
}
vis[v] = 1;
for(int j = 1; j <= n; j ++)
{
if(gra[v][j] + dis[v] < dis[j] && !vis[j])
{
dis[j] = gra[v][j] + dis[v];
}
}
}
printf("%d\n",dis[n]);
}
int main()
{
int i,j,a,b,c;
while(~scanf("%d%d",&m,&n))
{
for(i = 1; i <= n; i ++)
{
for(j = 1; j <= n; j ++)
{
if(i == j) gra[i][j] = 0;
else gra[i][j] = gra[j][i] = inf;
}
}
for(i = 1; i <= m; i ++)
{
scanf("%d%d%d",&a,&b,&c);
if(gra[a][b] > c ) gra[a][b] = gra[b][a] = c;
}
dj();
}
return 0;
}

Til the Cows Come Home ( POJ 2387) (简单最短路 Dijkstra)的更多相关文章

  1. Til the Cows Come Home(poj 2387 Dijkstra算法(单源最短路径))

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32824   Accepted: 11098 Description Bes ...

  2. (最短路 弗洛伊德) Til the Cows Come Home -- POJ --2387

      #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> ...

  3. kuangbin专题专题四 Til the Cows Come Home POJ - 2387

    题目链接:https://vjudge.net/problem/POJ-2387 题意:从编号为n的城市到编号为1的城市的最短路. 思路:dijkstra模板题,直接套板子,代码中我会带点注释给初学者 ...

  4. POJ 2449 第k短路 Dijkstra+A*

    这道题我拖了半年,,,终于写出来了 思路: 先反向建边 从终点做一次最短路 ->这是估价函数h(x) 再正常建边,从起点搜一遍 (priority_queue(h(x)+g(x))) g(x)是 ...

  5. POJ 2387 Til the Cows Come Home

    题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K ...

  6. POJ 2387 Til the Cows Come Home (图论,最短路径)

    POJ 2387 Til the Cows Come Home (图论,最短路径) Description Bessie is out in the field and wants to get ba ...

  7. POJ.2387 Til the Cows Come Home (SPFA)

    POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...

  8. POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)

    传送门 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 46727   Acce ...

  9. 怒学三算法 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 ...

随机推荐

  1. 工欲善其事必先利其器之Vagrant + VirtualBox

    Vagrant 可以管理 VirtualBox,实现快速配置创建虚拟机 1.下载软件 VirtualBox虚拟机下载地址:https://www.virtualbox.org/wiki/Downloa ...

  2. IDEA如何本机调试springboot应用打的jar包

    背景: 我用命名行 执行 java -jar  ***.jar发现 springboot启动时抛出错误,因此想debug进去看看究竟为什么出错. 1  在命令行执行 java -jar -Xdebug ...

  3. 注解Annotation原理详解及其应用示例

    一.什么是注解 注解也叫元数据,例如我们常见的@Override和@Deprecated,注解是JDK1.5版本开始引入的一个特性,用于对代码进行说明,可以对包.类.接口.字段.方法参数.局部变量等进 ...

  4. 【python+ddt】DDT模块的使用

    ddt模块包含了一个类的装饰器ddt和两个方法的装饰器: data:包含多个你想要传给测试用例的参数: file_data:会从json或yaml中加载数据: unpanck:通常data中包含的每一 ...

  5. bash功能——命令行编辑、内部命令 外部命令、命令补全 、命令历史、文件名通配符、命令别名

    命令行编辑: Ctrl + a : 跳转到当前编辑行首 Ctrl + e:跳转到当前编辑行尾 # mkdir /home/dira /home/diab 像这种命令,/home/dira 和 /hom ...

  6. linux基础--命令使用

    rpm命令 rpm -qa 包 查看包是否安装 rpm qa 列出系统安装的所有包 rpm -ql 包 查看软件包安装的位置及配置的目录 rpm -ivh 包 安装rpm包或强制安装包 rpm -Uv ...

  7. mongodb模式模型设计及编码-Mongoose

    走到这一步,我们的网站还不能称为动态的网站,因为所要的数据都是伪造的,所以现在要对数据库的模型进行设计   Mongoose 我们用到的工具模块是Mongoose,他能对Mongodb进行建模的这样一 ...

  8. hive中计算某个日期是星期几的算法

    pmod(floor((unix_timestamp('2019-06-11 00:00:00')-unix_timestamp('1970-01-05 00:00:00'))/(3600*24)), ...

  9. Python3+Appium学习笔记06-noReset参数

    百度很多文章说noReset这个参数是用来防止重复安装app的.可能这是以前的用法.目前最新版本appium默认是清除session信息,并且不会删除应用的. ​ noReset这个参数,根据appi ...

  10. MVVM框架-MVVMLight

    项目URL:http://www.mvvmlight.net/ 一.安装MVVMLight 在NuGet程序包中搜索MVVMLight,然后安装. 二.使用 安装完MVVMLight后项目中会自动生成 ...