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. Android开发实战之补间动画和属性动画

    说起动画,其实一点也不陌生,在使用一款app的时候为了优化用户体验,多多少少的,都会加入动画. 安卓中的动画,分为两大类:补间动画和属性动画.本篇博文会详细介绍总结这两大动画,希望本篇博文对你的学习和 ...

  2. Nginx源码完全注释(9)nginx.c: ngx_get_options

    本文分析 ngxin.c 中的 ngx_get_options 函数,其影响: nginx.c 中的: static ngx_uint_t ngx_show_help; static ngx_uint ...

  3. Computer2

    luo@luo-All-Series:~/MyFile/TensorflowProject$ conda create -n flappbird1 python=3.7Solving environm ...

  4. 621. Task Scheduler CPU任务间隔分配器

    [抄题]: Given a char array representing tasks CPU need to do. It contains capital letters A to Z where ...

  5. oracle数据库学习记录(持续更新中...)

    --------------------------------------------day1------------------------------------------------- 1. ...

  6. Golang 之 key-value LevelDB

    时常会在应用中用到数据库功能,象 Key-Value 性质的.直接搬个 Redis,Mysql嫌大,好在有 LevelDB,直接编进应用中. 有关什么是 LevelDB 以及 LevelDB 的特性, ...

  7. js中的函数参数问题

    js函数没有Java中的重载现象.js函数的参数是放在arguments的容器里面的. <script  type="text/javascript"> functio ...

  8. Selenium运用-漫画批量下载

    今天我们要爬去的网站是http://comic.sfacg.com/.漫画网站一般都是通过JavaScript和AJAX来动态加载漫画的,这也就意味着想通过原来爬取静态网站的方式去下载漫画是不可能的, ...

  9. 4.4.1 CAS详解

    一.什么是CAS CAS,compare and swap的缩写,中文翻译成比较并交换. 我们都知道,在java语言之前,并发就已经广泛存在并在服务器领域得到了大量的应用.所以硬件厂商老早就在芯片中加 ...

  10. [GO]并发实现聊天室服务器

    package main import ( "net" "fmt" "strings" "time") type Cli ...