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. LeetCode OJ--Anagrams **

    https://oj.leetcode.com/problems/anagrams/ 在一个vector<string>中,找到所有经过顺序变换,可以变成一样的 string. 首先,对每 ...

  2. jemalloc原理分析

    netty4引入了内存池的概念,它的主要思想源自于jemalloc,由于难以理解netty中这一块的代码,我决定先看一看网上的相关文章 官方git jemalloc原理分析 jemalloc和内存管理 ...

  3. 洛谷 P1328 生活大爆炸版石头剪刀布【模拟/环/周期】

    题目描述 石头剪刀布是常见的猜拳游戏:石头胜剪刀,剪刀胜布,布胜石头.如果两个人出拳一样,则不分胜负.在<生活大爆炸>第二季第8 集中出现了一种石头剪刀布的升级版游戏. 升级版游戏在传统的 ...

  4. 10.1综合强化刷题 Day2

    a[问题描述]你是能看到第一题的 friends呢.                                                —— hja世界上没有什么比卖的这 贵弹丸三还令人绝 ...

  5. ScutSDK 0.9版本发布

    ScutSDK简介: ScutSDK是和Scut游戏服务器引擎,简化客户端开发的配套SDK,她彻底打通了Scut开源游戏服务器引擎与客户端引擎(如Cocos2d-x/Quick-x/Unity3D)项 ...

  6. [置顶] python字典和nametuple互相转换例子

    如果tuple中的元素很多的时候操作起来就比较麻烦,有可能会由于索引错误导致出错. namedtuple对象给tuple命名. 下面的例子可以字典和nametuple互相转换 aa={'verbosi ...

  7. MFC中 获取新输入编辑框的内容

    //得到原始内容的长度 int len = m_editPoemFileStr.GetLength(); UpdateData(true); //得到新增加的内容 CString  sNewStrin ...

  8. Android--数据库数据显示至屏幕

    MainActivity.java 这段代码的作用是从数据库中获取到数据并显示在界面上 import java.util.ArrayList; import java.util.List; impor ...

  9. Oracle无安装客户端安装方法

    一. 1)下载Oracle客户端:http://www.oracle.com/technetwork/database/features/instant-client/index-097480.htm ...

  10. 思维探索者:完善个人知识体系的重要性 Google只会告诉你结果

    http://www.nowamagic.net/librarys/veda/detail/1711前面说了,人类解决问题大部分时候会习惯性地使用联想思维,简言之就是首先枚举你关于这个问题能够想到的所 ...