結果

問題 No.3482 Quod Erat Demonstrandum
コンテスト
ユーザー The Forsaking
提出日時 2026-03-28 17:05:17
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 54 ms / 2,000 ms
コード長 1,393 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,263 ms
コンパイル使用メモリ 214,484 KB
実行使用メモリ 11,520 KB
最終ジャッジ日時 2026-03-28 17:05:25
合計ジャッジ時間 7,156 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
const int N = 2000010, MOD = 998244353, INF = 0x3f3f3f3f;
int n, m, w[N];


int c;
bool st[N];
int d1[N], dn[N], e[N], ne[N], h[N], idx;
void add(int a, int b, int c) { e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++; }

void dj(int* d, int u) {
    memset(d, 0x3f, sizeof(int) * (n + 10));
    d[u] = 0;
    queue<int> q;
    q.push(u);
    while (q.size()) {
        auto t = q.front(); q.pop();
        for (int i = h[t]; ~i; i = ne[i]) {
            int j = e[i];
            if (w[i] == 1 && d[j] > d[t] + 1) {
                d[j] = d[t] + 1;
                q.push(j);
            }
        }
    }
}

void solve() {
    scanf("%d%d", &n, &m);
    memset(h, -1, sizeof(int) * (n + 10));
    for (int i = 1, a, b, c; i < m + 1; i++) {
        scanf("%d%d%d", &a, &b, &c);
        add(a, b, c), add(b, a, c);
    }
    dj(d1, 1), dj(dn, n);
    int res = d1[n];
    for (int i = 1; i < n + 1; i++) {
        for (int j = h[i]; ~j; j = ne[j]) {
            int k = e[j];
            if (w[j] == 2) res = min(res, d1[i] + dn[k] + 1);
        }
    }
    if (res >= INF) puts("Unknown");
    else {
        if (res == d1[n]) puts("Same");
        else puts("Different");
        printf("%d\n", res);
    }
}


int main() {
    int T;
    cin >> T;
    while (T--) solve();
    return 0;
}
0