Bzoj1050: [haoi2006] Travel comf

Source: Internet
Author: User
1050: [haoi2006] Travel comftime limit: 10 sec memory limit: 162 MB
Submit: 1307 solved: 632
[Submit] [Status] Description

An undirected graph with n (n <= 500) vertices and M (M <= 5000) edges with a weight VI (VI <30000 ). We will give you two vertices, S and T, and find a path to minimize the ratio of the maximum side and the Minimum side of the path. If there is no path between S and T, "impossible" is output; otherwise, this ratio is output. If needed, it is expressed as an equal score. Note: There may be multiple paths between two vertices.

Input

The first line contains two positive integers, N and M. Each row of the following m rows contains three positive integers: X, Y, and V. There is a two-way road between Scenic Spot X and scenic spot Y. the vehicle must drive at the speed V on the road. The last line contains two positive integers, T, indicating that you want to know the path from the Scenic Spot s to the Scenic Spot T. S and T cannot be the same.

Output

If there is no path between Scenic Spot S and scenic spot T, output "impossible ". Otherwise, a number is output, indicating the minimum speed ratio. If needed, an equal score is output.

Sample input [Example input 1]
4 2
1 2 1
3 4 2
1 4

[Example input 2]
3 3
1 2 10
1 2 5
2 3 8
1 3


[Example input 3]
3 2
1 2 2
2 3 4
1 3

Sample output [sample output 1]
Impossible

[Sample output 2]
5/4
[Sample output 3]
2

[Data Scope]
1 <n <= 500
1 <= X, Y <= N, 0 <v <30000, x = y
0 <m <= 5000

Hint

Question:

If the minimum weight is set, the smaller the maximum weight, the better. Then we enumerate the minimum weight and use the query set to find the minimum maximum weight in the current situation. Update the answer.

The correctness should be clear.

Code:

 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  begin12  i:=h;j:=l;m:=v[(i+j) shr 1];13  repeat14   while v[i]>m do inc(i);15   while v[j]<m do dec(j);16   if i<=j then17    begin18    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  begin29  readln(n,m);30  for i:=1 to m do  readln(x[i],y[i],v[i]);31  readln(s,t);32  qsort(1,m);33  end;34 procedure tryy(step:longint);35  var i:longint;36  begin37  for i:=1 to n do fa[i]:=i;38  for i:=step to m do39   begin40   fa[find(x[i])]:=find(y[i]);41   if find(s)=find(t) then42    begin43    inc(flag);44    if flag=1 then45     begin46     xi:=v[step];47     yi:=v[i];48     end49    else50     if (v[step]/v[i]<xi/yi) then51     begin52     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  begin61  if b=0 then exit(a) else exit(gcd(b,a mod b));62  end;63 procedure print;64  begin65  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  begin71  for i:=1 to m do tryy(i);72  if flag=0 then writeln(‘IMPOSSIBLE‘)73  else print;74  end;75 begin76  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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.