結果

問題 No.807 umg tours
ユーザー sinsincoscossinsincoscos
提出日時 2019-03-16 14:59:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,619 bytes
コンパイル時間 534 ms
コンパイル使用メモリ 66,332 KB
実行使用メモリ 16,604 KB
最終ジャッジ日時 2023-09-14 15:16:12
合計ジャッジ時間 7,566 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
8,864 KB
testcase_01 AC 14 ms
8,876 KB
testcase_02 AC 30 ms
10,896 KB
testcase_03 AC 24 ms
10,916 KB
testcase_04 AC 9 ms
8,840 KB
testcase_05 AC 10 ms
8,728 KB
testcase_06 AC 33 ms
12,996 KB
testcase_07 AC 18 ms
10,908 KB
testcase_08 AC 2 ms
5,640 KB
testcase_09 AC 3 ms
6,212 KB
testcase_10 AC 3 ms
6,116 KB
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
using namespace std;
typedef long long ll;

int N,M,used[1010] = {};
int ok[1010][1010] = {};
ll cost[1010][1010] = {},ans[1010] = {},inf = 1e18;
ll dist[10010][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(ok[j][i]==0 && cost[i][j]!=inf){
                dijkstra(cnt,i,j);
                cnt++;
                ok[i][j] = 1;
            }
        }
    }
    for(int i=1;i<=N;i++){
        for(int j=1;j<=M;j++){
            ans[i] = min(ans[i],dist[0][i]+dist[j][i]);
        }
        cout << ans[i] << endl;
    }
}
0