結果

問題 No.807 umg tours
ユーザー mamekinmamekin
提出日時 2019-03-22 22:47:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 480 ms / 4,000 ms
コード長 1,920 bytes
コンパイル時間 1,371 ms
コンパイル使用メモリ 134,072 KB
実行使用メモリ 19,084 KB
最終ジャッジ日時 2023-08-15 12:03:51
合計ジャッジ時間 7,714 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 255 ms
10,996 KB
testcase_12 AC 274 ms
11,108 KB
testcase_13 AC 364 ms
12,876 KB
testcase_14 AC 160 ms
7,604 KB
testcase_15 AC 124 ms
6,748 KB
testcase_16 AC 373 ms
14,144 KB
testcase_17 AC 480 ms
18,436 KB
testcase_18 AC 476 ms
19,084 KB
testcase_19 AC 460 ms
16,000 KB
testcase_20 AC 290 ms
11,080 KB
testcase_21 AC 301 ms
11,356 KB
testcase_22 AC 122 ms
6,668 KB
testcase_23 AC 94 ms
5,956 KB
testcase_24 AC 304 ms
17,608 KB
testcase_25 AC 479 ms
18,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
#include <memory>
#include <regex>
using namespace std;

class Edge
{
public:
    int to, cost;
    Edge(int to, int cost){
        this->to = to;
        this->cost = cost;
    }
};

const long long INF = LLONG_MAX / 2;

int main()
{
    int n, m;
    cin >> n >> m;
    vector<vector<Edge> > edges(n);
    for(int i=0; i<m; ++i){
        int a, b, c;
        cin >> a >> b >> c;
        -- a;
        -- b;
        edges[a].push_back(Edge(b, c));
        edges[b].push_back(Edge(a, c));
    }

    vector<vector<long long> > dp(2, vector<long long>(n, INF));
    dp[0][0] = dp[1][0] = 0;
    priority_queue<tuple<long long, int, int> > pq;
    pq.push(make_tuple(-0, 0, 0));
    pq.push(make_tuple(-0, 1, 0));
    while(!pq.empty()){
        long long cost;
        int cnt, curr;
        tie(cost, cnt, curr) = pq.top();
        cost *= -1;
        pq.pop();
        if(dp[cnt][curr] < cost)
            continue;

        for(const Edge& e : edges[curr]){
            int next = e.to;
            if(cnt == 0 && cost < dp[1][next]){
                dp[1][next] = cost;
                pq.push(make_tuple(-cost, 1, next));
            }
            long long cost2 = cost + e.cost;
            if(cost2 < dp[cnt][next]){
                dp[cnt][next] = cost2;
                pq.push(make_tuple(-cost2, cnt, next));
            }
        }
    }

    for(int i=0; i<n; ++i){
        long long ans = dp[0][i] + dp[1][i];
        cout << ans << endl;
    }

    return 0;
}
0