結果

問題 No.807 umg tours
ユーザー sykwersykwer
提出日時 2019-03-22 22:19:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 530 ms / 4,000 ms
コード長 3,132 bytes
コンパイル時間 1,398 ms
コンパイル使用メモリ 125,044 KB
実行使用メモリ 32,324 KB
最終ジャッジ日時 2023-08-15 12:01:38
合計ジャッジ時間 7,975 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 293 ms
23,488 KB
testcase_12 AC 274 ms
19,472 KB
testcase_13 AC 393 ms
26,188 KB
testcase_14 AC 162 ms
13,188 KB
testcase_15 AC 130 ms
11,700 KB
testcase_16 AC 427 ms
28,452 KB
testcase_17 AC 529 ms
31,904 KB
testcase_18 AC 512 ms
31,860 KB
testcase_19 AC 530 ms
32,324 KB
testcase_20 AC 303 ms
18,808 KB
testcase_21 AC 320 ms
19,432 KB
testcase_22 AC 128 ms
10,384 KB
testcase_23 AC 95 ms
8,476 KB
testcase_24 AC 297 ms
23,904 KB
testcase_25 AC 518 ms
31,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* ---------- STL Libraries ---------- */
// IO library
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <ios>
#include <iostream>

// algorithm library
#include <algorithm>
#include <cmath>
#include <numeric>
#include <random>
#include <cstring>

// container library
#include <array>
#include <bitset>
#include <deque>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#include <stack>

/* ---------- Namespace ---------- */
using namespace std;

/* ---------- Type ---------- */
using ll = long long;
#define int ll
#define P pair<ll, ll>

/* ---------- Constants  */
const double PI = 3.141592653589793238462643383279;
const ll MOD = 1e9 + 7;
const int INF = 1LL << 55;

vector<int> distances;

class DirectedGraph {
    struct edge {
        int to;
        int cost;
    };

public:
    int V;
    vector<vector<edge>> G;

    DirectedGraph(int v) : G(v), V(v) {}

    void add_edge(int u, int v, int cost) {
        G[u].push_back({v, cost});
    }

    // O(E * logV)
    vector<int> dijkstra(int s) {
        vector<int> d(V, INF);
        d[s] = 0;

        priority_queue<P, vector<P>, greater<>> que;
        que.push({0, s});

        while (!que.empty()) {
            P  p = que.top(); que.pop();
            int v = p.second;
            if (d[v] < p.first) continue;

            for (edge e : G[v]) {
                if (d[e.to] > d[v] + e.cost) {
                    d[e.to] = d[v] + e.cost;
                    que.push({d[e.to], e.to});
                }
            }
        }

        return move(d);
    }
};

class NewDirectedGraph {
    struct edge {
        int to;
        int cost;
    };

public:
    int V;
    vector<vector<edge>> G;

    NewDirectedGraph(int v) : G(v), V(v) {}

    void add_edge(int u, int v, int cost) {
        G[u].push_back({v, cost});
    }

    // O(E * logV)
    vector<int> dijkstra(int s) {
        vector<int> new_dis(V, INF);
        new_dis[s] = 0;

        priority_queue<P, vector<P>, greater<>> que;
        que.push({0, s});

        while (!que.empty()) {
            P  p = que.top(); que.pop();
            int v = p.second;
            if (new_dis[v] < p.first) continue;

            for (edge e : G[v]) {
                int to_dis = min(new_dis[e.to], min(distances[v], new_dis[v] + e.cost));
                if (new_dis[e.to] > to_dis) {
                    new_dis[e.to] = to_dis;
                    que.push({new_dis[e.to], e.to});
                }
            }
        }

        return move(new_dis);
    }
};

signed main() {
    int N, M;
    cin >> N >> M;
    DirectedGraph dg = DirectedGraph(N);
    NewDirectedGraph ndg = NewDirectedGraph(N);
    for (int i = 0; i < M; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        a--; b--;
        dg.add_edge(a, b, c);
        dg.add_edge(b, a, c);
        ndg.add_edge(a, b, c);
        ndg.add_edge(b, a, c);
    }

    distances = dg.dijkstra(0);
    vector<int> new_distances = ndg.dijkstra(0);

    for (int i = 0; i < N; i++) cout << distances[i] + new_distances[i] << endl;

    return 0;
}
0