# BZOJ1050: [HAOI2006]旅行comf

## 1050: [HAOI2006]旅行comf

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 1307  Solved: 632
[Submit][Status]

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

## Sample Output

【样例输出1】
IMPOSSIBLE

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

【数据范围】
1< N < = 500
1 < = x, y < = N，0 < v < 30000，x ≠ y
0 < M < =5000

## HINT

 1 var i,n,m,s,t,xi,yi,flag,xx:longint;
2     x,y,v:array[0..5010] of longint;
3     fa:array[0..510] of longint;
4 function find(a:longint):longint;
5  begin
6  if fa[a]<>a then fa[a]:=find(fa[a]);
7  exit(fa[a]);
8  end;
9 procedure qsort(h,l:longint);
10  var i,j,m,temp:longint;
11  begin
12  i:=h;j:=l;m:=v[(i+j) shr 1];
13  repeat
14   while v[i]>m do inc(i);
15   while v[j]<m do dec(j);
16   if i<=j then
17    begin
18    temp:=x[i];x[i]:=x[j];x[j]:=temp;
19    temp:=y[i];y[i]:=y[j];y[j]:=temp;
20    temp:=v[i];v[i]:=v[j];v[j]:=temp;
21    inc(i);dec(j);
22    end;
23  until i>j;
24  if i<l then qsort(i,l);
25  if j>h then qsort(h,j);
26  end;
27 procedure init;
28  begin
30  for i:=1 to m do  readln(x[i],y[i],v[i]);
32  qsort(1,m);
33  end;
34 procedure tryy(step:longint);
35  var i:longint;
36  begin
37  for i:=1 to n do fa[i]:=i;
38  for i:=step to m do
39   begin
40   fa[find(x[i])]:=find(y[i]);
41   if find(s)=find(t) then
42    begin
43    inc(flag);
44    if flag=1 then
45     begin
46     xi:=v[step];
47     yi:=v[i];
48     end
49    else
50     if (v[step]/v[i]<xi/yi) then
51     begin
52     xi:=v[step];
53     yi:=v[i];
54     end;
55    exit;
56    end;
57   end;
58  end;
59 function gcd(a,b:longint):longint;
60  begin
61  if b=0 then exit(a) else exit(gcd(b,a mod b));
62  end;
63 procedure print;
64  begin
65  xx:=gcd(xi,yi);
66  if xx=yi then writeln(xi div yi)
67  else writeln(xi div xx,'/',yi div xx);
68  end;
69 procedure main;
70  begin
71  for i:=1 to m do tryy(i);
72  if flag=0 then writeln('IMPOSSIBLE')
73  else print;
74  end;
75 begin
76  assign(input,'input.txt');assign(output,'output.txt');
77  reset(input);rewrite(output);
78  init;
79  main;
80  close(input);close(output);
81 end.  
View Code

posted @ 2014-08-13 12:54  ZYF-ZYF  Views(83)  Comments(0Edit  收藏