## Output

 1 #include <iostream>
2 #include <cstdio>
3 #include <algorithm>
4 #include <cstring>
5 #define rep(i,l,r) for(int i=l; i<=r; i++)
6 #define clr(x,y) memset(x,y,sizeof(x))
7 #define travel(x) for(int i=last[x]; i; i=edge[i].pre)
8 using namespace std;
9 const int INF = 0x3f3f3f3f;
10 const int maxn = 510;
11 struct Edge{
12     int from,to,cost;
13 }edge[5010];
14 int n,m,x,y,now,s,t,ans1,ans2,fa[maxn];
15 inline bool cmp(const Edge &x, const Edge &y){
16     return x.cost < y.cost;
17 }
19     int ans = 0, f = 1;
20     char c = getchar();
21     while (!isdigit(c)){
22         if (c == '-') f = -1;
23         c = getchar();
24     }
25     while (isdigit(c)){
26         ans = ans * 10 + c - '0';
27         c = getchar();
28     }
29     return ans * f;
30 }
31 int gcd(int x,int y){
32     return y ? gcd(y,x%y) : x;
33 }
34 int getfa(int x){
35     return fa[x] == x ? x : fa[x] = getfa(fa[x]);
36 }
37 int main(){
39     rep(i,1,m){
41     }
43     sort(edge+1,edge+m+1,cmp);
44     double rate = INF * 1.0;
45     rep(start,1,m){
46         rep(i,1,n) fa[i] = i; now = 0;
47         rep(i,start,m){
48             x = getfa(edge[i].from); y = getfa(edge[i].to);
49             fa[x] = y;
50             if (getfa(s) == getfa(t)){
51                 now = i; break;
52             }
53         }
54         if (!now) break;
55         if (edge[now].cost * 1.0 / edge[start].cost < rate){
56             rate = edge[now].cost * 1.0 / edge[start].cost;
57             ans1 = edge[now].cost; ans2 = edge[start].cost;
58         }
59     }
60     if (rate == INF) printf("IMPOSSIBLE\n");
61     else if (!(ans1%ans2)) printf("%d\n",ans1/ans2);
62     else printf("%d/%d\n",ans1/gcd(ans1,ans2),ans2/gcd(ans1,ans2));
63     return 0;
64 }
View Code

posted on 2015-11-27 14:40  ACMICPC  阅读(204)  评论(0编辑  收藏