L. Spicy Restaurant (多源bfs
L. Spicy Restaurant
思路:
枚举一个辣度 \(c\),把所有 \(w_i=c\) 的点作为起点进行 \(bfs\)
预处理出 \(dis[j][i]\) 表示 \(i\) 点距离辣度 \(j\) 有多远
然后顺着更新一遍 \(dis[i][j] = min(dis[i][j-1], dis[i][j])\)
因为满足 \(a_j<=c\) 的辣度都是合法的
有点卡常,\(scanf\) 读入
点击查看代码
#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define ld long double
#define all(x) x.begin(), x.end()
#define mem(x, d) memset(x, d, sizeof(x))
#define eps 1e-6
using namespace std;
const int maxn = 1e5 + 9;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll n, m;
int q;
int dis[maxn][109];
int w[maxn];
vector <int> e[maxn];
void bfs(int c){
queue <int> q;
for(int i = 1; i <= n; ++i) if(w[i] == c) q.push(i), dis[i][c] = 0;
while(!q.empty()){
int x = q.front();q.pop();
for(auto to : e[x]){
if(dis[to][c] > dis[x][c] + 1){
dis[to][c] = dis[x][c] + 1;
q.push(to);
}
}
}
}
void work()
{
mem(dis, 0x3f);
cin >> n >> m >> q;
for(int i = 1; i <= n; ++i) scanf("%d", &w[i]);
while(m--){
int x, y;scanf("%d %d", &x, &y);
e[x].push_back(y);e[y].push_back(x);
}
for(int i = 100; i >= 1; --i) bfs(i);
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= 100; ++j)
dis[i][j] = min(dis[i][j-1], dis[i][j]);
while(q--){
int l, r;scanf("%d %d", &l, &r);
printf("%d\n", dis[l][r] == inf ? -1 : dis[l][r]);
}
}
int main()
{
// ios::sync_with_stdio(0);
// int TT;cin>>TT;while(TT--)
work();
return 0;
}
遗迹是曾经讴歌繁荣的生命留下的缺页的梦。

浙公网安备 33010602011771号