POJ2387(dijkstra堆优化)
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
* 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
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堆优化)的更多相关文章
- POJ 2502 - Subway Dijkstra堆优化试水
做这道题的动机就是想练习一下堆的应用,顺便补一下好久没看的图论算法. Dijkstra算法概述 //从0出发的单源最短路 dis[][] = {INF} ReadMap(dis); for i = 0 ...
- Bzoj 2834: 回家的路 dijkstra,堆优化,分层图,最短路
2834: 回家的路 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 62 Solved: 38[Submit][Status][Discuss] D ...
- hdu 2544 单源最短路问题 dijkstra+堆优化模板
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- 深入理解dijkstra+堆优化
深入理解dijkstra+堆优化 其实就这几种代码几种结构,记住了完全就可以举一反三,所以多记多练多优化多思考. Dijkstra 对于一个有向图或无向图,所有边权为正(边用邻接矩阵的形式给出), ...
- dijkstra堆优化(multiset实现->大大减小代码量)
例题: Time Limit: 1 second Memory Limit: 128 MB [问题描述] 在电视时代,没有多少人观看戏剧表演.Malidinesia古董喜剧演员意识到这一事实,他们想宣 ...
- POJ-2387.Til the Cows Come Home.(五种方法:Dijkstra + Dijkstra堆优化 + Bellman-Ford + SPFA + Floyd-Warshall)
昨天刚学习完最短路的算法,今天开始练题发现我是真的菜呀,居然能忘记邻接表是怎么写的,真的是菜的真实...... 为了弥补自己的菜,我决定这道题我就要用五种办法写出,并在Dijkstra算法堆优化中另外 ...
- dijkstra算法的应用(poj2387)+堆优化【还没学C艹很尴尬,不理解的先不写了,未完,待续...】
一题非常简单的最短路题目,但是我就是很撒比的错在了,1.初始化:2.判断重边 堆优化,使用优先队列的堆优化:复杂度:O(ElogE); #include <stdio.h> #includ ...
- POJ 1511 - Invitation Cards 邻接表 Dijkstra堆优化
昨天的题太水了,堆优化跑的不爽,今天换了一个题,1000000个点,1000000条边= = 试一试邻接表 写的过程中遇到了一些问题,由于习惯于把数据结构封装在 struct 里,结果 int [10 ...
- Dijkstra堆优化学习
最短路径例题 今天特地学习了Dijkstra的堆优化(主要是慕名已久). 我们需要一个堆来记录[编号,到编号这个点的最短路径值(当然只是当前的)] 与原来的Dijkstra操作基本一致,主要有以下几点 ...
随机推荐
- Python 类的特殊成员介绍
类的成员有两种形式 公有成员,在任何地方都能访问 私有成员,只有在类的内部才能方法,私有成员命名时,前两个字符是下划线. class Foo: def __init__(self, name, age ...
- html常用标签整理
html文档结构 <!DOCTYPE html> <html lang="zh-CN"> #这个lang表示语言,zh-CN中文的意思,整个文档的内容以中文 ...
- 【代码笔记】Web-ionic-卡片
一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- 【读书笔记】iOS-加速计与陀螺仪
一,数据的“滤波” 直接从加速度计获得的原始数据,往往不能直接使用,而是需要去除一些干扰数据,这个过程称为“滤波”.“滤波”一词来源于无线电技术中对无线电信号的处理过程.事实上从数学角度而言它们是一样 ...
- Spring security实现国际化问题
这两天Spring用户登录国际化这个问题困扰我好久啊,于昨天晚上终于把它干掉了. 场景就是我们公司的产品-incopat,需要支持中英文,用户登录这块用的spring自带的security,需求讲的通 ...
- persist与checkpoint
1.当反复使用某些RDD时建议使用persist(缓存级别)(采用默认缓存级别时为cache())来对数据进行缓存. 2.如果某个步骤的RDD计算特别耗时或经历很多步骤的计算,当重新计算时代价特别大, ...
- C#语言————第三章 使用属性升级MyBank
********常见的访问修饰符*********: public :公共的,可以在其他类中访问 private:私有的,只有在本类里可以使用,其他的类无权访问 类的默认访问修饰符 internal( ...
- .net core HttpContext(Http上下文)
在.NET Core中,只有Controller才能直接使用 HttpContext ,其他地方需要通过HttpContextAccessor来访问
- dumpe2fs 命令的使用,转储 ext2/ext3/ext4 文件系统信息
使用man 命令可以查看 dumpe2fs 命令具体的使用的方法: NAME dumpe2fs - dump ext2/ext3/ext4 filesystem information SYNOPSI ...
- Selenium 、WebDriver :Capability
Selenium | WebDriver Capability 内容摘要: 1.WebDriver 通用配置 2.RemoteWebDriver特有配置 3.Grid特有配置 4.在使用特定浏览器时的 ...