QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#672260#6772. Spicy RestaurantACAAA#TL 0ms9980kbC++231.1kb2024-10-24 16:11:412024-10-24 16:11:41

Judging History

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

  • [2024-10-24 16:11:41]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:9980kb
  • [2024-10-24 16:11:41]
  • 提交

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() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	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";
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: