首页 > 其他分享 >最短路-迪杰斯特拉(dijkstra)

最短路-迪杰斯特拉(dijkstra)

时间:2024-05-23 11:40:56浏览次数:15  
标签:return cout int 短路 迪杰 cin i64 dijkstra define

迪杰斯特拉(dijkstra)

//
// Created by LANSGANBS on 2024/3/8.
//
/*
 * code template: https://github.com/LANSGANBS/code-template
 * URL:
 * Status:
 * 写完这道就去原
*/
#include <bits/stdc++.h>

using namespace std;
using i64 = long long;
#define endl '\n'
#define buff ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define debug cout << "********************************************" << endl;
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#define yes cout << "yes" << endl;
#define no cout << "no" << endl;
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define _REP(i, b, a) for (int i = b; i >= a; i--)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define _rep(i, b, a) for (int i = b; i > a; i--)
#define all(x) begin(x), end(x)
#define mem(a, x) memset(a, x, sizeof(a))
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a / gcd(a, b) * b)
#define sz(x) (int)x.size()
#define lowbit(x) (x & -x)
#define pb push_back
#define ll long long
#define int ll
#define ld long double
#define vi vector<int>

i64 ceilDiv(i64 n, i64 m) // up
{
    if (n >= 0)
    {
        return (n + m - 1) / m;
    }
    else
    {
        return n / m;
    }
}

i64 floorDiv(i64 n, i64 m) // low
{
    if (n >= 0)
    {
        return n / m;
    }
    else
    {
        return (n - m + 1) / m;
    }
}

template<typename T1, typename T2>
istream &operator>>(istream &cin, pair<T1, T2> &a)
{
    return cin >> a.first >> a.second;
}

template<typename T1>
istream &operator>>(istream &cin, vector<T1> &a)
{
    for (auto &x: a)
    {
        cin >> x;
    }
    return cin;
}

template<typename T1, typename T2>
ostream &operator<<(ostream &cout, const pair<T1, T2> &a)
{
    return cout << a.first << ' ' << a.second;
}

template<typename T1, typename T2>
ostream &operator<<(ostream &cout, const vector<pair<T1, T2>> &a)
{
    for (auto &x: a)
    {
        cout << x << endl;
    }
    return cout;
}

template<typename T1>
ostream &operator<<(ostream &cout, const vector<T1> &a)
{
    int n = a.size();
    if (!n)
    {
        return cout;
    }
    cout << a[0];
    for (int i = 1; i < n; i++)
    {
        cout << ' ' << a[i];
    }
    return cout;
}

template<typename... Args>
void see(Args &...args)
{
    ((cin >> args), ...);
}

template<typename... Args>
void out(Args... args)
{
    ((cout << args << " "), ...);
}

template<typename... Args>
void outl(Args... args)
{
    ((cout << args << endl), ...);
}

const int mod = 1e9 + 7;
#ifdef ONLINE_JUDGE
constexpr int N = 2e5 + 7;
#else
constexpr int N = 1e3 + 7;
#endif

using PII = pair<int, int>;
using i64 = long long;

const int inf = 1e9;

vector<PII> g[N]; //存图
vector<int> dist(N, inf); //存距离 初始化为无穷 inf

void dijkstra(int s)
{
    priority_queue<PII, vector<PII>, greater<PII>> heap;
    heap.emplace(0, s);
    dist[s] = 0;// s 为起点

    while (heap.size())
    {
        auto [dis, u] = heap.top();
        heap.pop();
        if (dist[u] < dis)
        {
            continue;
        }
        for (auto &[v, w]: g[u])
        {
            if (dist[v] > dis + w)
            {
                dist[v] = dis + w;
                heap.emplace(dist[v], v);
            }
        }
    }
}

void solve()
{
    int n, m, s, a, b, c;
    see(n, m, s);
    rep (i, 0, m)
    {
        see(a, b, c);
        g[a].pb({b, c});
    }
    dijkstra(s);
    cout << dist[5] << endl; //查询s-5的最短路
}

signed main()
{
    buff;
    int tt = 1;
    // cin >> tt;
    while (tt--)
    {
        solve();
    }
    return 0;
}

