算法:最短路(dijkstra)

该博客讨论了如何使用Dijkstra算法解决有向图中两点间最短路径的问题,包括处理可能存在多条最短路径的情况。文章详细解释了算法流程,包括初始化、迭代过程以及如何更新节点的最短距离。同时,还提到了在存在多条最短路径时,如何选择具有最多救援队员的路径。给出了完整的C++代码实现,并提供了输入输出样例。
摘要由CSDN通过智能技术生成

思路

首先最外层循环n次,代表进行n次迭代确定每个点到起点的最小值,最后输出的终点即为要找的最短路的距离。

dist[n] 用于存储每个点到起点的最短距离
st[n] 用于在更新最短距离时 判断当前的点的最短距离是否确定 是否需要更新
每次迭代过程中都需要找到当前未确定的最短距离的点中距离最短的点。

int t=-1;      
for(int j=1;j<=n;j++)
{
    if(!st[j]&&(t==-1||dist[t]>dist[j])   
        t=j;
}

通过这层循环之后,就能找到当前未确定的最短距离的点中距离最短的点
所以将此点标记之后,用这个点来更新其余点的距离。

for(int j = 1; j <= n; ++j){
	dist[j] = min(dist[j], dist[t] + g[t][j]);
    }

问题描述

给定一个n个点m条边的有向图,图中可能存在重边和自环,所有边权均为正值。

请你求出1号点到n号点的最短距离,如果无法从1号点走到n号点,则输出-1。

输入格式

第一行包含整数n和m。

接下来m行每行包含三个整数x,y,z,表示存在一条从点x到点y的有向边,边长为z。

输出格式

输出一个整数,表示1号点到n号点的最短距离。

如果路径不存在,则输出-1。

数据范围

1≤n≤500,
1≤m≤105,
图中涉及边长均不超过10000。

输入样例:
3 3
1 2 2
2 3 1
1 3 4
输出样例:
3

代码

#include<iostream>
#include<cstring>
using namespace std;
const int N = 510;
int n, m; 
int g[N][N];
int dist[N];
bool st[N];



int dijkstra(){
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;
    for(int i = 0; i < n; ++i){
        int t = -1;
        for(int j = 1; j <= n; ++j)
            if(!st[j] && (t == -1 || dist[t] > dist[j]))
                t = j;
                
        st[t] = true;
        for(int j = 1; j <= n; ++j){
            dist[j] = min(dist[j], dist[t] + g[t][j]);
        }
    }
    if(dist[n] == 0x3f3f3f3f) return -1;
    return dist[n];
}

int main(){
    cin >> n >> m;
    memset(g, 0x3f, sizeof g);
    int a, b, c;
    for(int i = 0; i < m; ++i){
        scanf("%d %d %d", &a, &b, &c);
        g[a][b] = min(g[a][b], c);
    }
    int t = dijkstra();
    cout << t << endl;
    return 0;
}

原题链接

紧急救援(dijkstra+dfs)

原题链接

思路

首先,题目说明最优解唯一,并且需要输出最优解所经过的点。这就不能只使用单纯的dijkstra来做,因为dijkstra只可以求出来最短路,并不能标记所经过的点。所以辅助以dfs来求出所经过的点。
另外,在写dijkstra时,还需要做一部分修改,因为题中可能存在多条最短路,但题目要求尽可能收集更多的救援队员,还有统计一共有多少条路,因此当找到t之后,更新其他未确定的点时,需要加一些判断,如果当前的 d i s t [ j ] = = d i s t [ t ] + g [ t ] [ j ] dist[j]==dist[t]+g[t][j] dist[j]==dist[t]+g[t][j]时说明此时最短路不止一条,然后选择救援部队较多的一条。但是如果 d i s t [ j ] > d i s t [ t ] + g [ t ] [ j ] dist[j]>dist[t]+g[t][j] dist[j]>dist[t]+g[t][j]就直接选择这一条就可。
最后,dfs是根据dijkstra得到的dist来进行的,每次判断当前dist是否等于现在的长度+g[now][i],如果是则说明此点是最短中的一个点,然后将其加入路径,直到找到目标点而且人数和路径长度都相等时,说明找到了最优解的路径。记得复原。
上代码

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 1010;
int num[N], dist[N], cnt[N], tot[N], path[N], idx;
int g[N][N];
int st[N];
int n, m, s, d;

void dijkstra()
{
    memset(dist, 0x3f, sizeof dist);
    dist[s] = 0, cnt[s] = 1, tot[s] = num[s];
    for (int i = 0; i < n; ++i){
        int t = -1;
        for (int j = 0; j < n; ++j){
            if (!st[j] && (t == -1 || dist[t] > dist[j]))
                t = j;
        }
        st[t] = true;
        for (int j = 0; j < n; ++j)
        {
            if (!st[j] && dist[j] == dist[t] + g[t][j]){
                cnt[j] += cnt[t];
                if (tot[j] < tot[t] + num[j]) {tot[j] = tot[t] + num[j];}
            }else if (!st[j] && dist[j] > dist[t] + g[t][j]){
                cnt[j] = cnt[t];
                tot[j] = tot[t] + num[j];
                dist[j] = dist[t] + g[t][j];
            }
        }
    }
    cout << cnt[d] << " " << tot[d] << endl;
}


int flag[N];
void dfs(int now, int now_len, int now_people)
{
    if (now == d && now_len == dist[d] && now_people == tot[d])
    {
        cout << s << " ";
        for (int i = 0; i < idx; ++i)
            if (i != idx - 1)
                cout << path[i] << " ";
            else
                cout << path[i]; 
        cout << endl;
        return;
    }
    for (int i = 0; i < n; ++i){
        if (!flag[i] && now_len + g[now][i] == dist[i]){
            flag[i] = 1;
            path[idx ++] = i;
            dfs(i, now_len+g[now][i], now_people+num[i]);
            path[idx --];
            flag[i] = 0;
        }
    }
}

int main()
{
    memset(g, 0x3f, sizeof g);
    cin >> n >> m >> s >> d;
    for (int i = 0; i < n; ++i) scanf("%d", &num[i]);
    while (m --){
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        g[a][b] = min(g[a][b], c);
        g[b][a] = min(g[a][b], c);
    }
    dijkstra();
    dfs(s,0,num[s]);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

evil心安

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值