Dijkstra实现最短路径
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXV=1000;
const int INF=100000000;
int n,m,s,G[MAXV][MAXV];
int d[MAXV];//起点到达各点的最短路径长度
bool vis[MAXV]={false};
void Dijkstra(int s){
fill(d,d+MAXV,INF);
d[s]=0;
for(int i=0;i<n;i++){
int u=-1,MIN=INF;
for(int j=0;j<n;j++){
if(vis[j]==false&&d[j]<MIN){
u=j;
MIN=d[j];
}
}
if(u==-1) return;
vis[u]=true;
for(int v=0;v<n;v++){
if(vis[v]==false&&G[u][v]!=INF&&d[u]+G[u][v]<d[v]){
d[v]=d[u]+G[u][v];
}
}
}
}
int main(){
int u,v,w;
cin>>n>>m>>s;
fill(G[0],G[0]+MAXV*MAXV,INF);
for(int i=0;i<m;i++){
cin>>u>>v>>w;
G[u][v]=w;
}
Dijkstra(s);
for(int i=0;i<n;i++){
cout<<d[i]<<" ";
}
return 0;
}
邻接表实现
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int INF=;
const int MAXV=;
int m,s,n,d[MAXV];//m个点n个边起点是s
bool vis[MAXV]={false};
struct Node{
int v,dis;
};
vector<Node>G[MAXV];
void dj(){
fill(d,d+MAXV,INF);
d[s]=;
for(int i=;i<m;i++){
int u=-,MAX=INF;
for(int j=;j<m;j++){
if(vis[j]==false&&d[j]<MAX){
u=j;
MAX=d[j];
}
}
if(u==-) return;
vis[u]=true;
for(int j=;j<G[u].size();j++){
int v=G[u][j].v;
if(vis[v]==false&&d[u]+G[u][j].dis<d[v]){
d[v]=d[u]+G[u][j].dis;//注意此处是G[u][j]而不是G[u][v]
}
}
}
}
int main(){
int uu,vv,w;
cin>>m>>n>>s;
for(int i=;i<n;i++){
cin>>uu>>vv>>w;
Node tp;
tp.dis=w;
tp.v=vv;
G[uu].push_back(tp);
}
dj();
for(int i=;i<m;i++){
cout<<d[i]<<" ";
}
return ;
}
测试:
6 8 0
0 1 1
0 3 4
0 4 4
1 3 2
2 5 1
3 2 2
3 4 3
4 5 3
代码解释:
void d(){
//初始化图
//将出发点到出发点的距离设为0
for(循环n次){//n表示顶点数
//设当前欲访问的顶点下标为u=-1;
//设所有点中到起点距离最短的那个点的路径长为MAX=100000000000;
for(循环n次){
if(第n个点没有被访问&&该点到起点的路径最短){
MAX=最短路径;
u=n;//记录该点,即该点已经被访问
}
}
if(u==-1) return;//所有点已经被访问,函数结束
//标记u点被访问
for(n次循环){
if(该点没有被访问&&该点到新被访问顶点u的距离小于原来路径){
//更新路径
}
}
}
}
视频解释:https://www.bilibili.com/video/av38254646/?redirectFrom=h5
时间复杂度=n*(n+n)
在实际解题中可能会出现多个权重的问题,直接的解题方法就是增加变量再重新划分逻辑,例如PAT A1003
As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.
Input
Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (<= 500) – the number of cities (and the cities are numbered from 0 to N-1), M – the number of roads, C1 and C2 – the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c2 and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.
Output
For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather.
All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.
Sample Input
5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1
Sample Output
2 4
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXV=510;
const int INF=10000000;
bool vis[MAXV]={false};
int n,m,s,e,G[MAXV][MAXV],d[MAXV],num[MAXV],weight[MAXV],w[MAXV];
void dj(){
fill(d,d+MAXV,INF);
memset(num,0,sizeof(num));
memset(w,0,sizeof(w));
d[s]=0;
w[s]=weight[s];
num[s]=1;
for(int i=0;i<n;i++){
int MIN=INF,u=-1;
for(int j=0;j<n;j++){
if(vis[j]==false&&d[j]<MIN){
u=j;
MIN=d[j];
}
}
if(u==-1) return;
vis[u]=true;
for(int v=0;v<n;v++){
if(vis[v]==false&&G[u][v]!=INF){
if(d[u]+G[u][v]<d[v]){
d[v]=d[u]+G[u][v];
w[v]=w[u]+weight[v];
num[v]=num[u];
}else if(d[u]+G[u][v]==d[v]){
if(w[u]+weight[v]>w[v]){
w[v]=weight[v]+w[u];
}
num[v]+=num[u];
}
}
}
}
}
int main(){ cin>>n>>m>>s>>e;
for(int i=0;i<n;i++){
cin>>weight[i];
}
int u,v,p;
fill(G[0],G[0]+MAXV*MAXV,INF);
for(int i=0;i<m;i++){
cin>>u>>v>>p;
G[v][u]=G[u][v]=p;
}
dj();
cout<<num[e]<<" "<<w[e];
return 0;
}
还有PAT A1030
A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.
Input Specification:
Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (≤) is the number of cities (and hence the cities are numbered from 0 to N−1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:
City1 City2 Distance Cost
where the numbers are all integers no more than 500, and are separated by a space.
Output Specification:
For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.
Sample Input:
4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20
Sample Output:
0 2 3 3 40
增加变量的常规解法
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXV=510;
const int INF=100000000;
bool vis[MAXV]={false};
int n,m,s,e,G[MAXV][MAXV],d[MAXV],cost[MAXV],weight[MAXV][MAXV],pre[MAXV];
void dj(){
fill(d,d+MAXV,INF);
fill(cost,cost+MAXV,INF);
cost[s]=0;
d[s]=0;
for(int i=0;i<n;i++)
pre[i]=i;
for(int i=0;i<n;i++){
int MIN=INF,u=-1;
for(int j=0;j<n;j++){
if(vis[j]==false&&d[j]<MIN){
u=j;
MIN=d[j];
}
}
//if(u==-1) return;
vis[u]=true;
for(int v=0;v<n;v++){
if(vis[v]==false&&G[u][v]!=INF){
if(d[v]>d[u]+G[u][v]){
d[v]=d[u]+G[u][v];
cost[v]=cost[u]+weight[u][v];
pre[v]=u;
}else if(d[v]==d[u]+G[u][v]&&cost[u]+weight[u][v]<cost[v]){
cost[v]=cost[u]+weight[u][v];
pre[v]=u;
}
}
}
}
}
void f(int v){
if(v==s){
cout<<v<<" ";
return;
}
f(pre[v]);
cout<<v<<" ";
}
int main(){
cin>>n>>m>>s>>e;
int u,v,p,q;
fill(G[0],G[0]+MAXV*MAXV,INF);
for(int i=0;i<m;i++){
cin>>u>>v>>p>>q; weight[v][u]=weight[u][v]=q;
G[v][u]=G[u][v]=p;
}
dj();
f(e);
cout<<d[e]<<" "<<cost[e];
return 0;
}
模板式DFS+dj
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int INF=100000000;
const int MAXV=510;
int d[MAXV],c[MAXV],G[MAXV][MAXV],cost[MAXV][MAXV];
bool vis[MAXV]={false};
int n,m,s,e,mincost=INF;
vector<int>pre[MAXV];
vector<int>temppath,path;
void dj(){
fill(d,d+MAXV,INF);
d[s]=0;
for(int i=0;i<n;i++){
int MIN=INF,u=-1;
for(int j=0;j<n;j++){
if(vis[j]==false&&d[j]<MIN){
MIN=d[j];
u=j;
}
}
if(u==-1) return;
vis[u]=true;
for(int v=0;v<n;v++){
if(vis[v]==false&&G[u][v]!=INF){
if(d[v]>d[u]+G[u][v]){
d[v]=d[u]+G[u][v];
pre[v].clear();
pre[v].push_back(u);
}else if(d[v]==d[u]+G[u][v]){
pre[v].push_back(u);
} }
}
}
}
void DFS(int v){
if(v==s){
temppath.push_back(v);
int tempcost=0;
for(int i=temppath.size()-1;i>0;i--){
int id=temppath[i],idnext=temppath[i-1];
tempcost+=cost[id][idnext];
}
if(tempcost<mincost){
mincost=tempcost;
path=temppath;
}
temppath.pop_back();
return;
}
temppath.push_back(v);
for(int i=0;i<pre[v].size();i++)
DFS(pre[v][i]);
temppath.pop_back();
}
int main(){
cin>>n>>m>>s>>e;
int a,b,c,dd;
fill(G[0],G[0]+MAXV*MAXV,INF);
fill(cost[0],cost[0]+MAXV*MAXV,INF);
for(int i=0;i<m;i++){
cin>>a>>b>>c>>dd;
G[a][b]=G[b][a]=c;
cost[a][b]=cost[b][a]=dd;
}
dj();
DFS(e);
for(int i=path.size()-1;i>=0;i--)
cout<<path[i]<<" ";
cout<<d[e]<<" "<<mincost;
}
关于该算法的题目和思想后续还会更新
Dijkstra实现最短路径的更多相关文章
- 算法-迪杰斯特拉算法(dijkstra)-最短路径
迪杰斯特拉算法(dijkstra)-最短路径 简介: 迪杰斯特拉算法是由荷兰计算机科学家狄克斯特拉于1959 年提出的,因此又叫狄克斯特拉算法.是从一个顶点到其余各顶点的最短路径算法,解决的是有向图中 ...
- Dijkstra求最短路径
单源点的最短路径问题:给定带权有向图G和源点V,求从V到G中其余各顶点的最短路径 Dijkstra算法描述如下: (1)用带权的邻接矩阵arcs表示有向图,arcs[i][j]表示弧<vi,vj ...
- NYOJ 1248 海岛争霸(Dijkstra变形——最短路径最大权值)
题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=1248 描述 神秘的海洋,惊险的探险之路,打捞海底宝藏,激烈的海战,海盗劫富等等.加勒比 ...
- POJ 2253 Frogger(Dijkstra变形——最短路径最大权值)
题目链接: http://poj.org/problem?id=2253 Description Freddy Frog is sitting on a stone in the middle of ...
- POJ 3790 最短路径问题(Dijkstra变形——最短路径双重最小权值)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3790 Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你 ...
- Dijkstra求最短路径&例题
讲了半天好像也许maybe听懂了一点,先写下来233 先整理整理怎么存(开始绕) 最简单的是邻接矩阵存,但是开到10000*10000就MLE了,所以我们用链式前向星存(据说是叫这个名字吧) 这是个什 ...
- Dijkstra算法 - 最短路径算法
2017-07-26 22:30:45 writer:pprp dijkstra算法法则:设置顶点集合S,首先将起始点加入该集合,然后根据起始点到其他顶点的路径长度, 选择路径长度最小的顶点加入到集合 ...
- POJ 2387 Til the Cows Come Home 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 ...
- Dijkstra算法——最短路径(转)
转自:http://www.cnblogs.com/biyeymyhjob/archive/2012/07/31/2615833.html Dijkstra算法 1.定义概览 Dijkstra(迪杰斯 ...
随机推荐
- [GO]revoer的应用
error的函数只是用来报一些低等级的错误,panic是报那些会导致程序崩溃的错误,但是会有一个问题就是panic也会导致程序中断 ,如果我们需要程序在报错之后继续运行并报出错误的信息 就需要使用到r ...
- CodeForces 474C Captain Marmot (数学,旋转,暴力)
题意:给定 4n * 2 个坐标,分成 n组,让你判断,点绕点的最少次数使得四个点是一个正方形的顶点. 析:那么就一个一个的判断,n 很小,不会超时,四个点分别从不转然后转一次,转两次...转四次,就 ...
- 编写高质量代码改善C#程序的157个建议——建议135: 考虑使用肯定性的短语命名布尔属性
建议135: 考虑使用肯定性的短语命名布尔属性 布尔值无非就是True和False,所以应该用肯定性的短语来表示它,例如,以Is.Can.Has作为前缀. 布尔属性正确命名的一个示例如下: class ...
- Postman -- HTTP请求的Chrome插件
摘要 : Postman是一款功能强大的网页调试与发送网页HTTP请求的Chrome插件. 用户在开发或者调试网络程序或者是网页B/S模式的程序的时候是需要一些方法来跟踪网页请求的,用户可以使用一些网 ...
- 企业搜索引擎开发之连接器connector(十六)
本人有一段时间没有接触企业搜索引擎之连接器的开发了,连接器是涉及企业搜索引擎一个重要的组件,在数据源与企业搜索引擎中间起一个桥梁的作用,类似于数据库之JDBC,通过连接器将不同数据源的数据适配到企业搜 ...
- delphi Table切换控件顺序问题
delphi Table切换控件顺序问题 Tagorder的值就是确定Table键切换顺序的 以上做法只能解决同一类型的多个控件(如Edit1,edit2....)显示顺序问题 假如有不同类型的控件如 ...
- 相当郁闷的问题,TabHost选项卡标签图标始终不出现?
在学习Android TabHost布局过程中,很多教程告诉我,这样来显示选项卡标签的图标和文字: TapSpec spec1 = tabHost.newTabSpec("tab 1&quo ...
- RobotFramework与Jenkins集成后构建成功率高于设置阈值但总是显示失败
摘要:robot执行后总是失败 1.在配置jenkins的job时,添加构建步骤Execute Windows batch command,输入执行robotframework测试用例命令 2.然后j ...
- 爬虫--使用scrapy爬取糗事百科并在txt文件中持久化存储
工程目录结构 spiders下的first源码 # -*- coding: utf- -*- import scrapy from firstBlood.items import Firstblood ...
- 快速搭建windows服务器的可视化运维环境
开发好的程序部署在服务器上,如何对服务器的基本指标进行监控呢?最近对一套工具进行了研究,可以快速搭建服务器监管环境,很是强大,最重要的是它还很酷炫. 原理:数据采集+时序数据库+可视化,下面记录一下搭 ...