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. MySQL的XA_prepare_event类型binlog的解析

    为了支持新版的xa事务,MySQL新加了一种binlog event类型:XA_prepare 项目中使用的开源组件mysql-binlog-connector-java无法解析此种binlog ev ...

  2. Centos7下实现多虚拟机互信

    假设CentOS 7三台虚拟机A(192.168.111.10).B(192.168.111.11).C(192.168.111.12),需要保证三台虚拟机之间网络的连通性. 操作步骤: 一.在A机上 ...

  3. Context都没弄明白,还怎么做Android开发?

    Activity mActivity =new Activity() 作为Android开发者,不知道你有没有思考过这个问题,Activity可以new吗?Android的应用程序开发采用JAVA语言 ...

  4. pair类型

    pair是一个模板数据类型,其中包含两个数据值,两个数据值可以不同 如 pair<int,string>a(2,"fgh");则a是一个pair类型,它包括两个数据,第 ...

  5. 处理类型(typedef,uisng,auto,decltype)

    一:类型别名是一个名字,它是某种类型的定价.有两种方法定义类型别名: 1.使用typedef关键字,如: typedef int *Int_Ptr Int_Ptr p=nullptr;   //Int ...

  6. python 浮点数保留小数

    http://www.cnblogs.com/Raymon-Geng/p/5784290.html 这里有三种方法, round(a,2) '%.2f' % a Decimal('5.000').qu ...

  7. Centos7/RedHat7 下 python3使用cx-freeze打包matplotlib程序遇到的问题和解决办法

    折腾了一天遇到了几个头疼的问题,还好回去前解决掉了 第一个:执行cxfreeze打包好的程序遇到 tkinter 和 _tkinter的缺失问题 首先终端:python tkinter python ...

  8. UE把环境变量Path改了

    为了比较个文件,装了UE. 文件比较完了,环境变量也被改了. 改还不是写添加式的改,是写覆盖式的改. 搞得ant都起不动了,一看Path被改的那样(C:\hy\soft\ultraedit\Ultra ...

  9. C 共用体

    C 共用体 共用体是一种特殊的数据类型,允许您在相同的内存位置存储不同的数据类型.您可以定义一个带有多成员的共用体,但是任何时候只能有一个成员带有值.共用体提供了一种使用相同的内存位置的有效方式. 定 ...

  10. 4种使用webpack提升vue应用的方式

    本文参考自:https://mp.weixin.qq.com/s?src=11&timestamp=1526886111&ver=889&signature=u9SixhvlJ ...