打了一个双指针+每次bfs判可行性,理论复杂度应该是O(m2+mn)的,并且常数应该也不大,但却炸了一个点。求大佬看看是不是复杂度实现的有问题。
#include<bits/stdc++.h>
#define int long long
using namespace std;
vector<int> e;
vector<int> to[501];
vector<int> cost[501];
bool vst[501];
int n,m;
int o,g;
bool bfs(int ulmt,int dlmt){
for(int i=1;i<=n;i++)vst[i]=0;
queue<int> q;
q.push(o);
while(!q.empty()){
int w=q.front();
q.pop();
if(w==g)return 1;
vst[w]=1;
for(int i=0;i<to[w].size();i++){
if(cost[w][i]<=ulmt and cost[w][i]>=dlmt){
if(!vst[to[w][i]])q.push(to[w][i]);
}
}
}
return 0;
}
bool cmp2(int f1,int f2,int f3,int f4){
return f1*f4 > f2*f3;
}
signed main(){
cin>>n>>m;
for(int i=1;i<=m;i++){
int f,t,s;
cin>>f>>t>>s;
to[f].push_back(t);
to[t].push_back(f);
cost[f].push_back(s);
cost[t].push_back(s);
e.push_back(s);
}
cin>>o>>g;
pair<int,int> ans;
ans.first=ans.second=-1;
int lr=0,rr=0;
sort(e.begin(),e.end());
while(lr<e.size()){
while(rr<e.size() and !bfs(e[rr],e[lr]))rr++;
if(rr<e.size()){
if(ans.first==-1)ans.first=e[rr],ans.second=e[lr];
else if(cmp2(ans.first,ans.second,e[rr],e[lr]))ans.first=e[rr],ans.second=e[lr];
}
lr++;
}
if(ans.first==-1)cout<<"IMPOSSIBLE"<<endl;
else{
int r=__gcd(ans.first,ans.second);
if(ans.second==r)cout<<ans.first/r<<endl;
else cout<<ans.first/r<<"/"<<ans.second/r<<endl;
}
return 0;
}