传送门
不想复制题目了。。
给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000)。给你两个顶点S和T,求
一条路径,使得路径上最大边和最小边的比值最小。
由于我们看到 n 和 m 的范围如此之小,就可以想到固定一个最大值或最小值,然后枚举求解即可
具体做法:我们按边权从小排到大,每次都固定一个最小值。然后依次加边,直到 s 和 t 联通,此时的最大值一定是当前情况下的最小的可能。我们在这个过程用并查集维护一下 s 和 t 是否连通即可
和题解讲的是一模一样啦,就不加注释咯
#include
#include
#include
#include
#include
#define M 5009
#define eps 1e-8
using namespace std;
int n,m,s,t,maxn,minn,shuchu[3];
double ans=30009;
struct node {int u,v,w;} p[M*2];
int tot=0,nxt[M*2],head[M*2],fa[509];
void add(int x,int y,int w){
nxt[++tot]=head[x];head[x]=tot;
p[tot].u=x;p[tot].v=y;p[tot].w=w;
}
bool cmp(const node &a,const node &b){ return a.weps){
ans=h;
shuchu[0]=-1;
}
}
else {
if(ans-h>eps) {
ans=h;
int hh=gcd(maxn,minn);
shuchu[0]=maxn/hh,shuchu[1]=minn/hh;
}
}
}
}
if(flag){
if(shuchu[0]==-1) printf("%.0lf",ans);
else printf("%d/%d",shuchu[0],shuchu[1]);
}
else printf("IMPOSSIBLE") ;
return 0;
}