洛谷--P4779 【模板】单源最短路径(标准版)

题目来源

洛谷–P4779 【模板】单源最短路径(标准版)

一、基础dijkstra

基本思路:

1.定义ans[100000],ans[i]代表到达i点的最小花费

2.定义bool数组visit,代表是否来过这里

2.ans[起点] = 0, 其余的赋值为inf

3.定义一个curr变量,visit[current] = 1(访问过),代表现在的位置,初始值为起点。

4.列举所有与curr相联通的的点,将这些点(i)的ans值更新:

ans[i] = min(ans[i], ans[curr] + ans[i] = min(ans[i], ans[curr] +
到这些点需要的花费))
5. 列举所有访问过的的点,找到ans值最小的点,赋值给curr,visit[current] = 1(访问过)

6 所有点都访问过(visit[i]都 == 1),程序结束。此时,ans[i]代表从起点到i的最短路径

bool vis[1000000];//是否访问过
int ans[1000000];
int curr = 起点;
memset(ans, 0x7fffffff, sizeof(ans))
while (vis[curr] == 0){
    vis[curr] = 1;
    for (int i; 列举所有curr连通的点) {
            ans[i] = min(ans[i],ans[curr] + k)//k代表从curr点到i点的最短路
        }
    int minn = 2147483647;
        for (int i = 1; i <= m; i++)//列举所有点 {
            if (vis[i] == 1 && ans[i] < minn)//访问过且小 {
                minn = ans[i];//更新最小值
                    curr = i;//更新下一个点
            }
        }
}

代码

#include<iostream>
using namespace std;
int head[100000], cnt;
long long ans[1000000];//记录源点(固定且位移)到各个节点的最短距离
bool vis[1000000];//记录是否访问过该点/边
int m, n, s;//n为节点个数,m为边的条数,s为源点
struct edge{
    int to;
    int nextt;
    int wei;
}edge[1000000];
void addedge(int x, int y, int z){
    edge[++cnt].to = y;
    edge[cnt].wei = z;
    edge[cnt].nextt = head[x];
    head[x] = cnt;
}
int main(){
    cin >> m >> n >> s;
    for (int i = 1; i <= n; i++) {
        ans[i] = 2147483647;//初始化使答案一开始都是无限ref
    }
    ans[s] = 0;//源点到自己一定是0
    for (int i = 1; i <= n; i++){
        int a, b, c;
        cin >> a >> b >> c;//读取点边
        addedge(a, b, c);
    }
    int pos = s;//从源点开始计算
    while (vis[pos] == 0) {
        long long minn = 2147483647;
        vis[pos] = 1;//表示该节点已经经历过了
        for (int i = head[pos]; i != 0; i = edge[i].nextt){//遍历节点,找出连接pos的所有
            if (!vis[edge[i].to] && ans[edge[i].to] > ans[pos] + edge[i].wei){//如果i点没有松弛过且现在s到to的距离长于s经过最短pos的加上pos到i的距离
                ans[edge[i].to] = ans[pos] + edge[i].wei;//改变s到to节点的距离为更短的.
            }
        }
        for (int i = 1; i <= m; i++)//遍历所有点 {
            if (ans[i] < minn && vis[i] == 0){//如果该节点小于当前最小的边,且没有经历过松弛改变长度 
                minn = ans[i];
                pos = i;//改变到该节点
            }
        }//目的是找出所有没有松弛过的点中到达距离最小的.当所有点都到达过,那么pos不会发生改变,退出while循环.
    }
    for (int i = 1; i <= m; i++) {
        cout << ans[i] << ' ';
    }
}

二、堆优化的dijkstra

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
const int maxn = 1000010;
 
int head[maxn], cnt = 0;//存储某个节点开始遍历的起始边
long long ans[maxn];//存储源点s到各节点的最短距离
bool vis[maxn];//存储该节点的距离是否被松弛过
int m, n, s;
 
 
struct edge{
    int to;//这条边连接的终点节点
    int dis;//这条边的距离
    int Next;//这条线的终点的兄弟节点,上一条边的编号
}edge[maxn];
struct priority{//运用优先队列(堆)来实现优化
    long long ans;//存储可用的ans值
    int id;
    bool operator  <(const priority& x) const{//重载<号,使其可以使用优先队列
        return x.ans < ans;
    }
};
void add_edge(int u, int v, int w) {//为edge添加边
    edge[++cnt].to = v;//边的个数编号cnt加一,该边的终点为v
    edge[cnt].dis = w;//该边的距离为w
    edge[cnt].Next = head[u];//该边的兄弟节点边是u的上一个开始的节点边
    head[u] = cnt;//u开始的第一个节点边变为当前这个.
}
 
priority_queue<priority> q;//创建优先队列
 
int main(){
    scanf("%d%d%d", &n, &m, &s);
    for (int i = 1; i <= n; i++) {
        ans[i] = 2147483647;//初始化ans,让其作为无穷
    }
    ans[s] = 0;//自己到自己是0
    for (int i = 1; i <= m; i++) {
        int x, y, z;
        scanf("%d%d%d", &x, &y, &z);
        add_edge(x, y, z);
    }
    int u;
    q.push({ 0,s });
    while (!q.empty()) {//如果空了就表示没有可以加入的节点了
        priority t = q.top();
        q.pop();
        u = t.id;
        if (!vis[u]) {//如果该点没有松弛过
            vis[u] = 1;
            for (int i = head[u]; i; i = edge[i].Next) {//对该u节点的所有子节点遍历
                int v = edge[i].to;//i边的终点
                if (ans[v] > ans[u] + edge[i].dis) {//原来s到v的距离和现在从s到u再到v的距离比较
                    ans[v] = ans[u] + edge[i].dis;//改变,因为起始时,ans为无穷,这一步便会将所有u节点连接的边入队
                    if (!vis[v]) {//防止重复入队
                        q.push({ ans[v], v });
                    }
                }
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        printf("%lld ", ans[i]);//输出
    }
    return 0;
}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值