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

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.

//题目意思是,第一行有两个整数n,m,说明有n个边,m个点,接下来n行,每行有三个整数,a,b,c,说明从 a 到 b 距离是多少,输出从1- n 的最小路程

//显然,这是一道水题,dijstra算法 4116kb 110ms

 //dijkstra 算法
#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
#define INF 0x3f3f3f3f
#define MX 1005 int n,m;
int mp[MX][MX]; int dis[MX];
bool vis[MX]; void dijkstra()
{
memset(vis,,sizeof(vis));
memset(dis,0x3f,sizeof(dis));
dis[]=; for(int i=;i<=n;i++)
{
int mim=INF,v;
for(int j=;j<=n;j++)
if(!vis[j] && dis[j]<mim)
{
v=j;
mim=dis[j];
}
vis[v]=;
for(int j=;j<=n;j++)
if(!vis[j] && dis[j]>mp[v][j]+dis[v])
dis[j]=mp[v][j]+dis[v];
}
printf("%d\n",dis[n]);
} int main()
{
while(~scanf("%d%d",&m,&n))
{
memset(mp,0x3f,sizeof(mp));
for(int i=;i<m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(mp[a][b]>c) mp[a][b]=mp[b][a]=c;//只记最小的
}
dijkstra();
}
return ;
}

//spfa 算法,很牛逼 260kb 0ms过了

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
#define MXN 1005
#define MXM 4010 struct Edge{
int to;
int w;
int nex;
}edge[MXM]; int n,m,r_m;
int headlist[MXN];
int dis[MXN];
int vis[MXN]; void spfa()
{
queue <int> Q;
memset(vis,,sizeof(vis));
memset(dis,0x3f,sizeof(dis));
dis[]=;
vis[]=;
Q.push();
while(!Q.empty())
{
int u =Q.front();Q.pop();
vis[u]=;
for(int i=headlist[u];i!=-;i=edge[i].nex)
{
int to=edge[i].to, w=edge[i].w;
if(dis[u]+w<dis[to])
{
dis[to]=dis[u]+w;
if(!vis[to])
{
vis[to]=;
Q.push(to);
}
}
}
}
printf("%d\n",dis[n]);
} int main(){
int i,a,b,c;
while(~scanf("%d%d",&m,&n))
{
for(i=;i<=n;i++) headlist[i]=-;
r_m=;
for(i=;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
edge[r_m]=(Edge){b,c,headlist[a]};
headlist[a]=r_m;
edge[r_m+]=(Edge){a,c,headlist[b]};
headlist[b]=r_m+;
r_m+=;
}
spfa();
}
return ;
}

Til the Cows Come Home(最短路模板题)的更多相关文章

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

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

  2. POJ 2387 Til the Cows Come Home(最短路模板)

    题目链接:http://poj.org/problem?id=2387 题意:有n个城市点,m条边,求n到1的最短路径.n<=1000; m<=2000 就是一个标准的最短路模板. #in ...

  3. POJ 2387 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 ...

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

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

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

  6. poj1511/zoj2008 Invitation Cards(最短路模板题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Invitation Cards Time Limit: 5 Seconds    ...

  7. HDU 5521.Meeting 最短路模板题

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  8. [poj2449]Remmarguts' Date(K短路模板题,A*算法)

    解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...

  9. 牛客小白月赛6 I 公交线路 最短路 模板题

    链接:https://www.nowcoder.com/acm/contest/136/I来源:牛客网 题目描述 P市有n个公交站,之间连接着m条道路.P市计划新开设一条公交线路,该线路从城市的东站( ...

随机推荐

  1. Java IO 学习(二)select/poll/epoll

    如上文所说,select/poll/epoll本质上都是同步阻塞的,但是由于实现了IO多路复用,在处理聊天室这种需要处理大量长连接但是每个连接上数据事件较少的场景时,相比最原始的为每个连接新开一个线程 ...

  2. zoj 2615 Cells 栈的运用

    题目链接:ZOJ - 2615 Scientists are conducting research on the behavior of a newly discovered Agamic Cell ...

  3. codeigniter 使用

    CodeIgniter系列 记录count和分页 对于某个表的不带条件的count,可以简单的用 $total = $this->db->count_all($table_name) 来获 ...

  4. MD5 algorithm in Objective C

    How to calculate the MD5 in objective C ? md5 is available on the iPhone and can be added as an exte ...

  5. 【hash】什么是hash,什么是哈希,什么是hash散列,什么是hash一致性算法【关于hash的详解】

    什么是hash,什么是哈希,什么是hash散列,什么是hash一致性算法

  6. 【IntelliJ IDEA】升级之后又要激活的解决方法

    用了几个月的idea,在它升级之后,又不听话了.启动时候需要重新激活. 解决方法: 1.找到C:\Windows\System32\drivers\etc\下的hosts文件 在文件中添加如下: 0. ...

  7. 在Android上编译OSG[3.0.2 ] (转)

    在Android上编译OSG[3.0.2 ] 分类:Android   This file contents can be applied for version OpenSceneGraph(OSG ...

  8. 通过Java的Domain类构建ElasticSearch的mapping

    通过给定一个Java的class类自行创建ElasticSearch的mapping Order的domain类 public class Order { public String system_i ...

  9. 【音乐App】—— Vue-music 项目学习笔记:搜索页面开发

    前言:以下内容均为学习慕课网高级实战课程的实践爬坑笔记. 项目github地址:https://github.com/66Web/ljq_vue_music,欢迎Star. 搜索歌手歌曲 搜索历史保存 ...

  10. 转:java工程师成神之路

    转自: http://www.hollischuang.com/archives/489 一.基础篇 1.1 JVM 1.1.1. Java内存模型,Java内存管理,Java堆和栈,垃圾回收 htt ...