# BZOJ1050: [HAOI2006]旅行comf(并查集 最小生成树)

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 4021  Solved: 2257
[Submit][Status][Discuss]

## Input

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

1<N<=500,1<=x,y<=N，0<v<30000，0<M<=5000

【样例输入1】
4 2
1 2 1
3 4 2
1 4
【样例输入2】
3 3
1 2 10
1 2 5
2 3 8
1 3
【样例输入3】
3 2
1 2 2
2 3 4
1 3

【样例输出1】
IMPOSSIBLE
【样例输出2】
5/4
【样例输出3】
2

## Source

1 2 2

2 3 4

1 3 5

#include<cstdio>
#include<algorithm>
#define LL long long
const int MAXN = 1e5 + 10, INF = 1e9 + 10;
using namespace std;
char c = getchar();int x = 0,f = 1;
while(c < '0' || c > '9'){if(c == '-')f = -1;c = getchar();}
while(c >= '0' && c <= '9'){x = x * 10 + c - '0',c = getchar();}
return x * f;
}
struct Edge {
int u, v, w;
bool operator < (const Edge &rhs) const {
return w < rhs.w;
}
}E[MAXN];
int N, M, fa[MAXN], S, T;
int find(int x) {
if(fa[x] == x) return fa[x];
else return fa[x] = find(fa[x]);
}
int Build(int now) {
int mx = -INF, tot = 0;
for(int i = 1; i <= N; i++) fa[i] = i;
for(int i = now; i <= M; i++) {
int fx = find(E[i].u), fy = find(E[i].v);
if(fx == fy) continue;
tot++;
fa[fx] = fy;
mx = max(mx, E[i].w);
if(find(S) == find(T)) return mx;
}
return INF;
}
main() {
for(int i = 1; i <= M; i++) {
E[i] = (Edge){x, y, z};
}
sort(E + 1, E + M + 1);
double now = INF;
int mi = INF, mx = INF;
for(int i = 1; i <= M; i++) {
int nowx = Build(i);
if((double)nowx / E[i].w < now) {
mi = E[i].w, mx = nowx;
now = (double)mx / mi;
}
}
if(mx == INF) printf("IMPOSSIBLE");
else {
int gcd = __gcd(mi, mx);
if(mi / gcd != 1) printf("%d/%d", mx / gcd, mi / gcd);
else printf("%d", mx / gcd);
}
}

posted @ 2018-07-12 19:40  自为风月马前卒  阅读(67)  评论(0编辑  收藏

……