結果

問題 No.1601 With Animals into Institute
ユーザー nawawan
提出日時 2021-07-10 14:23:28
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 564 ms / 3,000 ms
コード長 1,717 bytes
コンパイル時間 1,841 ms
コンパイル使用メモリ 180,568 KB
実行使用メモリ 34,124 KB
最終ジャッジ日時 2024-07-02 02:23:23
合計ジャッジ時間 10,914 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:38:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
   38 |         auto [dis, id] = q.top();
      |              ^
main.cpp:54:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
   54 |         auto [dis, id] = q.top();
      |              ^

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
struct edge{
    int to;
    long long cost;
};
typedef pair<long long, int> P;
int main(){
    int N, M;
    cin >> N >> M;
    vector<vector<edge>> G(N + 2 * M);
    vector<int> mon;
    for(int i = 0; i < M; i++){
        int a, b, x;
        long long c;
        cin >> a >> b >> c >> x;
        a--;
        b--;
        if(x == 1) {
            G[a].push_back({i + N, c});
            G[i + N].push_back({b, 0});
            G[b].push_back({i + M + N, c});
            G[i + M + N].push_back({a, 0});
            mon.push_back(i + N);
            mon.push_back(i + M + N);
        }
        else{
            G[a].push_back({b, c});
            G[b].push_back({a, c});
        }
    }
    long long INF = 10000000000000000;
    vector<long long> d(N + 2 * M, INF);
    priority_queue<P, vector<P>, greater<P>> q;
    d[N - 1] = 0;
    q.push({0, N - 1});
    while(!q.empty()){
        auto [dis, id] = q.top();
        q.pop();
        if(d[id] < dis) continue;
        for(edge e : G[id]){
            if(d[e.to] > e.cost + dis){
                d[e.to] = e.cost + dis;
                q.push({d[e.to], e.to});
            }
        }
    }
    vector<long long> ans(N + 2 * M, INF);
    for(int i = 0; i < (int)mon.size(); i++){
        q.push({d[mon[i]], mon[i]});
        ans[mon[i]] = d[mon[i]];
    }
    while(!q.empty()){
        auto [dis, id] = q.top();
        q.pop();
        if(ans[id] < dis) continue;
        for(edge e : G[id]){
            if(ans[e.to] > e.cost + dis){
                ans[e.to] = e.cost + dis;
                q.push({ans[e.to], e.to});
            }
        }
    }
    for(int i  = 0; i < N - 1; i++) cout << ans[i] << endl;
}
0