题目描述

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. IntelliJ 更改项目使用的 JDK 版本

    在当前使用的 IntelliJ 中的 JDK 版本为 1.8,如何修改 IntelliJ 使用的 JDK 版本为 1.11 呢? 你可以在 IntelliJ 中进行修改. 选择 File 后,然后选择 ...

  2. 【转】Redis内部数据结构详解——ziplist

    本文是<Redis内部数据结构详解>系列的第四篇.在本文中,我们首先介绍一个新的Redis内部数据结构--ziplist,然后在文章后半部分我们会讨论一下在robj, dict和zipli ...

  3. ExecutionContext(执行上下文)综述

    >>返回<C# 并发编程> 1. 简介 2. 同步异步对比 3. 上下文的捕获和恢复 4. Flowing ExecutionContext vs Using Synchron ...

  4. OpenLayers要素拖拽

    //拖拽要素 function dragFeature (_map,_dragEndCallback) { let selFeature = null; _map.on("pointerdr ...

  5. 【iOS】Spring Animations (弹性动画)

    This interface shows how a spring animation can be created by specifying a “damping” (bounciness) an ...

  6. 链接github

    引用https://www.cnblogs.com/u-1596086/p/11588957.html 第一步:登录git创建项目 右上角头像按钮,点击your repositories 接着绿色按钮 ...

  7. PMP--0. 前言(闲言)

    先说一句话给未来的自己:你一定会感谢你现在的努力,当你回看时,记得带着现在的心境和心愿.未来更好的明天. --2019.12.1禾木留 今天是正式发布的时间--2020.01.01,听着新年快乐的祝福 ...

  8. 阿里云服务器Web Deploy配置和使用Visual Studio进行Web项目发布部署遇到的坑

    阿里云的服务器一直闲着,烧着银子,当初花几千大洋开通,本想弄信息化的项目为所帮扶的贫困户脱贫助手,不想势单力薄,一直没有找到好的项目.最近大家都在众志成城抗击新肺疫情,于是又想能不能尽点自己的力量,于 ...

  9. .net core 中api 模型验证

    AddControllers/AddMvc方法允许添加自定义ActionFilterAttribute进行过滤 文档中这么定义Filter: 可以创建自定义筛选器,用于处理横切关注点. 横切关注点的示 ...

  10. Windows10设置系统参数

     屏幕分辨率设置 电源屏幕显示时间 投影可以进行手机投影到电脑进行操作,远程桌面可以进行远程访问,如云服务器 设置桌面图标和背景 设置默认应用 安装软件,必备的几项软件 --其中个人认为(1)(2)是 ...