题目描述

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. hexo博客

    安装软件 node.js(建议稳定版本,本人安装v8.11.3) npm install -g hexo-cli hexo init myBlog //初始化,在myBlog的文件夹下建立网站 hex ...

  2. JAVA架构之单点登录 任务调度 权限管理 性能优化大型项目实战

    单点登录SSO(Single Sign On)说得简单点就是在一个多系统共存的环境下,用户在一处登录后,就不用在其他系统中登录,也就是用户的一次登录能得到其他所有系统的信任.单点登录在大型网站里使用得 ...

  3. springboot 后台框架平台 mybatis 集成代码生成器 shiro 权限 websocket

    1.代码生成器: [正反双向](单表.主表.明细表.树形表,快速开发利器)freemaker模版技术 ,0个代码不用写,生成完整的一个模块,带页面.建表sql脚本.处理类.service等完整模块2. ...

  4. Python模块之Requests

    目录 Requests 模块 常规的get请求 基于ajax的get请求 常规的post请求 基于ajax的post请求 综合项目实战 requests模块高级 requests代理 验证码处理 Re ...

  5. java 关于xlsx(xls) 和 csv 文件的数据解析

    1.适用于xlsx 和 xls  <!--xlsx和xls文件pom依赖--> <dependency> <groupId>org.apache.poi</g ...

  6. nodejs编程优化

    如何编写优化的 JavaScript 对象属性的顺序:始终以相同的顺序实例化对象属性,以便共享的隐藏类和随后优化的代码可以共享之.   动态属性:在实例化之后向对象添加属性将强制执行隐藏的类更改,并降 ...

  7. Java多线程之线程的协作

    等待队列 每个实例都有一个等待,他是在实例的wait方法执行后停止操作的队列,除非发现以下情况,线程才会退出等待队列 1.有其他线程的notify方法来唤醒线程 2.有其他线程的notifyAll方法 ...

  8. javascript中DOM获取和设置元素的内容、样式及效果

    getElementById() 根据id获取dom元素 没有找到则返会Null <!DOCTYPE html> <html lang="en"> < ...

  9. Java JDK 1.8 安装及配置

    1.下载 下载地址:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 要注册,注 ...

  10. SP1805 HISTOGRA - Largest Rectangle in a Histogram

    --------------------------------------------------- 我就是想学个单调栈然后全网都是个蓝题 ----------------------------- ...