写wa了,没开longlong,丢人
n的数据范围很小,那么直接Floyd搞起来,跑完最短路预处理结束之后开始进行判断。
由于给的budget最大为$1e8$,那么如果硬搞是过不去的,直接二分,AC.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 105;
ll n, m, q;
ll s, t, b;
ll maze[N][N], len;
bool check(int x){
ll r = 1, sum = 0;
for(int i = 1; i <= len; i ++){
r *= x;
sum += r;
if(sum > b)
return 0;
}
if(sum <= b)
return 1;
else
return 0;
}
int main(){
cin >> n >> m;
memset(maze, 0x3f3f3f3f, sizeof maze);
for(int i = 1; i <= m; i ++){
int a, b;
cin >> a >> b;
maze[a][b] = maze[b][a] = 1;
}
for(int k = 1; k <= n; k ++){
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= n; j ++){
if(maze[i][j] > maze[i][k] + maze[k][j]){
maze[i][j] = maze[i][k] + maze[k][j];
maze[j][i] = maze[i][j];
}
}
}
}
cin >> q;
for(int i = 1; i <= q; i ++){
cin >> s >> t >> b;
ll l = 0, r = b;
len = maze[s][t];
//cout << days << endl;
while(l < r){
ll mid = (l + r + 1) >> 1;
if(check(mid)){
l = mid;
}else
r = mid - 1;
}
cout << l << endl;
}
return 0;
}