hdu 1874 dijkstra 队列实现 比数组高效特别在稀疏图
参考 http://blog.csdn.net/zhuyingqingfen/article/details/6370561
刘汝佳白皮书
#include<stdio.h>
#include<queue>
#include<iostream>
#include<vector>
using namespace std;
#define N 2100
#define inf 1000000000
int n,m;
struct node {
int u,v,w,next;
}bian[N];
int yong,head[N];
void creat(int u,int v,int w) {
bian[yong].u=u;
bian[yong].v=v;
bian[yong].w=w;
bian[yong].next=head[u];
head[u]=yong++;
}
void Dcreat(int u,int v,int w) {
creat(u,v,w);
creat(v,u,w);
}
int dijkstra(int u,int end) {
int dis[N],i,j,visit[N];
memset(visit,0,sizeof(visit));
for(i=0;i<n;i++)
dis[i]=inf;
dis[u]=0;
typedef pair<int ,int >p;//pair 定义了自己的排序规则--先比较第一维,相等才比较第二维
priority_queue<p,vector<p>,greater<p> >q;
q.push(make_pair(dis[u],u));
while(!q.empty()) {
p cur=q.top();
j=cur.second;
q.pop();
if(visit[j])continue;
visit[j]=1;
for(i=head[j];i!=-1;i=bian[i].next)
if(dis[bian[i].v]>dis[j]+bian[i].w) {
dis[bian[i].v]=dis[j]+bian[i].w;
q.push(make_pair(dis[bian[i].v],bian[i].v));
}
}
if(dis[end]<inf)
return dis[end];
return -1;
}
int main() {
int i,j,a,b;
while(scanf("%d%d",&n,&m)!=EOF) {
yong=0;
memset(head,-1,sizeof(head));
while(m--) {
scanf("%d%d%d",&a,&b,&j);
Dcreat(a,b,j);
}
scanf("%d%d",&a,&b);
printf("%d\n",dijkstra(a,b));
}
return 0;
}
//定义结构体优先队列
#include<stdio.h>
#include<queue>
#include<iostream>
#include<vector>
using namespace std;
#define N 2100
#define inf 1000000000
int n,m;
struct node {
int u,v,w,next;
}bian[N];
int yong,head[N];
void creat(int u,int v,int w) {
bian[yong].u=u;
bian[yong].v=v;
bian[yong].w=w;
bian[yong].next=head[u];
head[u]=yong++;
}
void Dcreat(int u,int v,int w) {
creat(u,v,w);
creat(v,u,w);
}
struct nodee {
int len,v;
friend bool operator<(nodee a,nodee b) {
if(a.len!=b.len)
return a.len>b.len;
return a.v>b.v;
}
};
int dijkstra(int u,int end) {
int dis[N],i,j,visit[N];
memset(visit,0,sizeof(visit));
for(i=0;i<n;i++)
dis[i]=inf;
dis[u]=0;
//typedef pair<int ,int >p;
//priority_queue<p,vector<p>,greater<p> >q;
// q.push(make_pair(dis[u],u));
priority_queue<nodee>q;
nodee cur,now;
cur.len=0;
cur.v=u;
q.push(cur);
while(!q.empty()) {
nodee cur=q.top();
// j=cur.second;
q.pop();
if(visit[cur.v])continue;
visit[cur.v]=1;
for(i=head[cur.v];i!=-1;i=bian[i].next)
if(dis[bian[i].v]>dis[cur.v]+bian[i].w) {
dis[bian[i].v]=dis[cur.v]+bian[i].w;
now.len=dis[bian[i].v];
now.v=bian[i].v;
q.push(now);
}
}
if(dis[end]<inf)
return dis[end];
return -1;
}
int main() {
int i,j,a,b;
while(scanf("%d%d",&n,&m)!=EOF) {
yong=0;
memset(head,-1,sizeof(head));
while(m--) {
scanf("%d%d%d",&a,&b,&j);
Dcreat(a,b,j);
}
scanf("%d%d",&a,&b);
printf("%d\n",dijkstra(a,b));
}
return 0;
}
hdu 1874 dijkstra 队列实现 比数组高效特别在稀疏图的更多相关文章
- hdu 1874 Dijkstra算法
先贴个网上找的比较通俗易懂的教程: 2.1Dijkstra算法(非负权,使用于有向图和无向图) Dijkstra算法是典型最短路算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心 ...
- ACM: HDU 1874 畅通工程续-Dijkstra算法
HDU 1874 畅通工程续 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Desc ...
- HDU 1874 畅通工程续-- Dijkstra算法详解 单源点最短路问题
参考 此题Dijkstra算法,一次AC.这个算法时间复杂度O(n2)附上该算法的演示图(来自维基百科): 附上: 迪科斯彻算法分解(优酷) problem link -> HDU 1874 ...
- (重刷)HDU 1874 畅通工程续 + HDU 2544 最短路 最短路水题,dijkstra解法。
floyd解法 今天初看dijkstra,先拿这两题练手,其他变形题还是不是很懂. 模版题,纯练打字... HDU 1874: #include <cstdio> #define MAXN ...
- HDU 1874 畅通工程续(初涉dijkstra算法实现)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 dijkstra算法实现可参照此博客学习:http://www.cnblogs.com/biye ...
- hdu 2544 hdu 1874 poj 2387 Dijkstra 模板题
hdu 2544 求点1到点n的最短路 无向图 Sample Input2 1 //结点数 边数1 2 3 //u v w3 31 2 52 3 53 1 20 0 Sample Output32 ...
- HDU 2544 最短路(floyd+bellman-ford+spfa+dijkstra队列优化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目大意:找点1到点n的最短路(无向图) 练一下最短路... dijkstra+队列优化: #i ...
- hdu 1874 畅通工程续(迪杰斯特拉优先队列,floyd,spfa)
畅通工程续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- hdoj 1874 dijkstra
在做PAT的甲1003,思考DFS和图什么的,时间紧张直接去看柳神(日后上传柳神的C++版本)的订阅,得知是dijkstra,转去用hdoj 1874练手,写了两天,终于调出来了 题目链接:http: ...
随机推荐
- 使用particles.js实现网页背景粒子特效
得知途径 B3log提供了两套博客系统,一个是用Java开发的,叫做Solo,我也是在网上搜索Java博客系统时发现了它,之后才了解了B3log:还有一个是用Go语言开发的,叫做Pipe.其中Solo ...
- 源码阅读之LinkedList(JDK8)
inkedList概述 LinkedList是List和Deque接口的双向链表的实现.实现了所有可选列表操作,并允许包括null值. LinkedList既然是通过双向链表去实现的,那么它可以被当作 ...
- jQuery——表单应用(2)
多行文本框应用之高度变化 HTML: <!--表单-多行文本框应用-高度变化--> <!DOCTYPE html> <html> <head> < ...
- 聊聊MyBatis缓存机制
https://tech.meituan.com/mybatis_cache.html 前言 MyBatis是常见的Java数据库访问层框架.在日常工作中,开发人员多数情况下是使用MyBatis的默认 ...
- CF817A Treasure Hunt
思路: 起点(x1, y1),终点(x2, y2),步长(dx, -dy),(dx, dy),(-dx, -dy),(-dx, dy).只要满足abs(x1 - x2) % dx == 0 并且 ab ...
- 2017-12-04HTML table布局
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 微信服务号获取openId流程(订阅号)
微信公众平台官网:https://mp.weixin.qq.com/ 微信测试开发平台官网:https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandb ...
- redis的安装、启动、主从配置,以及.Net下StackExchange.Redis的使用
开门见山,Linux下配个环境真是苦逼死了,这里记录一下,囧 一.环境 服务端:Ubuntu16.04 LTS(虚拟机,redis官方没有window发布版本,而且在Linux下运行更稳定) 客户端: ...
- 【译】x86程序员手册22-6.4页级保护
6.4 Page-Level Protection 页级保护 Two kinds of protection are related to pages: 与页相关的保护有两类: Restriction ...
- vue-element-admin使用常见问题
一.vue-element-admin添加快捷导航 这个组件是基于vue-i18n因此,首先在项目中安装i18n npm install --save vue-i18n 然后main.js中引入 im ...