题面

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

题解

题目大意:给定N个点,T条边

求出从节点1到节点N的最短路径长度。


直接求最短路即可

习惯用SPFA。。。

如果用dijkstra要考虑重边的情况(舍掉之类的)

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std;
#define MAX 11000
#define MAXL 22000
struct Line
{
int v,next,w;
}e[MAXL];
int u,v,w;
int h[MAX],cnt=1;
int T,N;
queue<int> Q; bool vis[MAX];
int dis[MAX];
inline void Add(int u,int v,int w)
{
e[cnt]=(Line){v,h[u],w};
h[u]=cnt++;
}
int main()
{
cin>>T>>N;
for(int i=1;i<=T;++i)
{
cin>>u>>v>>w;
Add(u,v,w);
Add(v,u,w);
}
for(int i=1;i<=N;++i)
dis[i]=1050000000;
/*********SPFA***********/
vis[1]=true;dis[1]=0;
Q.push(1);
while(!Q.empty())
{
u=Q.front();Q.pop();
vis[u]=false;
for(int i=h[u];i;i=e[i].next)
{
v=e[i].v;
if(dis[v]>dis[u]+e[i].w)
{
dis[v]=dis[u]+e[i].w;
if(!vis[v])
{
vis[v]=true;
Q.push(v);
}
}
}
}
cout<<dis[N]<<endl;
return 0;
}

【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 最短路Dijkstra+bellman(普通+优化)

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

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

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

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

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

  6. (Dijkstra) POJ2387 Til the Cows Come Home

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

  7. poj2387 Til the Cows Come Home 最短路径dijkstra算法

    Description Bessie is out in the field and wants to get back to the barn to get as much sleep as pos ...

  8. poj2387 Til the Cows Come Home

    解题思路:最短路的模板题,注意一个细节处理即可. 见代码: #include<cstdio> #include<cstring> #include<algorithm&g ...

  9. POJ2387 Til the Cows Come Home 【Dijkstra】

    题目链接:http://poj.org/problem?id=2387 题目大意; 题意:给出两个整数T,N,然后输入一些点直接的距离,求N和1之间的最短距离.. 思路:dijkstra求单源最短路, ...

  10. POJ-2387.Til the Cows Come Home.(五种方法:Dijkstra + Dijkstra堆优化 + Bellman-Ford + SPFA + Floyd-Warshall)

    昨天刚学习完最短路的算法,今天开始练题发现我是真的菜呀,居然能忘记邻接表是怎么写的,真的是菜的真实...... 为了弥补自己的菜,我决定这道题我就要用五种办法写出,并在Dijkstra算法堆优化中另外 ...

随机推荐

  1. PLEC-交流电机系统+笔记

    1.固有机械特性近似图 2.三相交流电机的控制系统 1)理论推导 第一次制动选择能耗制动,第二次制动选择倒拉制动. 2)模型搭建 3)模拟仿真 3.心得体会和笔记总结 制动方式的选择主要是根据各个制动 ...

  2. python[error] - mysql_config not found

    具体报错信息: root@pts/4 $ pip install MySQL-python Collecting MySQL-python Using cached MySQL-python-1.2. ...

  3. python学习:hashlib模块使用

    #!/usr/bin/env python   import sys import hashlib   def md5sum(f):     m = hashlib.md5()     with op ...

  4. word设置每页50行

    1. 显示行号:页面布局-行号-每页重编行号 2. 调整页边距:页面布局-页边距-调整上下边距为2 3. 调整行数:页面布局-文档网格-只指定网格-设置行数

  5. Dockerfile 中的 CMD 与 ENTRYPOINT

    CMD 和 ENTRYPOINT 指令都是用来指定容器启动时运行的命令.单从功能上来看,这两个命令几乎是重复的.单独使用其中的一个就可以实现绝大多数的用例.但是既然 doker 同时提供了它们,为了在 ...

  6. ansible安装

    本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn 1.配置epel源 wget -O /etc/yum.repos.d ...

  7. Spring boot 整合mybatis

    第一步:创建maven项目并添加spring boot依赖: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns ...

  8. hihoCoder 1523 数组重排2 贪心

    题意:给定一个1-N的排列A1, A2, - AN,每次操作小Hi可以选择一个数,把它放到数组的最左边. 请计算小Hi最少进行几次操作就能使得新数组是递增排列的. 思路:最后的序列是递增的,那么必定满 ...

  9. CodeForces-747E

    这几天好懒,昨天写的题,今天才来写博客.... 这题你不知道它究竟有多少层,但是知道字符串长度不超过10^6,那么它的总容量是被限定的,用一个二维动态数组就OK了.输入字符串后,可以把它按照逗号分割成 ...

  10. Linux中select poll和epoll的区别

    在Linux Socket服务器短编程时,为了处理大量客户的连接请求,需要使用非阻塞I/O和复用,select.poll和epoll是Linux API提供的I/O复用方式,自从Linux 2.6中加 ...