Til the Cows Come Home

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
//dijkstra
 #include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#define maxn 1005
#define ms(x,n) memset(x,n,sizeof x);
const int inf=0x3f3f3f3f;
using namespace std;
int n,t;
int u,v,w;
int cost[][];
int d[];
bool vis[];
void dij(int s)
{
int i,j;
ms(vis,);
memset(d,0x3f,sizeof d);
d[s]=;
for(i=;i<=n;i++)
{
int p=inf,e=-;
for(j=;j<=n;j++)
{
if(!vis[j]&&d[j]<p)
{
p=d[j];
e=j;
}
}
if(e==-)return;
vis[e]=;
for(j=;j<=n;j++)
{
if(!vis[j]&&d[j]>cost[e][j]+d[e])
{d[j]=cost[e][j]+d[e];
}
}
}
}
int main()
{
int i,j;
cin>>t>>n;
for(i=;i<=n;i++)
for(j=;j<=n;j++)
if(i!=j)
cost[i][j]=inf;
else if(i==j)
cost[i][j]=;
for(i=;i<t;i++)
{
cin>>u>>v>>w;
cost[u][v]=cost[v][u]=min(cost[u][v],w);
}
dij();
cout<<d[n];
}
//dijkstra堆优化
 #include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#define maxn 1005
#define ms(x,n) memset(x,n,sizeof x);
const int inf=0x3f3f3f3f;
using namespace std;
int n,t;
int u,v,w;
int cost[][];
int d[];
bool vis[];
typedef pair<int,int> p;//cost[],点的编号
vector<p>g[maxn];
void dij(int s)
{
ms(vis,);
ms(d,0x3f);
d[s]=;
priority_queue<p,vector<p>,greater<p> >q;
q.push(p(d[s],s));
while(!q.empty())
{
p cur=q.top();
q.pop();
u=cur.second;
//if(cur.first<d[u])continue;
int sz=g[u].size();
for(int i=;i<sz;i++)
{
v=g[u][i].second;
w=g[u][i].first;
if(d[v]>d[u]+w)
{d[v]=d[u]+w;
q.push(p(d[v],v));
}
}
}
}
int main()
{
int i;
cin>>t>>n;
for(i=;i<n;i++)
g[i].clear();
for(i=;i<t;i++)
{
cin>>u>>v>>w;
u--,v--;
g[u].push_back(p(w,v));
g[v].push_back(p(w,u));
}
dij();
cout<<d[n-];
}

//可以看出时空复杂度的明显差异

POJ2387(dijkstra堆优化)的更多相关文章

  1. POJ 2502 - Subway Dijkstra堆优化试水

    做这道题的动机就是想练习一下堆的应用,顺便补一下好久没看的图论算法. Dijkstra算法概述 //从0出发的单源最短路 dis[][] = {INF} ReadMap(dis); for i = 0 ...

  2. Bzoj 2834: 回家的路 dijkstra,堆优化,分层图,最短路

    2834: 回家的路 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 62  Solved: 38[Submit][Status][Discuss] D ...

  3. hdu 2544 单源最短路问题 dijkstra+堆优化模板

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  4. 深入理解dijkstra+堆优化

    深入理解dijkstra+堆优化 其实就这几种代码几种结构,记住了完全就可以举一反三,所以多记多练多优化多思考. Dijkstra   对于一个有向图或无向图,所有边权为正(边用邻接矩阵的形式给出), ...

  5. dijkstra堆优化(multiset实现->大大减小代码量)

    例题: Time Limit: 1 second Memory Limit: 128 MB [问题描述] 在电视时代,没有多少人观看戏剧表演.Malidinesia古董喜剧演员意识到这一事实,他们想宣 ...

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

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

  7. dijkstra算法的应用(poj2387)+堆优化【还没学C艹很尴尬,不理解的先不写了,未完,待续...】

    一题非常简单的最短路题目,但是我就是很撒比的错在了,1.初始化:2.判断重边 堆优化,使用优先队列的堆优化:复杂度:O(ElogE); #include <stdio.h> #includ ...

  8. POJ 1511 - Invitation Cards 邻接表 Dijkstra堆优化

    昨天的题太水了,堆优化跑的不爽,今天换了一个题,1000000个点,1000000条边= = 试一试邻接表 写的过程中遇到了一些问题,由于习惯于把数据结构封装在 struct 里,结果 int [10 ...

  9. Dijkstra堆优化学习

    最短路径例题 今天特地学习了Dijkstra的堆优化(主要是慕名已久). 我们需要一个堆来记录[编号,到编号这个点的最短路径值(当然只是当前的)] 与原来的Dijkstra操作基本一致,主要有以下几点 ...

随机推荐

  1. Nhibernate学习的第二天

    Fluent-Nhibernate   网站:http://www.fluentnhibernate.org/ 使用NuGet下载Fluent-Nhibernate. 1.创建实体类 2.创建实体类映 ...

  2. 2018-01-19 Xtext试用: 5步实现一个(中文)JVM语言

    续上文Xtext试用: 快速实现简单领域专用语言(DSL). 基于官方教程: Five simple steps to your JVM language 达成如下语言: 它被Quan6JvmMode ...

  3. 【读书笔记】iOS-优化内存

    imageNamed:方法创建UIImage对象,这些对象不再使用的时候 会放到应用的默认自动回收池中,而不是当前的事件循环的自动回收池中,这样的对象占用的内存只有在应用结束的时候 才会回收.如果用这 ...

  4. web全栈架构师[笔记] — 02 数据交互

    数据交互 一.http协议 基本特点 1.无状态的协议 2.连接过程:发送连接请求.响应接受.发送请求 3.消息分两块:头.体 http和https 二.form 基本属性 action——提交到哪儿 ...

  5. ScrollView与ListView的事件冲突

    布局文件 当ListView嵌套在ScrollView中时,会发生冲突,导致ListView控件的拉动效果消失‘ 解决办法: 重写ListView的onTouchEvent(),并在返回前调用getP ...

  6. 安卓开发----TextView控件属性列表(转)

    文章原地址: http://wwzcraig.blog.163.com/blog/static/64622969201373184343118/ android:autoLink设置是否当文本为URL ...

  7. 《Inside C#》笔记(六) 属性、数组、索引器

    一 属性 a) 属性可用于隐藏类的内部成员,对外提供可控的存取接口.属性相当于有些语言的getter.setter方法,只是使用起来更加方便一点,而且查看对应的IL码可以看到,属性的本质也确实是方法. ...

  8. [Objective-C] 创建常量

    新博客wossoneri.com #define宏定义 #define是一条预编译指令, 编译器在编译阶段前期会将所有使用到宏的地方简单地进行替换. 在预处理器里进行文本替换,没有类型,不做任何类型检 ...

  9. 【Java入门提高篇】Day21 Java容器类详解(四)ArrayList源码分析

    今天要介绍的是List接口中最常用的实现类——ArrayList,本篇的源码分析基于JDK8,如果有不一致的地方,可先切换到JDK8后再进行操作. 本篇的内容主要包括这几块: 1.源码结构介绍 2.源 ...

  10. redis介绍 (8) window 下redis的集群(cluster命令)

    前言: 前段时间我在centos上搭建过一次redis集群,那是借助ruby搭建,这次我介绍一种纯redis集群命令的方式去搭建[最后我会简单介绍ruby搭建]. redis集群搭建(三主三备): 准 ...