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. 16.O(logn)求Fibonacci数列[Fibonacci]

    [题目] log(n)时间Fib(n),本质log(n)求a^n. [代码]  C++ Code  12345678910111213141516171819202122232425262728293 ...

  2. mybatis There is no getter for property named 'xx' in 'class java.lang.String

    转载自://http://www.cnblogs.com/anee/p/3324140.html 用mybatis查询时,传入一个字符串传参数,且进行判断时,会报 There is no getter ...

  3. mybatis异常:Improper inline parameter map format. Should be: #{propName,attr1=val1,attr2=val2}问题分析及解决

    转载自:http://blog.csdn.net/jackpk/article/details/44158701 mybatis异常:Improper inline parameter map for ...

  4. 百度编辑器 ueditor .net开发

    ueditor1.4.3 下载地址:http://pan.baidu.com/s/1bnCQVtd   <!--editor--> <script type="text/j ...

  5. android之WakeLock机制浅析

    转自:http://blog.sina.com.cn/s/blog_4ad7c2540101n2k2.html 应用程序耗电的实质,是所启用的硬件在消耗电量.  手机的耗电单元 CPU: 应用处理器( ...

  6. device unauthorized & ANDROID_ADB_SERVER_PORT 问题解决

    最近做安卓开发的时候,碰到了如下两个极品问题,刚开始晕头转向,仔细一研究终于解决了: FAQ1: C:\Users\xxx>adb shelladb: Env var ANDROID_ADB_S ...

  7. nginx增加ssl服务方法

    1.将申请到的ssl加密证书文件拷贝到nginx的conf目录下 如:server.pem.server.key 2.vim nginx.conf 例子: server { listen 443 ss ...

  8. Diskpart命令安装系统小结

    <diskpart命令安装系统小结> 今天给同学安装系统,win8改win7.同学是预装了win8的联想y480,分区表采用的是GPT格式,捣鼓了半天才知道.GPT格式是新式的分区格式,相 ...

  9. CRT注册工具使用说明

    激活步骤如下:   1)准备工作:安装好SecureCRT软件,下载并得到该注册机.   2)保持SecureCRT软件关闭(运行的话会提示你正在运行的,关闭就好).   3)将注册机拷贝到你的CRT ...

  10. wp8 入门到精通 线程

    Dispatcher.BeginInvoke(() => MessageBox.Show(String.Format("A push notification {0} error oc ...