Acwing 1129. 热浪 解题报告

Acwing 1129. 热浪

  单源最短路模板。用 (Dijkstra) 即可。

/*
@Author: nonameless
@Date:   2020-06-02 17:18:07
@Email:  2835391726@qq.com
@Blog:   https://www.cnblogs.com/nonameless/
*/
#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PLL;
typedef pair<int, int> PII;
const double eps = 1e-8;
const double PI  = acos(-1.0);
const int INF = 0x3f3f3f3f;
const ll LNF  = 0x3f3f3f3f3f3f;
inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
inline ll  gcd(ll  a, ll  b) { return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b) { return a * b / gcd(a, b); }

const int N = 2510, M = 6300 << 1;

int idx = 0;
int h[N], to[M], wei[M], nxt[M];

int vis[N];
int dist[N];

void add(int u, int v, int w){
    to[ ++ idx] = v; nxt[idx] = h[u]; wei[idx] = w; h[u] = idx;
}

int main(){

    int t, c, ts, te;
    scanf("%d%d%d%d", &t, &c, &ts, &te);
    for(int i = 1; i <= c; i ++){
        int s, e, w;
        scanf("%d%d%d", &s, &e, &w);
        add(s, e, w);
        add(e, s, w);
    }

    priority_queue<PII, vector<PII>, greater<PII> > pq;
    pq.push({0, ts});
    memset(dist, INF, sizeof dist);
    dist[ts] = 0;

    memset(vis, 0, sizeof vis);
    while(!pq.empty()){
        PII cur = pq.top(); pq.pop();
        int d = cur.x, u = cur.y;
        if(vis[u]) continue;
        vis[u] = 1;
        for(int i = h[u]; i; i = nxt[i]){
            int node = to[i];
            int cost = wei[i];
            if(dist[node] >= dist[u] + cost){
                dist[node] = dist[u] + cost;
                pq.push({dist[node], node});
            }
        }
    }

    cout << dist[te] << endl;

    return 0;
}

原文地址:https://www.cnblogs.com/nonameless/p/13035795.html