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. FFT(1)

    FFT Complex struct complex{ double re,im; complex(double r,double i){re=r,im=i;} complex(){re=0.0,im ...

  2. 已知局域网IP地址,如何查看mac

    arp -a 加对方IP是查对方的MAC地址 转自: http://zhidao.baidu.com/link?url=8sRdpGcjfGQ-C1F9zNub49Mxe3DAR-RCAHDkHvKC ...

  3. 【Python】Django Model 怎么使用 UUID 作为主键?

    >>> import uuidprint uuid.uuid3(uuid.uuid1(), 'python.org') >>> # make a UUID base ...

  4. OAuth

    http://oauth.net http://oauth.net/2/ http://tools.ietf.org/html/rfc6749 人人网:http://wiki.dev.renren.c ...

  5. Js document.frmLogin.action = '/login.htm';的意义和form表单的target属性

    一.解答:就是把 这个id名为frmLogin的form的提交地址改为上面的/login.htm <form id="frmLogin" name="frmLogi ...

  6. Android 使用dip单位进行布局的一点知识

    先看看怎么算出一个设备的dpi, 其实就是算出对角线上有多少个px,之后除上屏幕尺寸.比如,1280*720 的10.1寸设备,dpi = (sqrt(1280*1280+720*720))  / 1 ...

  7. 解决虚拟机 正在决定eht0 的ip信息失败 无链接-- 虚拟机上linux redhat 上网问题

    对于虚拟机上,linux redhat上网的配置方式有三种 一.用setup命令进行配置(具体技巧可查setup命令的使用) 二.直接用 ifconfig eth0  ip地址进行配置 三.进入系统文 ...

  8. python文件取MD5

    import hashlib def md5sum(filename, blocksize=65536): hash = hashlib.md5() with open(filename, " ...

  9. [SVN(ubuntu)] svn 文件状态标记含义

    A item 文件.目录或是符号链item预定加入到版本库. C item 文件item发生冲突,在从服务器更新时与本地版本发生交迭,在你提交到版本库前,必须手工的解决冲突. D item 文件.目录 ...

  10. Paths on a Grid(poj 1942)

    给定一个矩形网格的长m和高n,其中m和n都是unsigned int32类型,一格代表一个单位,就是一步,求从左下角到右上角有多少种走法,每步只能向上或者向右走. //注意循环的时候,要循环小的数,否 ...