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.

 
解题思路:题目大意就是求最短路,从n到1的最短路。
关于最短路径的思想在前面的博客有;
代码如下:
 #include<iostream>
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std ; const int INF = 0x3f3f3f3f;
int G[][];
int d[]; int i ,j; struct node{
int num;
int dis;
friend bool operator<(node a ,node b)
{
return a.dis>b.dis;
}
}; int main()
{
int M , N;
int x,y,D; priority_queue<node>que; while(scanf("%d%d",&M,&N)!=EOF)
{
for( i = ;i <= N ;i++)
{
for( j = ;j <= N ;j++)
{
G[i][j] = INF;
}
} for(int i = ; i <= N ;i++)
{
G[i][i] = ;
}
for( i = ; i <= M ;i++)
{
scanf("%d%d%d",&x,&y,&D); if(G[x][y]>D)
{
G[x][y] = D;
G[y][x] = D;
} }
memset(d,0x3f,sizeof(d));
d[] = ;
que.push({,});
while(!que.empty())
{
node tp = que.top(); que.pop(); for(i = ;i <= N ;i++)
{
if(G[tp.num][i])
{
if(d[i]>d[tp.num]+G[tp.num][i])
{
d[i] = d[tp.num] + G[tp.num][i];
que.push({i,d[i]});
}
}
}
} printf("%d\n",d[N]);
while(!que.empty())
{
que.pop();
} }
return ;
}

POJ - 2387 Til the Cows Come Home (最短路Dijkstra+优先队列)的更多相关文章

  1. POJ 2387 Til the Cows Come Home(模板——Dijkstra算法)

    题目连接: http://poj.org/problem?id=2387 Description Bessie is out in the field and wants to get back to ...

  2. POJ 2387 Til the Cows Come Home(最短路模板)

    题目链接:http://poj.org/problem?id=2387 题意:有n个城市点,m条边,求n到1的最短路径.n<=1000; m<=2000 就是一个标准的最短路模板. #in ...

  3. POJ 2387 Til the Cows Come Home --最短路模板题

    Dijkstra模板题,也可以用Floyd算法. 关于Dijkstra算法有两种写法,只有一点细节不同,思想是一样的. 写法1: #include <iostream> #include ...

  4. 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 ...

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

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

  6. Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化)

    Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化) 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回 ...

  7. POJ 2387 Til the Cows Come Home

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

  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 ...

  10. POJ 2387 Til the Cows Come Home (最短路 dijkstra)

    Til the Cows Come Home 题目链接: http://acm.hust.edu.cn/vjudge/contest/66569#problem/A Description Bessi ...

随机推荐

  1. css常用属性总结第二弹:id选择器

    承接上一篇class选择器,这一篇我们来说说css的id选择器. id选择器类似于类选择器,不过也有一些重要的差别,首先,id选择器前面有一个#号----称它为棋牌号吧,class为点号,用法就和cl ...

  2. Nginx通过CORS实现跨域

    Nginx通过CORS实现跨域 2016-09-01 10:33 阅读 9.4k 评论 0 社区广播:运维派(Yunweipai.com)是国内最早成立的IT运维社区,欢迎大家投稿,让运维人不再孤寂的 ...

  3. 利用fetch进行POST传参

    fetch(config.host+"url",{      method:"POST",      mode: 'cors',跨域请求 headers: { ...

  4. ubuntu 源码安装 lnmp 环境

    准备篇 下载软件包 1.下载nginx http://nginx.org/download/nginx-1.2.0.tar.gz 2.下载pcre  (支持nginx伪静态) ftp://ftp.cs ...

  5. 关于@property与@syntheszie的使用问题

    写在前面:在ARC大行其道的“现代化社会”,不少人不再对“完整的“OC”抱有它应该获得的尊重,于是浮躁成了代名词~~ 在使用ARC时,大家声明变量的过程中,往往使用@property来通过编译器,隐式 ...

  6. Solidity oraclize解析Json格式数据

    solidity虽然不能解析json数据但是oraclize_query可以直接处理: pragma solidity ^; import "github.com/oraclize/ethe ...

  7. 移动端flexbox的小tips

    我也是看了腾讯isux的博客,解答了我关于flexbox一个很长时间的疑惑,就是flex布局在安卓手机会出现内容长短不同导致不均分的现象. 具体的内容可以去看腾讯isux的博客,地址在这:https: ...

  8. code1174 靶形数独

    主要是优化搜索顺序 从选择较少的点开始,可以大大提高效率 在search(x,y)找点的时候,对于一个空点(x y),设置一个评分score: score=min{ 横线x上能填的数字个数,竖线y上. ...

  9. 实践作业3:白盒测试----junit的难点DAY11.

    本次白盒测试 需要独立完成整个项目和工具的配置安装运行操作,并编写.运行测试脚本,并完成实验的一些小细节等等. 首先,导入Junit测试框架所需的Jar包 然后编写测试脚本,为.java运行程序,见打 ...

  10. spring mvc 集成hibernate步骤

    今天从头把hibernate集成进入springMVC框架中,把过程记录下来. 1.首先要在监听器配置文件中加入hibernate支持,如下: <?xml version="1.0&q ...