結果

問題 No.1601 With Animals into Institute
ユーザー 25__toma25__toma
提出日時 2021-07-10 14:27:55
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 560 ms / 3,000 ms
コード長 2,664 bytes
コンパイル時間 2,414 ms
コンパイル使用メモリ 213,700 KB
実行使用メモリ 22,604 KB
最終ジャッジ日時 2023-09-14 19:25:44
合計ジャッジ時間 11,706 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 8 ms
4,376 KB
testcase_04 AC 9 ms
4,376 KB
testcase_05 AC 9 ms
4,380 KB
testcase_06 AC 555 ms
22,444 KB
testcase_07 AC 554 ms
22,396 KB
testcase_08 AC 554 ms
22,372 KB
testcase_09 AC 555 ms
22,408 KB
testcase_10 AC 557 ms
22,468 KB
testcase_11 AC 559 ms
22,412 KB
testcase_12 AC 551 ms
22,392 KB
testcase_13 AC 558 ms
22,412 KB
testcase_14 AC 553 ms
22,604 KB
testcase_15 AC 551 ms
22,432 KB
testcase_16 AC 553 ms
22,452 KB
testcase_17 AC 560 ms
22,380 KB
testcase_18 AC 9 ms
4,376 KB
testcase_19 AC 8 ms
4,376 KB
testcase_20 AC 8 ms
4,380 KB
testcase_21 AC 8 ms
4,376 KB
testcase_22 AC 8 ms
4,376 KB
testcase_23 AC 8 ms
4,376 KB
testcase_24 AC 8 ms
4,380 KB
testcase_25 AC 9 ms
4,376 KB
testcase_26 AC 9 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include"bits/stdc++.h"

using namespace std;
#define REP(k,m,n) for(int (k)=(m);(k)<(n);(k)++)
#define rep(i,n) REP((i),0,(n))
using ll = long long;
using pll = pair<ll, ll>;
using tp3 = tuple<ll, ll, ll>;
constexpr int INF = 1 << 28;
constexpr ll INFL = 1ll << 60;
constexpr int dh[4] = { 0,1,0,-1 };
constexpr int dw[4] = { -1,0,1,0 };
bool isin(const int H, const int W, const int h, const int w) {
    return 0 <= h && h < H && 0 <= w && w < W;
}
// ============ template finished ============
using Pos = ll;
using Dist = ll;
using HaveAnimal = bool;
using Edge = tuple<Pos, Dist, HaveAnimal>;
using Graph = vector<vector<Edge>>;

vector<pll> calc_goals_by_dijkstra(const Graph& graph) {
    const int N = graph.size();
    vector<ll> dists(N, INFL);
    vector<ll> haveAnimalDists(N, INFL);
    priority_queue<pll, vector<pll>, greater<pll>> pq;

    dists[N - 1] = 0;
    pq.push(make_pair(0, N - 1));
    while (!pq.empty()) {
        auto[dist, pos] = pq.top(); pq.pop();
        for (auto[nextPos, edgeDist, haveAnimal] : graph[pos]) {
            ll nextDist = dist + edgeDist;
            if (nextDist < dists[nextPos]) {
                dists[nextPos] = nextDist;
                pq.push(make_pair(nextDist, nextPos));
            }
            if (haveAnimal && nextDist < haveAnimalDists[nextPos]) {
                haveAnimalDists[nextPos] = nextDist;
            }
        }
    }
    vector<pll> goals;
    rep(i, N)if (haveAnimalDists[i] != INFL) {
        goals.push_back(make_pair(haveAnimalDists[i], i));
    }
    return goals;
}

vector<ll> dijkstra_with_many_goals(const Graph& graph, const vector<pll>& goals) {
    const int N = graph.size();
    vector<ll> dists(N, INFL);
    priority_queue<pll, vector<pll>, greater<pll>> pq;
    for (auto[dist, pos] : goals) {
        dists[pos] = dist;
        pq.push(make_pair(dist, pos));
    }
    while (!pq.empty()) {
        auto[dist, pos] = pq.top(); pq.pop();
        for (auto[nextPos, edgeDist, _] : graph[pos]) {
            ll nextDist = dist + edgeDist;
            if (nextDist < dists[nextPos]) {
                dists[nextPos] = nextDist;
                pq.push(make_pair(nextDist, nextPos));
            }
        }
    }
    return dists;
}

int main()
{
    int N, M;
    cin >> N >> M;
    Graph graph(N);
    rep(i, M) {
        ll A, B, C, X;
        cin >> A >> B >> C >> X;
        A--; B--;
        graph[A].push_back(make_tuple(B, C, X == 1));
        graph[B].push_back(make_tuple(A, C, X == 1));
    }
    auto goals = calc_goals_by_dijkstra(graph);
    auto dists = dijkstra_with_many_goals(graph, goals);
    rep(i, N - 1)cout << dists[i] << endl;
    return 0;
}
0