dijkstra,SPFA,Floyd求最短路
Dijkstra:
裸的算法,O(n^2),使用邻接矩阵:
算法思想:
定义两个集合,一开始集合1只有一个源点,集合2有剩下的点。
STEP1:在集合2中找一个到源点距离最近的顶点k:min{d[k]}
STEP2:把顶点k加入集合1中,同时修改集合2中的剩余顶点j的d[j]是否经过k之后变短,若变短则修改d[j];
if d[k]+a[k,j]<d[j] then d[j]=d[k]+a[k,j];
STEP3:重复STEP1,直到集合2为空为止。
#include <iostream>
#include <cstring>
using namespace std;
#define MAXINT 9999999 int minx,minj,x,y,t,k,n,m,tmp;
int v[],d[],a[][]; int main()
{
cin>>n>>m>>k;
memset(a,,sizeof(a));
memset(d,MAXINT,sizeof(d));
memset(v,,sizeof(v));
d[k]=;
for (int i=;i<=m;i++)
{
cin>>x>>y>>t;
a[x][y]=t;
a[y][x]=t;
} for (int i=;i<=n-;i++)
{
minx=MAXINT;
for (int j=;j<=n;j++)
if ((v[j]==)&&(d[j]<minx))
{
minx=d[j];
minj=j;
}
v[minj]=;
for (int j=;j<=n;j++)
if ((v[j]==)&&(a[minj][j]>))
{
tmp=d[minj]+a[minj][j];
if (tmp<d[j]) d[j]=tmp;
}
} for (int i=;i<=n;i++)
cout<<d[i]<<" ";
cout<<endl; return ;
}
Tips:上述STEP1可以用优先队列优化。
模版:
(使用邻接表,Reference:http://www.cnblogs.com/qijinbiao/archive/2012/10/04/2711780.html)
eg:邻接表
i:某条边的起点 eg[i][j].x:从点i出发的第j条边的终点 eg[i][j].d:这条边的距离
#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
const int Ni = ;
const int INF = <<;
struct node{
int x,d;
node(){}
node(int a,int b){x=a;d=b;}
bool operator < (const node & a) const
{
if(d==a.d) return x<a.x;
else return d > a.d;
}
};
vector<node> eg[Ni];
int dis[Ni],n;
void Dijkstra(int s)
{
int i;
for(i=;i<=n;i++) dis[i]=INF;
dis[s]=;
priority_queue<node> q;
q.push(node(s,dis[s]));
while(!q.empty())
{
node x=q.top();q.pop();
for(i=;i<eg[x.x].size();i++)
{
node y=eg[x.x][i];
if(dis[y.x]>x.d+y.d)
{
dis[y.x]=x.d+y.d;
q.push(node(y.x,dis[y.x]));
}
}
}
}
int main()
{
int a,b,d,m,k;
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=n;i++) eg[i].clear();
while(m--)
{
scanf("%d%d%d",&a,&b,&d);
eg[a].push_back(node(b,d));
eg[b].push_back(node(a,d));
}
Dijkstra(k); for (int i=;i<=n;i++)
printf("%d\n",dis[i]); return ;
}
STL优先队列Reference:http://www.cnblogs.com/wanghetao/archive/2012/05/22/2513514.html
SPFA:
即用队列优化过的Bellman-Ford
( Reference:http://blog.csdn.net/niushuai666/article/details/6791765 )
Pascal代码...
var q,d:array[..] of longint;
a:array[..,..] of longint;
visited:array[..] of boolean;
head,tail,s,n,dt,i,j:longint; begin
assign(input,'spfa.in');
reset(input); fillchar(d,sizeof(d), div );
fillchar(visited,sizeof(visited),false);
fillchar(a,sizeof(a),); readln(s);
d[s]:=;
readln(n);
for i:= to n do
for j:= to n do
begin
read(dt);
a[i,j]:=dt;
a[j,i]:=dt;
{ if dt<>0 then
begin
if i=s then d[j]:=dt;
if j=s then d[i]:=dt;
end;
}
end; head:=;
q[]:=s;
visited[s]:=true;
tail:=;
while head<tail do
begin
inc(head);
visited[q[head]]:=false;
for i:= to n do
begin
if (a[q[head],i]>) and (d[q[head]]+a[q[head],i]<d[i]) then
begin
d[i]:=d[q[head]]+a[q[head],i];
if not visited[i] then
begin
inc(tail);
q[tail]:=i;
visited[i]:=true;
end;
end;
end;
end; for i:= to n do
write(d[i],' ');
writeln; close(input);
end.
Floyd:
多源最短路
//path[i,j]:用来输出最短路径
//floyd
var path,d:array[..,..] of longint;
n,k,i,j,st,en,x,y,tmp:longint; procedure dfs(i,j:longint);
begin
if path[i,j]> then
begin
dfs(i,path[i,j]);
write(path[i,j],'->');
dfs(path[i,j],j);
end;
end; begin
assign(input,'floyd.in');
reset(input); fillchar(d,sizeof(d), div ); readln(n); for i:= to n do d[i,i]:=;
for i:= to n do
for j:= to n do
path[i,j]:=-; readln(st,en);
while not eof do
begin
readln(x,y,tmp);
d[x,y]:=tmp;
d[y,x]:=tmp;
path[x,y]:=;
path[y,x]:=;
end; for k:= to n do
for i:= to n do
for j:= to n do
begin
if d[i,k]+d[k,j]<d[i,j] then
begin
d[i,j]:=d[i,k]+d[k,j];
path[i,j]:=k;
end;
end; writeln(d[st,en]); write(st,'->');
dfs(st,en);
writeln(en); close(input);
end.
dijkstra,SPFA,Floyd求最短路的更多相关文章
- 最短路算法详解(Dijkstra/SPFA/Floyd)
新的整理版本版的地址见我新博客 http://www.hrwhisper.me/?p=1952 一.Dijkstra Dijkstra单源最短路算法,即计算从起点出发到每个点的最短路.所以Dijkst ...
- Floyd 求最短路(poj 1161)
Floyd-Warshall算法介绍: Floyd-Warshall算法的原理是动态规划. 设为从到的只以集合中的节点为中间节点的最短路径的长度. 若最短路径经过点k,则: 若最短路径不经过点k,则. ...
- hdu1869六度分离,spfa实现求最短路
就是给一个图.假设随意两点之间的距离都不超过7则输出Yes,否则 输出No. 因为之前没写过spfa,无聊的试了一下. 大概说下我对spfa实现的理解. 因为它是bellmanford的优化. 所以之 ...
- 几个小模板:topology, dijkstra, spfa, floyd, kruskal, prim
1.topology: #include <fstream> #include <iostream> #include <algorithm> #include & ...
- E - Easy Dijkstra Problem(求最短路)
Description Determine the shortest path between the specified vertices in the graph given in the inp ...
- 【Dijkstra+邻接表求次短路】POJ Sightseeing 3463
Language: Default Sightseeing Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7766 Ac ...
- 854. Floyd求最短路(模板)
给定一个n个点m条边的有向图,图中可能存在重边和自环,边权可能为负数. 再给定k个询问,每个询问包含两个整数x和y,表示查询从点x到点y的最短距离,如果路径不存在,则输出“impossible”. 数 ...
- AcWing 854. Floyd求最短路 多源 邻接矩阵
//不存在负权回路 //边权可能为负数 #include <cstring> #include <iostream> #include <algorithm> us ...
- FLOYD 求最小环
首先 先介绍一下 FLOYD算法的基本思想 设d[i,j,k]是在只允许经过结点1…k的情况下i到j的最短路长度则它有两种情况(想一想,为什么):最短路经过点k,d[i,j,k]=d[i,k,k- ...
随机推荐
- linux下的缓存机制及清理buffer/cache/swap的方法梳理
(1)缓存机制 为了提高文件系统性能,内核利用一部分物理内存分配出缓冲区,用于缓存系统操作和数据文件,当内核收到读写的请求时,内核先去缓存区找是否有请求的数据,有就直接返回,如果没有则通过驱动程序直接 ...
- C语言 百炼成钢1
//题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> ...
- php基础05:常量
<?php // 1.PHP 常量介绍 // 常量是单个值的标识符(名称).在脚本中无法改变该值.有效的常量名以字符或下划线开头(常量名称前面没有 $ 符号). // 2设置 PHP 常量 // ...
- Java集合---HashSet的源码分析
一. HashSet概述: HashSet实现Set接口,由哈希表(实际上是一个HashMap实例)支持.它不保证set 的迭代顺序:特别是它不保证该顺序恒久不变.此类允许使用null元素. 二. ...
- 流程引擎Activiti系列:如何将kft-activiti-demo-no-maven改用mysql数据库
kft-activiti-demo-no-maven这个工程默认使用h2数据库,这是一个内存数据库,每次启动之后都要重新对数据库做初始化,很麻烦,所以决定改用mysql,主要做3件事情: 1)在mys ...
- 坑死我啊,一个WPF Adorner使用注意事项
1.见鬼了? 项目中遇到这样的要求,一个Button用一个Adorner装饰,这个Adorner上又有一个Button,如下面这样 此时,我们在点击小Button的时候只希望处理小Button的事件, ...
- JavaScript原型链和instanceof运算符的暧昧关系
时间回到两个月前,简单地理了理原型链.prototype以及__proto__之间的乱七八糟的关系,同时也简单了解了下typeof和instanceof两个运算符,但是,anyway,试试以下两题: ...
- I2C和LCD信号干扰的解决:硬件工程师都硬不起来,让软件工程师硬着头上
DEMO4,LCD的clk干扰I2C,I2C无法通信. 把排针压下,去掉LCD的CLK,恢复正常. 过程: 直接跳线I2C,没问题.两排针插到一起就无法通信. 一个个的排针去除,最终找到LCD ...
- 整合 Bing translator 到自己的系统中
整合这个功能, 是因为 aliexpress 的买家来自不同国家, 我的 "小卖家" 同步到买家的留言, 很多西班牙,俄罗斯等小语种的文字, 看不懂. Google 被墙, 基本很 ...
- asp.net Core开启全新的时代,用视频来告诉你,学习就是这么SO easy。
https://channel9.msdn.com/Blogs/NET-Core/What-is-NET-Core 系统大家多发布一些视频的资料,学习起来更方便!我看到很多人发布的博客里面有的时候对于 ...