最短路之Dijkstra及其优化

#最短路之Dijkstra
之前写了一个Floyd,但是他的时间复杂度实在是太高了,所以我们现在看一种时间复杂度低一点的最短路算法:Dijkstra!

其实之前Floyd那篇博客的例题也可以用Dijkstra来做,但是为了体现Dijkstra算法的优越性,我们拿一个数据大一点的题来看。

题目描述:
Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.

Farmer John’s field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1…N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.

Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.

题目意思大概是有N(2到1000)个点,T(1到2000)条边,求点1到点n的最短路。

输入:
第一行是T,N,接下来T行,每行三个整数,一条路的起点,终点,距离。

输出:
1到n的最短路。

样例输入:
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100

样例输出:
90

那么对于这个题,Floyd肯定是用不了的,所以我们用时间复杂度低一点的Dijkstra。

朴素版Dijkstra

#include<iostream>
#include<queue>
#include<string.h>
#include<string>
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1e3+5;
const int INF=0x3f3f3f3f;
int dis[N],flag[N];
int e[N][N];
int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,m;
    while(cin>>n>>m)
    {
        memset(e,INF,sizeof e);//初始化任意两点之间的距离为一个很大的值
        for(int i=1;i<=n;i++)
        {
            int u,v,w;
            cin>>u>>v>>w;
            e[u][v]=min(e[u][v],w);
            e[v][u]=min(e[v][u],w);//无向图正向反向存边
        }
        for(int i=1;i<=m;i++)
        {
            dis[i]=e[1][i];//点1到点i的距离;
            flag[i]=0;//表示没访问过
        }
        dis[1]=0;flag[1]=1;//从源点开始
        for(int i=1;i<m;i++)
        {
            int u,minx=INF;
            for(int j=1;j<=m;j++)
            {
                if(dis[j]<minx&&!flag[j])
                {
                    u=j;
                    minx=dis[j];//找出没被访问过而且离源点最近的那个点
                }
            }
            flag[u]=1;//把找出来的那个点标记
            for(int j=1;j<=m;j++)
            {
            //判断经过u点是否会使得源点到j点之间的距离更短
                if(dis[j]>dis[u]+e[u][j]&&!flag[j])
                    dis[j]=dis[u]+e[u][j];
            }
        }
        cout<<dis[m]<<endl;
    }
}

Dijkstra虽然把时间复杂度降低了一点,但是开一个e[ ][ ]的二维数组很浪费空间,因为并不是任意两点之间都会有路。所以我们对他进行优化。

优化版Dijkstra(链式前向星)

#include<iostream>
#include<queue>
#include<string.h>
#include<string>
#include<stdio.h>
#include<math.h>
#include<vector>
#include<algorithm>
#define IOS std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
const int N=1e3+5;
const int M=1e5+5;
const int INF=0x3f3f3f3f;
int m,n;
struct edge{
int next,to,w;
}e[M];
int dis[M],head[M],vis[M],cnt;
//dis[i]表示源点到i的最短距离
//vis[i]标记i点是否被访问过
void addedge(int u,int v,int w)//链式前向星存图
{
    e[++cnt].next=head[u];//上一条以u为起点的边的编号
    e[cnt].to=v;
    e[cnt].w=w;
    head[u]=cnt;//更新编号
}
void dijkstra(int s)//s是源点
{
    for(int i=1;i<=n;i++)
        dis[i]=INF,vis[i]=0;//初始化
    priority_queue<pair<int,int> >q;
    dis[s]=0;
    q.push({-dis[s],s});
    while(!q.empty())//队列q为非空
    {
        int u=q.top().second;//pair类型中的第二个值,即上上行中的s
        q.pop();
        if(!vis[u])
        {
            vis[u]=1;
            //e[i].next中存的是以u为起点的上一条边的编号
            for(int i=head[u];i;i=e[i].next)//遍历以u为起点的所有边
            {
                int v=e[i].to;
                if(dis[v]>dis[u]+e[i].w)
                {
                    dis[v]=dis[u]+e[i].w;//更新
                    q.push({-dis[v],v});//把v点放到队列中
                    //pair类型的优先队列按第一关键字降序,所以存负值
                }
            }
        }
    }

}
int main()
{
    IOS;
    cin>>m>>n;
    for(int i=1;i<=m;i++)
    {
        int a,b,c;
        cin>>a>>b>>c;
        addedge(a,b,c);
        addedge(b,a,c);//同样是双向存边
    }
    dijkstra(1);
    cout<<dis[n]<<endl;
}

优化之后的代码就把空间有效利用了,避免了二维数组的大量浪费空间。

最后,Dijkstra也不能用来处理存在负边权的情况!!!

欲知如何处理负边权,请看spfa。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值