結果

問題 No.416 旅行会社
ユーザー pekempey
提出日時 2016-08-27 02:42:33
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 516 ms / 4,000 ms
コード長 996 bytes
コンパイル時間 2,601 ms
コンパイル使用メモリ 173,532 KB
実行使用メモリ 161,776 KB
最終ジャッジ日時 2024-12-14 20:01:00
合計ジャッジ時間 7,431 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 21
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:20:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   20 |                 scanf("%d %d", &a, &b);
      |                 ~~~~~^~~~~~~~~~~~~~~~~
main.cpp:28:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   28 |                 scanf("%d %d", &c, &d);
      |                 ~~~~~^~~~~~~~~~~~~~~~~

ソースコード

diff #

#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