题目描述

Description

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.

 
 
大意就是求1到n之间的最短路径,由于点的个数只有1000,O(N^2)dijkstra的算法即可解决。
代码如下
#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(最短路板子题)的更多相关文章

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

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

  3. Til the Cows Come Home(最短路模板题)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description Bessie is ...

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

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

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

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

  6. POj2387——Til the Cows Come Home——————【最短路】

    A - Til the Cows Come Home Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & ...

  7. POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)

    Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37662   Accepted ...

  8. (Dijkstra) POJ2387 Til the Cows Come Home

    Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 81024   Accepted ...

  9. POJ-2387Til the Cows Come Home,最短路坑题,dijkstra+队列优化

    Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K       http://poj.org/problem?id=238 ...

  10. POJ 2387 Til the Cows Come Home (最短路径 模版题 三种解法)

    原题链接:Til the Cows Come Home 题目大意:有  个点,给出从  点到  点的距离并且  和  是互相可以抵达的,问从  到  的最短距离. 题目分析:这是一道典型的最短路径模版 ...

随机推荐

  1. 第三篇 SpringBoot整合log4j2详解

    源代码:https://pan.baidu.com/s/1d1Lwv1gIvVNltIKVWeEseA 提取码:wff0 SpringBoot整合Log4j2步骤: 1.删除spring-boot-s ...

  2. typeof和类型转换

    编程形式 ① 面向过程 ② 面向对象 ③ Js既面向过程又面向对象 typeof(数据) 1)typeof(数据)返回该数据是什么类型的 2)写法: ① typeof(数据) ② typeof 数据 ...

  3. 基于HttpURLConnection的接口调用,支持GET&POST

    单位要做一个多级部署平台,大概意思就是一级系统可以监控下属地域的数据也可管理本地的数据.之前做短信猫用过httpClient请求,与此大同小异.封装了一个两种请求方式的工具类. package com ...

  4. git 指令笔记

    狂躁,太狂躁!!赶上过年,赶上自己的懒癌,12月底就学完的教程直到今天才整理笔记,中途沉默在游戏中..... 只给出Windows下git指令操作,推荐大家去廖雪峰前辈那里学习(百度搜索:廖雪峰的官方 ...

  5. 普通版js运动框架

    //获取样式 function getStyle(obj){ if(obj.currentStyle){ return obj.currentStyle[attr]; }else{ return ge ...

  6. linux中其他搜索命令(locate/which/whereis/grep)

    目录 locate which whereis grep locate 解释 命令名称:locate 命令所在路径:/usr/bin/locate 执行权限:所有用户 功能描述:在文件资料库中查找文件 ...

  7. 「Flink」使用Java lambda表达式实现Flink WordCount

    本篇我们将使用Java语言来实现Flink的单词统计. 代码开发 环境准备 导入Flink 1.9 pom依赖 <dependencies> <dependency> < ...

  8. Windows Server 2012 R2的安装(GUI桌面版本)

    镜像:cn_windows_server_2012_r2_x64_dvd_2707961.iso 1.将安装光盘插入服务器,开机会读取到Windows安装程序,点击下一步 2.点击现在安装     3 ...

  9. Android Vitamio初探

    GitHub: https://github.com/yixia/VitamioBundle 1.下载完毕导入用Android Studio打开 2.新建Mode,引入依赖 dependencies ...

  10. VMware使用与安装

    VMware安装 下载完Vmware -> 双击打开安装包 -> 选择下一步(如下图界面) 选择接受协议,点击下一步 选择经典进行安装.这个是默认安装,会把默认插件安装到相对应的路径 选择 ...