結果

問題 No.807 umg tours
ユーザー kkey000kkey000
提出日時 2023-06-24 10:23:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,774 ms / 4,000 ms
コード長 2,407 bytes
コンパイル時間 1,647 ms
コンパイル使用メモリ 177,704 KB
実行使用メモリ 62,576 KB
最終ジャッジ日時 2023-09-14 05:54:55
合計ジャッジ時間 13,478 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 440 ms
16,324 KB
testcase_12 AC 656 ms
22,940 KB
testcase_13 AC 1,104 ms
36,468 KB
testcase_14 AC 380 ms
19,660 KB
testcase_15 AC 257 ms
18,760 KB
testcase_16 AC 613 ms
23,664 KB
testcase_17 AC 1,092 ms
26,392 KB
testcase_18 AC 1,774 ms
62,576 KB
testcase_19 AC 1,376 ms
38,072 KB
testcase_20 AC 310 ms
10,732 KB
testcase_21 AC 314 ms
11,028 KB
testcase_22 AC 125 ms
6,620 KB
testcase_23 AC 99 ms
5,900 KB
testcase_24 AC 289 ms
17,992 KB
testcase_25 AC 546 ms
27,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>


#define INF 10e9*10e4*2
#define rep(i, a) for (int i = 0; i < (a); i++)
using namespace std;

typedef struct edge
{
    int to;
    int cost;
} edge;

int main()
{
    long long int n, m;
    cin >> n >> m;

    vector<edge> graph[n];
    long long int u, v, c;

    long long int i, j;
    for (i = 0; i < m; i++)
    {
        cin >> u >> v >> c;
        edge e;
        e.to = v - 1;
        e.cost = c;
        graph[u - 1].push_back(e);
        e.to = u - 1;
        graph[v - 1].push_back(e);
    }

    long long int d[2][n];

    for (i = 0; i < n; i++)
    {
        if (i == 0)
        {
            d[0][i] = 0;
            d[1][i] = 0;
        }
        else
        {
            d[0][i] = INF;
            d[1][i] = INF;
        }
    }
    priority_queue<pair<pair<long long int, long long int>, long long int>, vector<pair<pair<long long int, long long int>, long long int>>, greater<pair<pair<long long int, long long int>, long long int>>> q;
    q.push(make_pair(make_pair(0, 0), 0));
    while (!q.empty())
    {
        pair<pair<long long int, long long int>, long long int> p = q.top();
        q.pop();

        long long int v = p.first.first;

        if ((p.second == 0) && (d[0][v] >= p.first.second))
        {
            for (i = 0; i < graph[v].size(); i++)
            {
                edge e = graph[v][i];
                if (p.second == 0)
                {
                    if (d[0][e.to] > d[0][v] + e.cost)
                    {
                        d[0][e.to] = d[0][v] + e.cost;
                        q.push(make_pair(make_pair(e.to, d[0][e.to]), 0));
                    }
                    if (d[1][e.to] > d[0][v])
                    {
                        d[1][e.to] = d[0][v];
                        q.push(make_pair(make_pair(e.to, d[1][e.to]), 1));
                    }
                }
            }
        }
        else if ((p.second == 1) && (d[1][v] >= p.first.second))
        {
            for (i = 0; i < graph[v].size(); i++)
            {
                edge e = graph[v][i];
                if (d[1][e.to] > d[1][v] + e.cost)
                {
                    d[1][e.to] = d[1][v] + e.cost;
                    q.push(make_pair(make_pair(e.to, d[1][e.to]), 1));
                }
            }
        }
    }
    for (i = 0; i < n; i++)
    {
        cout << (d[0][i] + d[1][i]) << endl;
    }
}
0