# BZOJ 1050: [HAOI2006]旅行comf

Time Limit: 10 Sec Memory Limit: 162 MB
Submit: 4011 Solved: 2249
[Submit][Status][Discuss]
Description

Input

，车辆必须以速度v在该公路上行驶。最后一行包含两个正整数s，t，表示想知道从景点s到景点t最大最小速度比

1

## 代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>

using namespace std;
const int MAXN = 5005;

inline int rd(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
while(isdigit(ch)) {x=(x<<1)+(x<<3)+ch-48;ch=getchar();}
return x*f;
}

struct Edge{
int to,val,fr;
}edge[MAXN<<1];

int mx=1<<30,mn=1;
int fa[MAXN],S,T;

inline bool cmp(Edge A,Edge B){
return A.val<B.val;
}

inline int find(int x){
if(x==fa[x]) return x;
return fa[x]=find(fa[x]);
}

int main(){
n=rd();m=rd();
for(register int i=1;i<=m;i++)
edge[i].fr=rd(),edge[i].to=rd(),edge[i].val=rd();
S=rd(),T=rd();
sort(edge+1,edge+1+m,cmp);
for(register int i=1;i<=m;i++){
int p=-1;
for(register int j=1;j<=n;j++) fa[j]=j;
for(register int j=i;j<=m;j++){
int u=find(edge[j].fr),v=find(edge[j].to);
if(u!=v) fa[u]=v;
if(find(S)==find(T)) {p=j;break;}
}
if(p==-1) continue;
if((double)mx/(double)mn>(double)edge[p].val/double(edge[i].val))
mx=edge[p].val,mn=edge[i].val;
}
if(mx==(1<<30)) {puts("IMPOSSIBLE");return 0;}
int g=__gcd(mn,mx);
mn/=g,mx/=g;
if(mn==1) printf("%d",mx);
else printf("%d/%d",mx,mn);
return 0;
}
posted @ 2018-07-04 15:00  Monster_Qi  阅读(35)  评论(0编辑  收藏