結果

問題 No.807 umg tours
ユーザー sinsincoscossinsincoscos
提出日時 2019-03-16 13:58:44
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 1,724 bytes
コンパイル時間 544 ms
コンパイル使用メモリ 69,760 KB
実行使用メモリ 812,180 KB
最終ジャッジ日時 2023-09-14 15:14:56
合計ジャッジ時間 5,642 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 MLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
#include <cassert>
using namespace std;
typedef long long ll;
typedef pair<ll,int> P;
int N,M,used[100010] = {};
vector<vector<P>> v(100010),tree(100010);
ll dp[100010][2] = {},inf = 1e18;

void dfs(int n,int m){
    for(auto x:tree[n]){
        if(x.second!=m){
            dfs(x.second,n);
            dp[n][1] = min(dp[n][1],dp[x.second][1]+x.first);
        }
    }
}

int main(){
    cin >> N >> M;
    assert(2<=N && N<=100000);
    assert(N-1<=M && M<=min(100000,N*(N-1)/2));
    int a,b; ll c;
    for(int i=0;i<M;i++){
        cin >> a >> b >> c;
        assert(1<=a && a<=N && 1<=b && b<=N && 1<=c && c<=1000000000);
        v[a].push_back(P(c,b));
        v[b].push_back(P(c,a));
    }
    for(int i=2;i<=N;i++){
        dp[i][0] = inf;
        dp[i][1] = inf;
    }
    priority_queue<P,vector<P>,greater<P>> Q;
    Q.push(P(0,1));
    while(!Q.empty()){
        P p = Q.top(); Q.pop();
        int n = p.second;
        used[n] = 1;
        if(dp[n][0]<p.first) continue;
        for(auto x:v[n]){
            if(dp[n][0]==dp[x.second][0]+x.first){
                tree[n].push_back(P(x.first,x.second));
                tree[x.second].push_back(P(x.first,n));
            }
            if(dp[x.second][0]>p.first+x.first){
                dp[x.second][0] = p.first+x.first;
                Q.push(P(dp[x.second][0],x.second));
            }
            if(used[x.second]!=1) dp[x.second][1] = min(min(dp[x.second][1],dp[n][1]+x.first),p.first);
        }
    }
    dfs(1,0);
    for(int i=1;i<=N;i++){
        assert(dp[i][0]!=inf && dp[i][1]!=inf);
        //cerr << dp[i][0] << " " << dp[i][1] << endl;
        cout << dp[i][0]+dp[i][1] << endl;
    }
}
0