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- ...
随机推荐
- Unity 协程与线程
协程是不同步的 协程 不是 线程,协同程序是 不同步 的 一个线程在程序中和其他线程是异步运行的,在多处理器机器中一个线程可以同时与所有其他线程的实时运行其代码,这使得线程编程能够解决很复杂的事情,因 ...
- AS3声音录音
MicRecorder, a tiny microphone library http://www.bytearray.org/?p=1858
- linux下查找某个目录下包含某个字符串的文件
有时候要找一些字符串,但是又不知道在哪个文件,只记得一些字符串 那么如何在linux下寻找包含某段文字的文件呢? 强大的find命令可以帮你完成不可能的任务. 比如我只记得我的程序里包含唯一的字符串“ ...
- html和css知识总结
今天把整个html和css的总结了一遍.可能还有疏忽之处,共同学习吧 [行为样式三者分离] 不加行内css样式,不加行内js效果 [标签]1>单标签<!doctype html> 文 ...
- dos系统下mysql常用命令
show table status;//查看所有表状态,通过这个命令可以得知表的创建时间和最后更新时间,以及该表是基表还是视图以及是什么表引擎等信息. show table status from d ...
- Window.open()方法参数详解
Window.open()方法参数详解 1, 最基本的弹出窗口代码 window.open('page.html'); 2, 经过设置后的弹出窗口 window.open('page.html ...
- Web项目构建
Gradle为Web开发提供了两个插件,war和jetty apply plugin: 'war' apply plugin: 'jetty' war插件继承了java插件,jetty插件继承了war ...
- JAVA 根据数据库表内容生产树结构JSON数据
1.利用场景 组织机构树,通常会有组织机构表,其中有code(代码),pcode(上级代码),name(组织名称)等字段 2.构造数据(以下数据并不是组织机构数据,而纯属本人胡编乱造的数据) List ...
- 墙国内新建Rails应用的要点(windows 7环境, Rails 4.2.0)
1. 使用rails new 命令创建完的应用在自动执行bundle install不会成功,根据出错提示,判断原因有可能是被墙与https的证书的安全性问题. 作为开发环境,选用绕开的办法,在目录 ...
- Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return t ...