QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#672254#6772. Spicy RestaurantACAAA#TL 0ms9904kbC++231.0kb2024-10-24 16:10:142024-10-24 16:10:15

Judging History

你现在查看的是最新测评结果

  • [2024-10-24 16:10:15]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:9904kb
  • [2024-10-24 16:10:14]
  • 提交

answer

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int N = 5e5 + 10, M = 105;
int n, m,t, w[N],vis[N],dist[N][M];
vector<int>a[N];
void bfs(int val) {
	queue<int>q;
	for (int i = 1; i <= n; i++) {
		vis[i] = 0;
		dist[i][val] = 1e9;
		if (w[i] == val) {
			dist[i][val] = 0;
			q.push(i);
		}
	}
	while (!q.empty()) {
		int u = q.front();
		q.pop();
		if (vis[u])continue;
		vis[u] = 1;
		for (int i = 0; i < a[u].size(); i++) {
			int v = a[u][i];
			if (!vis[v] && dist[v][val] > dist[u][val] + 1) {
				dist[v][val] = dist[u][val] + 1;
				q.push(v);
			}
		}
	}
}
int main() {
	cin >> n >> m >> t;
	for (int i = 1; i <= n; i++)
		cin >> w[i];
	for (int i = 1; i <= m; i++) {
		int u, v;
		cin >> u >> v;
		a[u].push_back(v);
		a[v].push_back(u);
	}
	for (int i = 1; i <= n; i++)
		bfs(i);
	while (t--) {
		int pos, k;
		cin >> pos >> k;
		int ans = 1e9;
		for (int i = 1; i <= k; i++)
			ans = min(ans, dist[pos][i]);
		if (ans == 1e9)cout << "-1\n";
		else cout << ans << "\n";
	}
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 9904kb

input:

4 4 5
5 4 2 3
1 2
2 3
3 4
4 1
1 1
1 2
1 3
1 4
1 5

output:

-1
2
1
1
0

result:

ok 5 lines

Test #2:

score: -100
Time Limit Exceeded

input:

50000 100000 100000
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 ...

output:


result: