bzoj1208| Hnoi Travel comf| and find a collection

Source: Internet
Author: User

Description
give you an n<=500 graph, N (a) vertex, M (m<=5000) edge, each edge has a weight Vi (vi<30000). Give you two vertices s and t to find a path that minimizes the ratio of the maximum and minimum edges on the path. If there is no path between S and T, the output "impossible", otherwise the output of this ratio, if necessary, expressed as an approximate fraction. Note: There may be more than one path between two vertices.
Input
The first line contains two positive integers, N and M. The M line down contains three positive integers per line: x, Y and V. Indicates that there is a two-way road between the sights X and the attraction Y, and the vehicle must travel at speed v on the public road. The last line contains two positive integer s,t, indicating the shortest path from attraction s to the maximum minimum speed of attraction T. S and T cannot be the same.
Output
if the attraction s to attractions t have no path, output "impossible". Otherwise the output is a number that represents the minimum speed ratio. If necessary, output an approximate fraction.
Sample Input
"Sample input 1"
4 2
1 2 1
3 4 2
1 4 "Sample input 2"
3 3
1 2
1 2 5
2 3 8
1 3
"Sample 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 range"
1< N < =
1 < = x, y < = n,0 < v < 30000,x≠y
0 < M < =5000
Analysis: The same as the comfortable route.

#include <iostream>#include<cstdio>#include<cstring>using namespacestd;structnode{intX,y,v;} e[5001];intfa[501];BOOLCMP (node A,node b) {returna.v<B.V;}intgcdintAintb) {returnB?GCD (b,a%b): A; }intFindintx) {returnx==fa[x]?x:fa[x]=find (Fa[x]);}intMain () {intn,m; CIN>> N >>m;  for(intI=1; i<=m; i++) Cin >> e[i].x >> e[i].y >>e[i].v; Sort (e+1, e+m+1, CMP); intst=1, mx,mn; ints,t,ansmx=1, ansmn=0; CIN>> s >>T;  while(st<m) {intK;  for(intI=1; i<=n; i++) fa[i]=i;  for(inti=st; i<=m; i++) {fa[e[i].x]=find (E[I].Y); if(Find (s) ==find (t)) {mx=e[i].v; k=i; Break; } }          if(mx==-1)if(!ANSMN) {cout<<"Impossible";return 0; } Else  Break;  for(intI=1; i<=n; i++) fa[i]=i;  for(; k>=1; k--) {fa[e[k].x]=find (E[K].Y); if(Find (s) ==find (t)) {mn=e[k].v; st=k+1; Break; } }          if(mn==-1)if(!ANSMN) {cout<<"Impossible";return 0; } Else  Break; intR=GCD (MX,MN); Mx/=r; Mn/=R; if(ANSMX*MN&GT;ANSMN*MX) {ansmn=mn;ansmx=MX;} }    if(ansmn==1) cout <<Ansmx; Elsecout << ansmx <<"/"<< ANSMN <<Endl; return 0;}

bzoj1208| Hnoi Travel comf| and find a collection

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.