結果

問題 No.416 旅行会社
コンテスト
ユーザー pekempey
提出日時 2016-08-27 02:42:33
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 454 ms / 4,000 ms
コード長 996 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,796 ms
コンパイル使用メモリ 190,520 KB
実行使用メモリ 161,920 KB
最終ジャッジ日時 2026-05-25 12:18:47
合計ジャッジ時間 8,565 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

template<class T1, class T2>
bool chmax(T1 &a, T2 b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}

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

	vector<map<int, int>> g(n);
	for (int i = 0; i < m; i++) {
		int a, b;
		scanf("%d %d", &a, &b);
		a--; b--;
		g[a][b] = Q + 1;
		g[b][a] = Q + 1;
	}

	for (int i = 0; i < Q; i++) {
		int c, d;
		scanf("%d %d", &c, &d);
		c--; d--;
		g[c][d] = i + 1;
		g[d][c] = i + 1;
	}

	vector<queue<int>> q(Q + 2);
	q[Q + 1].push(0);

	vector<int> dist(n);
	dist[0] = Q + 1;

	for (int i = Q + 1; i >= 1; i--) {
		while (!q[i].empty()) {
			int curr = q[i].front(); q[i].pop();

			if (i < dist[curr]) continue;

			for (auto kv : g[curr]) {
				int next = kv.first;
				int cost = kv.second;

				if (chmax(dist[next], min(dist[curr], cost))) {
					q[dist[next]].push(next);
				}
			}
		}
	}

	for (int i = 1; i < n; i++) {
		if (dist[i] == Q + 1) dist[i] = -1;
		printf("%d\n", dist[i]);
	}
}
0