BZOJ2292——【POJ Challenge 】永远挑战

时间:2023-03-08 19:56:53

1、题意:dijkstra模板题,存点模板

#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define M 2000010
#define inf 1047483647

inline int read(){
    char ch = getchar(); int x = 0, f = 1;
    while(ch < '0' || ch > '9'){
        if(ch == '-') f = -1;
        ch = getchar();
    }
    while('0' <= ch && ch <= '9'){
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}

namespace dijkstra{
    struct Node{
        int d, u;
        inline bool operator < (const Node& rhs) const{
            return d > rhs.d;
        }
    };
    int d[M], done[M];
    priority_queue<Node> Q;
    struct Edge{
        int u, v, w, next;
    } G[M];
    int head[M], tot;
    int n; //number of points

    inline void init(){
        memset(head, -1, sizeof(head));
        tot = -1;
    }

    inline void add(int u, int v, int w){
        G[++ tot] = (Edge){u, v, w, head[u]};
        head[u] = tot;
    }

    inline void get_dis(int s){
        for(int i = 1; i <= n; i ++) d[i] = inf;
        d[s] = 0; while(!Q.empty()) Q.pop();
        Q.push((Node){0, s});
        memset(done, 0, sizeof(done));
        while(!Q.empty()){
            Node u = Q.top(); Q.pop();
            int x = u.u;
            if(done[x]) continue;
            done[x] = 1;
            for(int i = head[x]; i != -1; i = G[i].next){
                Edge& e = G[i];
                if(d[e.v] > d[x] + e.w){
                    d[e.v] = d[x] + e.w;
                    Q.push((Node){d[e.v], e.v});
                }
            }
        }
    }
}

using namespace dijkstra;

int main(){
    n = read();
    int m = read();
    init();
    for(int i = 1; i <= m; i ++){
        int u = read(), v = read(), w = read();
        add(u, v, w);
    }
    get_dis(1);
    printf("%d\n", d[n]);
    return 0;
}