• Roadblocks POJ


    https://vjudge.net/problem/POJ-3255

     

    #include <iostream>
    #include <cstring>
    #include <queue>
    #include <vector>
    using namespace std;
    typedef pair<int, int> P;
    const int N = 2e5 + 10;
    struct Edge {
        int to, next, w;
    }edge[N];
    
    int head[N], dis1[N], dis2[N];
    int cnt, n, r;
    
    priority_queue<P, vector<P>, greater<P> > q;
    
    //链式前向星
    void addEdge(int u, int v, int cost) {
        edge[++cnt].to = v;
        edge[cnt].w = cost;
        edge[cnt].next = head[u];
        head[u] = cnt;
    }
    
    void dijkstra(int s) {
        dis1[s] = 0;
        q.push(P(0, s));
        while(!q.empty()) {
            P t = q.top();
            q.pop();
            int d = t.first, cur = t.second;
            if(dis2[cur] < d) continue;
            for(int i = head[cur]; i != - 1; i = edge[i].next) { //搜边
                int d2 = d + edge[i].w, u = edge[i].to;
                if(d2 < dis1[u]) {  //更新最短路径前更新次短路径
                    dis2[u] = dis1[u];
                    dis1[u] = d2;
                    q.push(P(dis1[u], u));
                }
                if(d2 > dis1[u] && d2 < dis2[u]) { //再次判断次短路出现的情况
                    dis2[u] = d2;
                    q.push(P(dis2[u], u));
                }
            }
        }
    }
    
    int main() {
        ios::sync_with_stdio(0);
        memset(head, -1, sizeof(head));
        memset(dis1, 0x3f, sizeof(dis1));
        memset(dis2, 0x3f, sizeof(dis2));
        int u, v, c;
        cin >> n >> r;
        for(int i = 1; i <= r; i++) {
            cin >> u >> v >> c;
            addEdge(u, v, c);
            addEdge(v, u, c);
        }
        dijkstra(1);
        cout << dis2[n];
        return 0;
    }
  • 相关阅读:
    xadmin可视化上传图片
    home数据库设计
    xadmin后台管理
    静态目录
    Git线上操作
    python 学习之JavaScript
    python学习之CSS
    python学习之HTML
    Python之旅(day10&day11 各种运算及基本数据类型)
    python之旅(第一课day9)
  • 原文地址:https://www.cnblogs.com/xcfxcf/p/12642433.html
Copyright © 2020-2023  润新知