Dijkstra求最短路 (堆优化 && +权值)模版

朴素版,仅仅支持权值为1

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector> 
#include <map>
#include <stack>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
int n, m,  now, l, r, c, b, T, k;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
int pie[] = { 3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,8,4,6,2,6,4,3,3,8,3,2,7,9,5,0,2,8,8,4,1,9,7,1,6,9,3,9,9,3,7,5,1,0 };
int step[50][50][50];
int nx, ny, nz, ex, ey, ez;
int w[50];
int sum[510][510];
int e[2022199];
int f[2022199];
int  dp[50];
ll mod = 1e9 + 7;
string ss = "codeforces";
bool p[20221199];//找没找到最小值
int check()
{
    memset(f, 0x3f, sizeof(f));
    f[1] = 0;
    for (int i = 1; i <= n; i++)
    {
        int t = -1;
        for (int j = 1; j <= n; j++)
        {
            if (!p[j] && (t == -1 || f[t] > f[j]))
            {
                t = j;
                
            }
        }
        p[t] = true;
        for (int j = 1; j <= n; j++)//这里sum[t][j]指的是从t到j的距离
        {//f[t] = 为从1 到t的距离
            f[j] = min(f[j], f[t] + sum[t][j]);
        }
    }
    if (f[n] == 0x3f3f3f3f)
    {
        return -1;
    }
    else
    {
        return f[n];
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    memset(sum , 0x3f , sizeof(sum));
    cin >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        sum[a][b] = min(sum[a][b], c);
    }
    int ans = check();
    cout << ans << endl;
    return 0;
}

堆优化支持权值只要为正的都行,并且时间复杂度也更优越

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector> 
#include <map>
#include <stack>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;//该堆里储存距离和节点编号
ll n, m, k;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
int pie[] = { 3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,8,4,6,2,6,4,3,3,8,3,2,7,9,5,0,2,8,8,4,1,9,7,1,6,9,3,9,9,3,7,5,1,0 };
int nx, ny, nz, ex, ey, ez;
const int N = 1e6 + 10;
int maxx = 1e6 + 3;
int dx[] = { 0 , 0 , 1 , -1 };
int dy[] = { 1 , -1 , 0 , 0 };
ll mod = 1e9 + 7;
int idx, e[N], ne[N], h[N] , w[N];//邻接表存图
int dij[N];//储存距离
bool st[N];//储存状态
void add(int a , int b , int c)//邻接表的储存
{
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
int div()
{
    memset(dij, 0x3f, sizeof(dij));
    dij[1] = 0;
    priority_queue<PII, vector<PII>, greater<PII>> heap;//堆里存的piar//小根堆
    heap.push({ 0 , 1 });//插入距离和节点//距离为0 , 编号为1
    while (!heap.empty())//堆里不为空
    {
        PII t = heap.top();//取距离原点最近的点//也就是堆的原点
        heap.pop();//取出之后出队
        int ver = t.second, dis = t.first;//ver 编号 , dis 原点距离ver的距离大小
        if (st[ver])//看是否距离已经确定//已经处理过了
        {
            continue;
        }
        st[ver] = 1;//现在没确定也要确定了
        for (int i = h[ver]; i != -1; i = ne[i])//邻接表遍历 , 更新ver所指向的节点距离//用当前这个点来更新所有的带你
        {
            int j = e[i];//它的对象
            if (dij[j] > dij[ver] + w[i])//权值相加
            {
                dij[j] = dij[ver] + w[i];//更新为更小的//并将后面的也一并更新
                heap.push({ dij[j] , j });//距离只要变小,就继续入堆
            }
        }
    }
    if (dij[n] == 0x3f3f3f3f)
    {
        return -1;
    }
    else
    {
        return dij[n];
    }
}
void solve()
{
    cin >> n >> m;

    memset(h, -1, sizeof(h));
    while (m--)
    {
        int a, b, c;
        cin >> a >> b >> c;
        add(a , b , c );
    }
    cout << div() << endl;
    return;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Tang_7777777

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

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

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

打赏作者

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

抵扣说明:

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

余额充值