# BZOJ 1050 [HAOI2006]旅行comf

## 1050: [HAOI2006]旅行comf

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 1889  Solved: 976
[Submit][Status][Discuss]

【样例输入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

## HINT

【数据范围】

1<  N < = 500

1 < = x, y < = N，0 < v < 30000，x ≠ y

0 < M < =5000

## Source

1 #include<iostream>
2 #include<cstdio>
3 #include<cmath>
4 #include<algorithm>
5 #include<stack>
6 #include<queue>
7 #include<cstring>
8 #define PAU putchar(' ')
9 #define ENT putchar('\n')
10 using namespace std;
11 const int maxn=500+10,maxm=10000+10,inf=1e9;
12 struct edge{int x,y,w;}e[maxm];bool operator<(const edge&a,const edge&b){return a.w<b.w;}
13 int n,m,fa[maxn];
14 int findset(int x){return x==fa[x]?x:fa[x]=findset(fa[x]);}
16     int x=0;bool sig=1;char ch=getchar();
17     for(;!isdigit(ch);ch=getchar())if(ch=='-')sig=0;
18     for(;isdigit(ch);ch=getchar())x=10*x+ch-'0';
19     return sig?x:-x;
20 }
21 inline void write(long long x){
22     if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x;
23     int len=0;static long long buf[20];while(x)buf[len++]=x%10,x/=10;
24     for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return;
25 }
26 long long gcd(long long a,long long b){return !b?a:gcd(b,a%b);}
27 int S,T;
28 int main(){
32     for(int i=1;i<m;i++){
33         for(int j=1;j<=n;j++)fa[j]=j;
34         for(int j=i;j<=m;j++){
35             x=findset(e[j].x);y=findset(e[j].y);fa[x]=y;
36             if(findset(S)==findset(T)){
37                 if(e[j].w*mother<e[i].w*son)mother=e[i].w,son=e[j].w;break;
38             }if(e[j].w*mother>=e[i].w*son)break;
39         }
40     }if(son==inf){puts("IMPOSSIBLE");return 0;}
41     long long t=gcd(son,mother);son/=t;mother/=t;
42     if(mother==1)write(son);
43     else write(son),putchar('/'),write(mother);
44     return 0;
45 }

posted @ 2015-08-14 21:57  AI_Believer  阅读(162)  评论(0编辑  收藏