题目:
题目链接:
题解:
最短路+二分
#include <bits/stdc++.h>
using namespace std;
int pos[105][105];
int inf =0x3f3f3f3f;
int n,m;
long long s,t,w;
void floyd()
{
for(int k=1;k<=n;k++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
pos[i][j]=min(pos[i][j],pos[i][k]+pos[k][j]);
}
}
}
}
bool check(int x)
{
long long ans=0;
for(int i=1;i<=pos[s][t];i++)
{
long long r=1;
for(int j=1;j<=i;j++)
{
r*=x;
}
ans+=r;
if(ans>w) break;
}
//cout<<ans<<endl;
if(ans>w) return true;
else return false;
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
pos[i][j]=i==j?0:inf;
}
}
while(m--)
{
int u,v;
cin>>u>>v;
pos[u][v]=pos[v][u]=1;
}
floyd();
int q;
cin>>q;
while(q--)
{
cin>>s>>t>>w;
long long l=0,r=w;
while(l<r)
{
int mid=(l+r+1)>>1;
if(check(mid)) r=mid-1;
else l=mid;
}
cout<<l<<endl;
}
return 0;
}