poj2387- Til the Cows Come Home(最短路板子题)
题目描述
Description
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<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std; const int INF=2e9;
int V,E;
int d[];
int cost[][];
int used[];
void dijkstra(int s)
{ for(int i=;i<=V;i++)
{
used[i]=;
d[i]=INF;
}
d[s]=;
int minn,v;
for(int i=;i<=V;i++)
{
minn=INF;
v=-;
for(int j=;j<=V;j++)
{
if(!used[j]&&d[j]<minn)
{
minn=d[j];
v=j;
}
}
used[v]=;
for(int j=;j<=V;j++)
{
if((!used[j])&&(d[j]>cost[v][j]+d[v]))
{
d[j]=cost[v][j]+d[v];
}
}
}
}
int main()
{
cin>>E>>V;
for(int i=;i<=;i++)
{
for(int j=;j<=;j++)
{
cost[i][j]=INF;
if(i==j)
cost[i][j]=;
}
}
for(int i=;i<=E;i++)
{
int s,t,cos;
cin>>s>>t>>cos;
if(cos<cost[s][t])
{
cost[s][t]=cos;
cost[t][s]=cos;
}
}
dijkstra();
printf("%d\n",d[V]);
return ;
}
poj2387- Til the Cows Come Home(最短路板子题)的更多相关文章
- POJ2387 Til the Cows Come Home (最短路 dijkstra)
AC代码 POJ2387 Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to ...
- POJ-2387 Til the Cows Come Home ( 最短路 )
题目链接: http://poj.org/problem?id=2387 Description Bessie is out in the field and wants to get back to ...
- Til the Cows Come Home(最短路模板题)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Description Bessie is ...
- POJ 2387 Til the Cows Come Home --最短路模板题
Dijkstra模板题,也可以用Floyd算法. 关于Dijkstra算法有两种写法,只有一点细节不同,思想是一样的. 写法1: #include <iostream> #include ...
- Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化)
Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化) 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回 ...
- POj2387——Til the Cows Come Home——————【最短路】
A - Til the Cows Come Home Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & ...
- POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37662 Accepted ...
- (Dijkstra) POJ2387 Til the Cows Come Home
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 81024 Accepted ...
- POJ-2387Til the Cows Come Home,最短路坑题,dijkstra+队列优化
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K http://poj.org/problem?id=238 ...
- POJ 2387 Til the Cows Come Home (最短路径 模版题 三种解法)
原题链接:Til the Cows Come Home 题目大意:有 个点,给出从 点到 点的距离并且 和 是互相可以抵达的,问从 到 的最短距离. 题目分析:这是一道典型的最短路径模版 ...
随机推荐
- JS水仙花数
题目:3位数==个位立方+十位的立方+百位的立方.这个3位数就是水仙花数.要求打印出所有的水仙花数 <body> <div id=d1> </div> <sc ...
- Git简易教程(常用命令)
本文章参考了Pro Git 1 Git简介 Linux内核开源项目有着众多参与者,为了提高开发效率,项目组于2002年开始启用分布式版本控制系统BitKeeper来管理和维护代码.在BitKeeper ...
- MySQL中的执行计划explain
一.用法及定义: explain为sql的执行计划.在sql前面加上explain关键字即可 如:explain select * from tbl_emp; 名词解释: id:[操作表的顺序] 1. ...
- Linux 文件、目录操作
Linux中的路径只能使用/,不能使用\ 或\\. cd 切换目录 cd / 切换到系统根目录,cd即change dir cd /bin 切换到根目录下的bin目录 cd .. ...
- App自动化测试环境搭建
只做记录和注意点,详细内容不做解释 环境:win+appium+夜神模拟器+python 需要用到的工具: 1.java JDK 2. node.js 3. Android SDK 4.Appium- ...
- The server cannot be started because one or more of the ports are invalid. Open the server editor and correct the invalid ports.
在eclipse里运行jsp文件最初迟迟没有反应,重启报了这个错误,tomcat的端口设置有问题.需要打开服务器设置一下端口号. 点击Servers,如果没有这一项,按照Window-Show Vie ...
- 《自拍教程19》aapt_apk信息查看工具
aapt命令行工具介绍 aapt.exe(Linux/Ubuntu/imac操作系统下是未带后缀的aapt), 是android sdk自带的用于打包apk,解析apk的命令行工具软件. aapt.e ...
- tensorflow安装问题
环境 windows7 开始的时候,打算在最新版本下安装tensorflow体验下,python是3.6 anaconda是最新的5.0.1 结果执行 pip3 install --upgrade ...
- 数据库自学笔记(2)--- HAVING和WHERE, ANY 和 ALL,IN和EXIST。
1.HAVING和WHERE: WHERE 和 HAVING 的作用对象不一样.WHERE作用于基本表或视图,挑出满足条件的元组.HAVING作用于组(group),一般配合GROUP BY 使用. ...
- P2918 [USACO08NOV]买干草Buying Hay
链接:Miku ---------------- 这就是一个完全背包的板子题 ---------------- 我们把重量当作重量,开销当作价值,那么这个题就是个求价值最小的完全背包 然而题目上说了是 ...