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. ubuntu下面板上无网络连接的图标

    解决方法:删除旧的网络配置,重新让networkManager自动配置 sudo service network-manager stop sudo rm /var/lib/NetworkManage ...

  2. centos7 更新源 安装ifconfig

    centos7最小化安装后,ifconfig是不可用的,可以使用ip addr或ip link查看网络信息. 更新源之前,先确定网络是否连通.我用的虚拟机,因为桥接受公司ip限制,换成了NAT模式,确 ...

  3. unity在安卓中横屏闪退

    竖屏没问题,横屏闪退 配置文件的AndoridManifest.xml横竖屏设置要和UNITY设置的一致,否则就会强退 UNITY横竖屏设置

  4. 卸载 Windows 8/8.1/10 无法常规卸载的内置应用

    现在已经有一款可以卸载内置应用的软件了:http://www.thewindowsclub.com/10appsmanager-windows-10 在应用商店里下了一个计算器+,于是想把内置的计算器 ...

  5. android-tip-关于SpannableString的使用

    如果想单独设置TextView上其中几个字的样式,该怎么办? 答案是使用SpannableString. 使用SpannableString可以为TextView上的某字或某些字设置: 前景色(For ...

  6. eval 是执行一段完整的js字符串代码,并将结果返回

    var strArray="[{"message1":{ "id": "-1","content": &quo ...

  7. 文件操作getc

    getc函数的作用是从打开的文件中获取一个字符,并加文件指针自动加1,获取的字符在返回值中. 我写了一个读取一个文件255个字节的程序. int main() { FILE *p; fopen_s(& ...

  8. 协方差与pearson相关系数

    协方差 协方差大于0,表示两个随机变量正线性相关 协方差等于0,表示两随机变量无线性相关 协方差小于0,表示两随机变量负线性相关 协方差智能表示随机变量的线性相关关系,不能刻画其相关程度. 因此引入了 ...

  9. LWIP协议栈1

    STM32F4自带的MAC,而没有PHY纯模拟电路部分,没有把PHY做进STM32F4是因为会对芯片的功耗有影响,同时芯片的体积会增大等原因. MAC与PHY的通信接口是MII以及RMII方式. MD ...

  10. pymsql的简单实用方法

    在进行本文以下内容之前需要注意: 1.你有一个MySQL数据库,并且已经启动. 2.你有可以连接该数据库的用户名和密码 3.你有一个有权限操作的database 连接数据库 #导入pymsql imp ...