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. Machine Learning Done Wrong【转】

    1. Take default loss function for granted Many practitioners train and pick the best model using the ...

  2. 输出n行等腰三角形(符号为*)

    输出n行等腰三角形(符号为*) 1. 核心操作 First, 找出每一行的第一个*之前需要的空格个数 规律1:设该等腰三角形一共N行, 那么第n行的第一个*之前需要的空格个数就为N-n个空格 推导过程 ...

  3. php的session与免登陆问题

    Session 与 Session的GC 由于PHP的工作机制,它并没有一个daemon线程来定期的扫描Session 信息并判断其是否失效,当一个有效的请求发生时,PHP 会根据全局变量 sessi ...

  4. QQ分享到电脑SDK bug

    问题:当图文(图片+文字+url)混合分享到我的电脑时,就会出bug,只显示图片. 经过测试: params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL," ...

  5. dedecms 调取当前栏目的链接和 栏目名称

    <a href="{dede:field name='typeurl' function=”GetTypeName(@me)”/}" target="_blank& ...

  6. Poj2826 An Easy Problem

    呵呵哒.WA了无数次,一开始想的办法最终发现都有缺陷.首先需要知道: 1)线段不相交,一定面积为0 2)有一条线段与X轴平行,面积一定为0 3)线段相交,但是能接水的三角形上面线段把下面的线段完全覆盖 ...

  7. EasyMvc入门教程-基本控件说明(5)小图标

    我们网页很多时候需要小图标来进行美化,EasyMvc默认提供了100多种常用小图标,您可以根据实际情况选择使用,请看下面的例子: @Html.Q().Ico().Type(EasyMvcHelper. ...

  8. TortoiseGit在github上创建工程

    一.前期准备 TortoiseGit官网下载地址:http://code.google.com/p/tortoisegit/ git下载地址:https://git-scm.com/download/ ...

  9. RR调度(Round-robin scheduling)简单介绍

    在RR调度策略下,一个线程会一直运行.直到: 自愿放弃控制权 被更高优先级的线程抢占 时间片用完 例如以下图所看到的,A在用完自己的时间片后,将CPU运行权让给线程B.于是A离开Read队列,而B进入 ...

  10. centos6.5下载

    1.64位系统 http://mirrors.163.com/centos/6.5/isos/x86_64/CentOS-6.5-x86_64-bin-DVD1.iso http://mirrors. ...