题解:P17890 [ICPC 2026 Nanchang I] Tourist
20111019Yu · · 题解
题意
初始
思路
考虑
令一条边的值为最早出现时间,考虑路径
code
::::success[
#include <bits/stdc++.h>
using namespace std;
int n, m, x, y, k, q, w;
struct Matrix{
int m[205][205];
Matrix operator * (const Matrix a) const{
Matrix c;
memset(c.m, 0x3f, sizeof(c.m));
for( int i = 1; i <= n; i++ ){
for( int k = 1; k <= n; k++ ){
for( int j = 1; j <= n; j++ ){
c.m[i][j] = min(c.m[i][j], max(m[i][k], a.m[k][j]));
}
}
}
return c;
}
} s, res;
template<typename type>
inline void read(type &x){
x=0;bool flag(0);char ch=getchar();
while(!isdigit(ch)) flag=ch=='-',ch=getchar();
while(isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
flag?x=-x:0;
}
template<typename type>
inline void write(type x){
x<0?x=-x,putchar('-'):0;static short Stack[50],top(0);
do Stack[++top]=x%10,x/=10; while(x);
while(top) putchar(Stack[top--]|48);
putchar('\n');
}
int main(){
// freopen("chess.in", "r", stdin);
// freopen("chess.out", "w", stdout);
read(n), read(m);
memset(s.m, 0x3f, sizeof(s.m));
for( int i = 1; i <= m; i++ ){
read(x), read(y);
s.m[x][y] = min(s.m[x][y], 0);
}
read(k);
for( int i = 1; i <= k; i++ ){
read(x), read(y);
s.m[x][y] = min(s.m[x][y], i);
}
memset(res.m, 0x3f, sizeof(res.m));
for( int i = 1; i <= n; i++ ) res.m[i][i] = 0;
read(q), read(w);
while(w){
if(w & 1) res = res * s;
s = s * s;
w >>= 1;
}
while(q--){
read(x), read(y);
if(res.m[x][y] > 1e9) write(-1);
else write(res.m[x][y]);
}
return 0;
}
/*
岁受谢,燕辞归。
去白日,骛太微。
吁嗟,羌斯路之焭焭兮,不可追。
欸董道兮常违。
蹇,䰟兮其往,荪奚儃佪。
徒离忧兮倾寤,孰将为。
䰟兮其往。䰟兮其往。
无悲。
何方归,燕双宿。
何方逢,银釭烛。
梦觉。玉钗分处、玄鸟去云悠悠。
留雨铃、凭谁诉。
恨芳年倏忽、朱颜难驻。
却念青山永固、总凝目。
但为何故。
知我相负、未负。
*/
::::