• 【模板】POJ-1502(dijkstra)


    POJ-1502

    #include <bits/stdc++.h>
    using namespace std;
    typedef pair<int, int> PI;
    const int N = 105;
    const int INF = 0x3f3f3f3f;
    
    struct Edge
    {
        int to, dis;
    };
    vector<Edge> G[N]; //邻接表存图,G[i].to/dis i与to之间的边权值为dis
    int n, m, s;
    int d[N][N];
    
    void dijkstra() {
        priority_queue<PI, vector<PI>, greater<PI> > q; //按first从小到大出队,first最短路径second顶点编号
        for (int i = 0; i <= n; i++) d[s][i] = INF;
        d[s][s] = 0;
        q.push({0, s});
        while (!q.empty()) {
            PI tmp = q.top(); q.pop(); //有可能进行松弛的点,<与源点的距离,编号>
            int v = tmp.second;
            if (d[s][v] < tmp.first) continue; //如果当前(点与源点的距离)不是最短距离,则没必要进行松弛操作
            for (int i = 0; i < G[v].size(); i++) { //遍历当前点的所有出边
                Edge e = G[v][i]; //e代表与v相连的边
                if (d[s][e.to] > d[s][v] + e.dis) { //如果源点到(e的另一端点)的距离>源点与v的距离+e的权值,则进行松弛
                    d[s][e.to] = d[s][v] + e.dis;
                    q.push({d[s][e.to], e.to});
                }
            }
        }
    }
    
    int main() {
        ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
        cin >> n;
        for (int i = 1; i <= n; i++) G[i].push_back({i, 0});
        for (int i = 2; i <= n; i++) {
            for (int j = 1; j < i; j++) {
                string val; cin >> val;
                if (val == "x") {
                    G[i].push_back({j, INF});
                    G[j].push_back({i, INF});
                } else {
                    G[i].push_back({j, atoi(val.c_str())});
                    G[j].push_back({i, atoi(val.c_str())});
                }
            }
        }
        s = 1;
        dijkstra();
        int ans = -INF;
        for (int i = 1; i <= n; i++) ans = max(ans, d[1][i]);
        cout << ans << endl;
        return 0;
    }
    
  • 相关阅读:
    ORACLE 日期格式
    Oracle 12c release 2的安装
    InstantClient+PLSQL安装配置教程
    POSTMAN打印日志与json文件传参
    POSTMAN Request的几个要素
    Python+selenium处理滚动条
    Keys 类键盘操作的常用方法
    ActionChains类鼠标操作的常用方法
    Python+webdriver 文件上传自动化
    【SDOI2011】染色
  • 原文地址:https://www.cnblogs.com/Nepenthe8/p/13640377.html
Copyright © 2020-2023  润新知