Dijkstra+优先队列

#include<cstdio>
#include<cctype>
#include<queue>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
inline int read() {
int x=,f=;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-;
for(;isdigit(c);c=getchar()) x=x*+c-'';
return x*f;
}
const int maxn=;
const int maxm=;
struct Dijkstra {
int n,m,first[maxn],next[maxm],done[maxn],d[maxn];
struct Edge {int from,to,dist;}edges[maxm];
struct HeapNode {
int d,u;
bool operator < (const HeapNode& ths) const {return d>ths.d;}
};
void init(int n) {
this->n=n;m=;
memset(first,,sizeof(first));
}
void AddEdge(int u,int v,int w) {
edges[++m]=(Edge){u,v,w};next[m]=first[u];first[u]=m;
}
void solve(int s) {
priority_queue<HeapNode> Q;
memset(done,,sizeof(done));
for(int i=;i<=n;i++) d[i]=;
d[s]=;Q.push((HeapNode){,s});
while(!Q.empty()) {
int x=Q.top().u;Q.pop();
if(done[x]) continue;done[x]=;
for(int i=first[x];i;i=next[i]) {
Edge& e=edges[i];
if(d[e.to]>d[x]+e.dist) {
d[e.to]=d[x]+e.dist;
Q.push((HeapNode){d[e.to],e.to});
}
}
}
}
}sol;
int main() {
int n=read(),m=read();sol.init(n);
for(int i=;i<=m;i++) {
int u=read(),v=read(),w=read();
sol.AddEdge(u,v,w);
}
sol.solve();
for(int i=;i<=n;i++) printf("%d ",sol.d[i]);
return ;
}
/*
7 8
1 3 5
1 4 2
1 5 1
4 3 2
3 6 2
4 6 3
6 7 3
5 7 1
*/

SPFA

#include<cstdio>
#include<cctype>
#include<queue>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
inline int read() {
int x=,f=;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-;
for(;isdigit(c);c=getchar()) x=x*+c-'';
return x*f;
}
const int maxn=;
const int maxm=;
struct SPFA {
int n,m,first[maxn],next[maxm],inq[maxn],d[maxn];
struct Edge {int from,to,dist;}edges[maxm];
void init(int n) {
this->n=n;m=;
memset(first,,sizeof(first));
memset(inq,,sizeof(inq));
}
void AddEdge(int u,int v,int w) {
edges[++m]=(Edge){u,v,w};next[m]=first[u];first[u]=m;
}
void solve(int S) {
queue<int> Q;Q.push(S);
for(int i=;i<=n;i++) d[i]=;d[S]=;
while(!Q.empty()) {
int u=Q.front();Q.pop();inq[u]=;
for(int i=first[u];i;i=next[i]) {
Edge& e=edges[i];
if(d[e.to]>d[u]+e.dist) {
d[e.to]=min(d[e.to],d[u]+e.dist);
if(!inq[e.to]) inq[e.to]=,Q.push(e.to);
}
}
}
}
}sol;
int main() {
int n=read(),m=read();sol.init(n);
for(int i=;i<=m;i++) {
int u=read(),v=read(),w=read();
sol.AddEdge(u,v,w);
}
sol.solve();
for(int i=;i<=n;i++) printf("%d ",sol.d[i]);
return ;
}
/*
7 8
1 3 5
1 4 2
1 5 1
4 3 2
3 6 2
4 6 3
6 7 3
5 7 1
*/

SPFA优化(还是有些用的)

