QOJ.ac
QOJ
ID | 提交记录ID | 题目 | Hacker | Owner | 结果 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|
#782 | #322325 | #6772. Spicy Restaurant | 18953267621 | mshcherba | Failed. | 2024-08-18 20:30:59 | 2024-08-18 20:30:59 |
詳細信息
Extra Test:
Accepted
time: 0ms
memory: 54792kb
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
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#322325 | #6772. Spicy Restaurant | mshcherba# | AC ✓ | 362ms | 62212kb | C++20 | 1.3kb | 2024-02-06 20:39:05 | 2024-02-06 20:39:05 |
answer
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;
const int N = 1 << 17;
const int K = 103;
VI g[N];
int dist[K][N];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, m, que;
cin >> n >> m >> que;
VI w(n);
for (int& wi : w)
cin >> wi;
FOR(i, 0, m)
{
int u, v;
cin >> u >> v;
u--;
v--;
g[u].PB(v);
g[v].PB(u);
}
FOR(sp, 0, K)
{
fill(dist[sp], dist[sp] + n, -1);
queue<int> q;
FOR(i, 0, n)
{
if (w[i] == sp)
{
q.push(i);
dist[sp][i] = 0;
}
}
while (!q.empty())
{
int v = q.front();
q.pop();
for (int to : g[v])
{
if (dist[sp][to] == -1)
{
q.push(to);
dist[sp][to] = dist[sp][v] + 1;
}
}
}
}
while (que--)
{
int p, a;
cin >> p >> a;
p--;
int d = -1;
FOR(sp, 1, a + 1)
{
if (dist[sp][p] == -1)
continue;
if (d == -1 || (dist[sp][p] < d))
d = dist[sp][p];
}
cout << d << "\n";
}
return 0;
}