POJ - 2387 Til the Cows Come Home (最短路Dijkstra+优先队列)
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
* 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
Sample Input
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
Sample Output
90
Hint
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 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+优先队列)的更多相关文章
- 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 ...
- POJ 2387 Til the Cows Come Home(最短路模板)
题目链接:http://poj.org/problem?id=2387 题意:有n个城市点,m条边,求n到1的最短路径.n<=1000; m<=2000 就是一个标准的最短路模板. #in ...
- POJ 2387 Til the Cows Come Home --最短路模板题
Dijkstra模板题,也可以用Floyd算法. 关于Dijkstra算法有两种写法,只有一点细节不同,思想是一样的. 写法1: #include <iostream> #include ...
- 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 ...
- POJ.2387 Til the Cows Come Home (SPFA)
POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...
- Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化)
Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化) 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回 ...
- POJ 2387 Til the Cows Come Home
题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K ...
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
传送门 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46727 Acce ...
- 怒学三算法 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 ...
- 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 ...
随机推荐
- MyCat的安装及基本使用(MySQL分库分表)
1.Mycat的简介 1.1 数据库集群产生的背景 如今随着互联网的发展,数据的量级也是成指数的增长,从GB到TB到PB.对数据的各种操作也是愈加的困难,传统的关系性数据库已经无法满足快速查询与插入数 ...
- 使用Jenkins集成和自动化打包资料
1.手把手教你利用Jenkins持续集成iOS项目 http://www.jianshu.com/p/41ecb06ae95f 2.Jenkins+ Xcode+ 蒲公英 实现IOS自动化打包和分发 ...
- 为什么要使用href=”javascript:void(0);”
为什么要使用href=”javascript:void(0);” href=”javascript:void(0);”这个的含义是,让超链接去执行一个js函数,而不是去跳转到一个地址,而void( ...
- 跨版本mysqldump恢复报错Errno1449
已经有一套主从mysql,新增两个slave主库Server version: 5.6.22-log MySQL Community Server (GPL)旧从库Server version: 5. ...
- CentOS JAVA安装及查看路径方法
一.安装: 方法一:手动解压JDK的压缩包,然后设置环境变量 1.在/usr/目录下创建java目录 [root@localhost ~]# mkdir/usr/java[root@localhost ...
- maven 执行本地、服务器 jar包安装
开发时遇到过第三方jar包依赖不了时的尴尬 因为遇到过几次所以记录一下,POM文件引入的个推jar包无效,就必须本地安装了,服务器上的也是一样,执行相同的maven命令就行,注意修改路径!和将jar包 ...
- poj 2007 Scrambled Polygon
#include<stdio.h> #include<algorithm> using namespace std; #define Max 60 struct Point { ...
- 并查集 - 1611 The Suspects
题目地址: http://poj.org/problem?id=1611 分析: - 数据结构 - parent[x] 表示 x 元素的父节点位置. - rank[x] 记录x的子链的长度, 以便在合 ...
- maven 引用本地jar
1.添加lib文件夹在src文件夹中.2.拷贝所需要的test.jar包到lib文件夹.3.在pom文件加入如下依赖 <!--添加本地私有包--><dependency> &l ...
- Ceph介绍
1. 介绍 云硬盘是IaaS云平台的重要组成部分,云硬盘给虚拟机提供了持久的块存储设备.目前的AWS 的EBS(Elastic Block store)给Amazon的EC2实例提供了高可用高可靠的块 ...