#include<cstdio>
#include<cctype>
#include<queue>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
inline int read() {
int x=,f=;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-;
for(;isdigit(c);c=getchar()) x=x*+c-'';
return x*f;
}
const int maxn=;
const int maxm=;
struct SPFA {
int n,m,first[maxn],next[maxm],inq[maxn],d[maxn];
struct Edge {int from,to,dist;}edges[maxm];
void init(int n) {
this->n=n;m=;
memset(first,,sizeof(first));
memset(inq,,sizeof(inq));
}
void AddEdge(int u,int v,int w) {
edges[++m]=(Edge){u,v,w};next[m]=first[u];first[u]=m;
}
void solve(int S) {
deque<int> Q;Q.push_back(S);
for(int i=;i<=n;i++) d[i]=;d[S]=;
while(!Q.empty()) {
int u=Q.front();Q.pop_front();inq[u]=;
for(int i=first[u];i;i=next[i]) {
Edge& e=edges[i];
if(d[e.to]>d[u]+e.dist) {
d[e.to]=min(d[e.to],d[u]+e.dist);
if(!inq[e.to]) {
inq[e.to]=;
if(!Q.empty()&&d[e.to]<d[Q.front()]) Q.push_front(e.to);
else Q.push_back(e.to);
}
}
}
}
}
}sol;
int main() {
int n=read(),m=read();sol.init(n);
for(int i=;i<=m;i++) {
int u=read(),v=read(),w=read();
sol.AddEdge(u,v,w);
}
sol.solve();
for(int i=;i<=n;i++) printf("%d ",sol.d[i]);
return ;
}
/*
7 8
1 3 5
1 4 2
1 5 1
4 3 2
3 6 2
4 6 3
6 7 3
5 7 1
*/

Dijkstra堆优化与SPFA模板的更多相关文章

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

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

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

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

  3. 深入理解dijkstra+堆优化

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

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

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

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

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

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

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

  7. POJ2387(dijkstra堆优化)

    Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to get as much s ...

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

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

  9. hdu3790 dijkstra+堆优化

    题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=3790 分析:dijkstra没有优化的话,复杂度是n*n,优化后的复杂度是m*logm,n是顶点数,m ...

随机推荐

  1. django-cms 代码研究(七)杂七杂八

    实体关系图 核心对象: cms_page/cms_placeholder/cms_cmsplugin. page模型类继承关系图 CMSPlugin&Placeholder模型类继承关系图 = ...

  2. grep与egrep

    当只有一个匹配条件时:egrep pattern file等价于grep -E pattern file 例如: 当多个匹配条件时,只能用egrep -e pattern1 -e pattern2 - ...

  3. PHP面向对象三大特点学习(充分理解抽象、封装、继承、多态)

    PHP面向对象三大特点学习 学习目标:充分理解抽象.封装.继承.多态   面象对向的三大特点:封装性.继承性.多态性 首先简单理解一下抽象:我们在前面定义一个类的时候,实际上就是把一类事物共有的属性和 ...

  4. How can I pretty-print JSON in python?

    python -m json.tool my_json.json 转自: http://stackoverflow.com/questions/352098/how-can-i-pretty-prin ...

  5. /lib /usr/lib /usr/local/lib区别

    昨天问我/usr/lib 和/usr/local/lib 我仅记得一个是系统的,一个是用户的,于是今天查了查,有两篇文章介绍的不错,usr 很多人都认为是user缩写,其实不然,是unix syste ...

  6. maven3 junit4 spring3 jdk8 :junit一直报错,害的我几个星期都是这个错,你妹的!

    [org.springframework.test.context.junit4.SpringJUnit4ClassRunner]SpringJUnit4ClassRunner constructor ...

  7. sqlserver的执行计划

    一:执行计划生成过程 说到执行计划,首先要知道的是执行计划大概生成的过程,这样就可以做到就心中有数了,下面我画下简图: 1. 分析过程 这三个比较容易理解,首先我们要保证sql的语法不能错误,sele ...

  8. July 26th, Week 31st Tuesday, 2016

    The best preparation for tomorrow is doing your best today. 对明天最好的准备就是今天做到最好. The road toward tomorr ...

  9. Ruby备忘

  10. 引水入城(codevs 1066)

    题目描述 Description 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政 区划十分特殊,刚好构成一个N行M列的矩形,如上图所示,其中每个格子都代表一座城 市,每座 ...