标签:return,cout,int,短路,迪杰,cin,i64,dijkstra,define
From: https://www.cnblogs.com/LANSGANBS/p/18208096

相关文章

  • 最短路-Floyd
    Floyd////CreatedbyLANSGANBSon2024/3/18.///**codetemplate:https://github.com/LANSGANBS/code-template*local:C:\Users\18019\CLionProjects\.cpp-code*URL:https://www.luogu.com.cn/problem/B3647*Status:AC*写完这道就去原*/#include<......
  • 最短路径
    拓扑序有这样一个问题:我们给定一张\(n\)个点\(m\)条边的有向无环图(DAG),请求出从\(1\)号结点出发,到达任意结点的最短路径,保证\(s\)可以到达任意结点,\(n,m\leq10^7\)。我们以下面这张图为例。如果我们想求\(1\rightarrow4\)的路径,我们不难发现,找到\(1\rightar......
  • PKUSC 2024 最短路径
    本文首发于QOJhttps://qoj.ac/blog/skip2004/blog/866大家好,我是钱哥,我来写一下PKUSC2024最短路径的题解。没有做过这个题的同学可以先自行做一做。我们下面来讲解一下如何一步步解决这个题目。subtask4首先,我们来解决第一个具有挑战性的子任务:\(m\leq2.5n\),这个点具......
  • dijkstra迪杰斯特拉算法(邻接表法)
    ​算法简易过程:迪杰斯特拉算法(朴素)O(n^2)G={V,E}V:点集合E:边集合初始化时令S={某源点ear},T=V-S={其余顶点},T中顶点对应的距离(ear,Vi)值若存在,d(ear,Vi)为弧上的权值,dist【i】若不存在,d(ear,Vi)为无穷大,dist【i】循环n-1次(n个点):1、从T中选......
  • 链式前向星+dijkstra
    #include<iostream>#include<vector>usingnamespacestd;constintN=1000;struct{intto;intw;intnext;}edge[N];inthead[N];voidadd_edge(intu,intv,intw){staticintcnt=0;edge[cnt].to=v;edge[cnt].w=......
  • 【CodeChef】Prison Escape(最短路)
    题目大意:给出一个01矩阵,求每个0移动(每次可以向有公共边的格子移动一步)到矩阵边界至少要经过多少个1。考虑建最短路模型,将矩阵中的每个位置拆分为入点和出点,矩阵外部设为一个点。枚举矩阵中的每个位置:如果这个位置在矩阵边界,矩阵外部向这个位置的入点连一条长度为0的边。......
  • Dijkstra(迪杰斯特拉)
    Dijkstra(迪杰斯特拉)算法简单版须知首先用几个数组表示需要的状态:dis[] 表示距离从初始点到对应点的距离,初始点置为0,其他置为无穷graph[][]邻接矩阵,记录两点间连线的权重,可以记录无向图和有向图check[]判断当前点是否被记录算法思路:假定a为起点,每......
  • 单源最短路算法
    Dijkstra算法原理首先将所有点分为两类,确定和没确定最短路的点。首先我们可以知道,起点只能通过已经确定最短路的点去到其他点,所以我们可以不断的确定距离起点最近的点的路径长度,再用这个确定的点更新其他点到起点的最短路距离。此时找最近点为\(O(n)\),更新邻接点为\(O(m)\),......
  • Dijkstra
    非常经典的单源最短路算法。仅能用于正权图(边权可为\(0\))拥有朴素版\(O(n^2)\)和堆优化版\(O((n+m)\log{m})\)朴素版一般用邻接矩阵存图而优化版使用邻接表或者链式前向星,我常用链式前向星中心思想每次在没用过的点内找一个距离起点最近的点,用这个点对其他点进行松弛操......
  • 【最短路】网络延迟时间
    题源狄克斯特拉【待完成】classSolution:defnetworkDelayTime(self,times:List[List[int]],n:int,k:int)->int:g=[[float('inf')]*nfor_inrange(n)]forx,y,timeintimes:g[x-1][y-1]=timedist......