《算法笔记》Dijkstra算法笔记

今日在华农终于接近完成阅读算法笔记,有点点成就感,做下dijkstra跟DFS算法结合的笔记

简单状态:纯dijkstra:

#include<iostream>
#include<algorithm>
#include<vector>
#define inf 1000000000
using namespace std;
const int maxn=1010;
int d[maxn];
bool vis[maxn]={false};
int G[maxn][maxn];
void dijkstra(int s){
	fill(d,d+maxn,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;//找不s的最近的点 
		vis[u]=true;
		for(int v=0;v<n;v++){
			if(vis[v]==false&&d[u]+G[u][v]<d[v]&&G[u][v]!=inf){
				d[v]=d[u]+G[u][v];
			}
		}
	}
}

只需dijkstra(s);

打印路径且有第二标尺:dijkstra:

#include<iostream>
#include<algorithm>
#define inf 1000000000
using namespace std;
const int maxn=1010;
int d[maxn],c[maxn];
bool vis[maxn]={false};
int G[maxn][maxn],cost[maxn][maxn];
int n,m,st,ed;//n顶点数,m边数,st,ed起点终点 
int pre[maxn];

void dijkstra(int s){
	fill(d,d+maxn,inf);
	d[s]=0;
	c[s]=0;//表示到自身的花费为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;//找不s的最近的点 
		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];
					c[v]=c[u]+cost[u][v];
					pre[v]=u;//v的前驱是u 
				}else if(d[u]+G[u][v]==d[v]){
					if(c[u]+cost[u][v]<c[v]){
						c[v]=c[u]+cost[u][v];
						pre[v]=u;//v的前驱是u
					}
				}
			}
		}
	}
}

void dfs(int v){
	if(v==st){
		printf("%d",v);//递归边界是起点 
		return;
	}
	dfs(pre[v]);
	printf("%d",v);
}

int main(){
	scanf("%d%d%d%d",&n,&m,&st,&ed);
	int u,v;
	fill(G[0],G[0]+maxn*maxn,inf);
	for(int i=0;i<m;i++){
		scanf("%d%d",&u,&v);
		scanf("%d%d",&G[u][v],&cost[u][v]);
		G[v][u]=G[u][v];
		cost[v][u]=cost[u][v];
	}
	dijkstra(st);
	dfs(ed);
	printf("%d %d\n",d[ed],c[ed]);
	return 0;
}

vector写法:

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=510;//最大顶点数
const int inf=1000000000;

int n,m,st,ed,G[maxn][maxn],cost[maxn][maxn];//n为顶点数目,m为边数,st和ed分别为起点和终点
//G为距离矩阵,cost为花费矩阵
//d[i]表示为源点到那个点的最短距离
//mincost记录最小花费
int d[maxn],mincost=inf;
bool vis[maxn]={false};
vector<int> pre[maxn];//前驱 
vector<int> temppath,path;//临时路径、最优路径

void dijkstra(int s){
	fill(d,d+maxn,inf);
	d[s]=0;//源点到源点的距离为0
	for(int i=0;i<n;i++){
		int u=-1,min=inf;
		for(int j=0;j<n;j++){
			if(vis[j]==false&&min>d[j]){
				min=d[j];
				u=j;
			}
		}
		if(u==-1) return ;
		vis[u]=true;
		for(int v=0;v<n;v++){
			//如果v未被访问且u能到达v 
			if(vis[v]==false&&G[u][v]!=inf){
				if(d[u]+G[u][v]<d[v]){
					d[v]=d[u]+G[u][v];
					pre[v].clear();//清空v的前驱
					pre[v].push_back(u);//更新v的前驱为u 
				}
				else if(d[u]+G[u][v]==d[v]){
					pre[v].push_back(u);
				}
			}
		}
	}
} 

void dfs(int v){//v为当前节点 
	if(v==st){//到达递归边界,叶子结点/路径起点 
		temppath.push_back(v);
		int tempcost=0;
		for(int i=temppath.size()-1;i>0;i--){//要倒着访问 
			//当前结点id,下个结点idnext
			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(){
	scanf("%d%d%d%d",&n,&m,&st,&ed);
	int u,v;
	fill(G[0],G[0]+maxn*maxn,inf);
	fill(cost[0],cost[0]+maxn*maxn,inf);
	for(int i=0;i<m;i++){
		scanf("%d%d",&u,&v);
		scanf("%d%d",&G[u][v],&cost[u][v]);
		G[v][u]=G[u][v];
		cost[v][u]=cost[u][v];
	}
	dijkstra(st);
	dfs(ed);
	for(int i=path.size()-1;i>=0;i--){
		printf("%d ",path[i]);
	}
	printf("%d %d\n",d[ed],mincost);
	return 0;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值