結果

問題 No.807 umg tours
ユーザー finefine
提出日時 2019-03-22 23:06:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,716 bytes
コンパイル時間 1,873 ms
コンパイル使用メモリ 180,852 KB
実行使用メモリ 26,860 KB
最終ジャッジ日時 2023-10-19 10:20:06
合計ジャッジ時間 7,479 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 WA -
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 WA -
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 235 ms
14,052 KB
testcase_21 AC 242 ms
14,412 KB
testcase_22 AC 95 ms
8,192 KB
testcase_23 AC 71 ms
7,044 KB
testcase_24 AC 225 ms
22,820 KB
testcase_25 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

struct State {
    int at;
    ll cost;
    ll maxc;
    int prev;
    State(int at, ll cost, int prev) : at(at), cost(cost), maxc(0), prev(prev) {}
    State(int at, ll cost, ll maxc, int prev) : at(at), cost(cost), maxc(maxc), prev(prev) {}
    bool operator>(const State& s) const {
        if (cost != s.cost) return cost > s.cost;
        return maxc > s.maxc;
    }
};

struct Edge {
  int to;
  ll cost;
  Edge(int to, ll cost) : to(to), cost(cost) {}
};

typedef vector<vector<Edge> > AdjList; //隣接リスト

const ll INF = 1e18;
const int NONE = -1;

AdjList graph;

//sは始点、mincは最短経路のコスト、Prevは最短経路をたどる際の前の頂点
void dijkstra(int s, vector<ll>& minc, vector<int>& Prev){
    priority_queue<State, vector<State>, greater<State> > pq;
    pq.push(State(s, 0, NONE));
    while(!pq.empty()) {
        State cur = pq.top();
        pq.pop();
        if (minc[cur.at] <= cur.cost) continue;
        minc[cur.at] = cur.cost;
        Prev[cur.at] = cur.prev;
        for(Edge e : graph[cur.at]) {
            ll cost = cur.cost + e.cost;
            if (minc[e.to] <= cost) continue;
            pq.push(State(e.to, cost, cur.at));
        }
    }
}

void dijkstra2(int s, vector<ll>& minc, vector<int>& Prev){
    priority_queue<State, vector<State>, greater<State> > pq;
    vector<ll> mxc(minc.size(), 0);
    pq.push(State(s, 0, 0, NONE));
    while(!pq.empty()) {
        State cur = pq.top();
        pq.pop();
        if (minc[cur.at] < cur.cost || (minc[cur.at] == cur.cost && mxc[cur.at] <= cur.maxc)) continue;
        minc[cur.at] = cur.cost;
        Prev[cur.at] = cur.prev;
        mxc[cur.at] = cur.maxc;
        for(Edge e : graph[cur.at]) {
            ll maxc = max(e.cost, cur.maxc);
            ll cost = cur.cost;
            if (cur.maxc < maxc) {
                cost += cur.maxc;
            } else {
                cost += e.cost;
            }
            if (minc[e.to] < cost || (minc[e.to] == cost && mxc[e.to] <= maxc)) continue;
            pq.push(State(e.to, cost, maxc, cur.at));
        }
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    graph.resize(n);
    for (int i = 0; i < m; i++) {
        int a, b;
        ll c;
        cin >> a >> b >> c;
        a--; b--;
        graph[a].emplace_back(b, c);
        graph[b].emplace_back(a, c);
    }

    vector<ll> minc(n, INF), minc2(n, INF);
    vector<int> p1(n, NONE), p2(n, NONE);

    dijkstra(0, minc, p1);
    dijkstra2(0, minc2, p2);

    for (int i = 0; i < n; i++) {
        cout << minc[i] + minc2[i] << endl;
    }
    return 0;
}
0