結果

問題 No.807 umg tours
ユーザー sinsincoscossinsincoscos
提出日時 2019-03-16 14:54:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,615 bytes
コンパイル時間 513 ms
コンパイル使用メモリ 65,156 KB
実行使用メモリ 12,900 KB
最終ジャッジ日時 2023-09-14 15:16:19
合計ジャッジ時間 6,561 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
12,900 KB
testcase_01 AC 20 ms
12,784 KB
testcase_02 TLE -
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>
using namespace std;
typedef long long ll;

int N,M,used[1010] = {};
ll cost[1010][1010] = {},ans[1010] = {},inf = 1e18;
ll dist[1010][1010] = {};//辺iのコストを0にしたときの1からの距離の最小値


void dijkstra(int id,int a,int b){
    for(int i=1;i<=N;i++){
        dist[id][i] = inf;
        used[i] = 0;
    }
    dist[id][1] = 0;
    while(1){
        int u = -1;
        for(int i=1;i<=N;i++){
            if(used[i]==0 && (u==-1 || dist[id][u]>dist[id][i])){
                u = i;
            }
        }
        used[u] = 1;
        if(u==-1) break;
        for(int i=1;i<=N;i++){
            ll c = cost[u][i];
            if(c==inf) continue;
            if((u==a && i==b) || (u==b && i==a)) c = 0;
            dist[id][i] = min(dist[id][i],dist[id][u]+c);
        }
    }
//    for(int i=1;i<=N;i++) cout << dist[id][i] << endl;
}

int main(){
    cin >> N >> M;
    int a,b; ll c;
    for(int i=1;i<=N;i++){
        ans[i] = inf;
        for(int j=1;j<=N;j++){
            cost[i][j] = inf;
        }
    }
    for(int i=1;i<=M;i++){
        cin >> a >> b >> c;
        cost[a][b] = c;
        cost[b][a] = c;
    }
    int cnt = 1;
    dijkstra(0,0,0);
    for(int i=1;i<=N;i++){
        for(int j=1;j<=N;j++){
            if(cost[i][j]!=inf){
                dijkstra(cnt,i,j);
                cnt++;
            }
        }
    }
    for(int i=1;i<=N;i++){
        for(int j=1;j<=cnt-1;j++){
     //       cerr << dist[0][i] << " " << dist[j][i] << endl;
            ans[i] = min(ans[i],dist[0][i]+dist[j][i]);
        }
        cout << ans[i] << endl;
    }
}
0