結果

問題 No.1601 With Animals into Institute
ユーザー 25__toma25__toma
提出日時 2021-07-10 14:27:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 599 ms / 3,000 ms
コード長 2,664 bytes
コンパイル時間 2,433 ms
コンパイル使用メモリ 216,728 KB
実行使用メモリ 22,852 KB
最終ジャッジ日時 2024-07-02 02:24:43
合計ジャッジ時間 11,711 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 9 ms
5,376 KB
testcase_04 AC 9 ms
5,376 KB
testcase_05 AC 9 ms
5,376 KB
testcase_06 AC 557 ms
22,808 KB
testcase_07 AC 562 ms
22,800 KB
testcase_08 AC 556 ms
22,812 KB
testcase_09 AC 552 ms
22,852 KB
testcase_10 AC 558 ms
22,816 KB
testcase_11 AC 571 ms
22,820 KB
testcase_12 AC 566 ms
22,824 KB
testcase_13 AC 572 ms
22,808 KB
testcase_14 AC 599 ms
22,796 KB
testcase_15 AC 591 ms
22,804 KB
testcase_16 AC 556 ms
22,852 KB
testcase_17 AC 562 ms
22,792 KB
testcase_18 AC 9 ms
5,376 KB
testcase_19 AC 9 ms
5,376 KB
testcase_20 AC 10 ms
5,376 KB
testcase_21 AC 9 ms
5,376 KB
testcase_22 AC 9 ms
5,376 KB
testcase_23 AC 9 ms
5,376 KB
testcase_24 AC 9 ms
5,376 KB
testcase_25 AC 9 ms
5,376 KB
testcase_26 AC 9 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 2 ms
5,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