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 10
1 2 5
2 3 8
1 3
"Sample Input 3"
3 2
1 2 2
2 3 4
1 3Sample Output"Sample Output 1"
Impossible
"Sample Output 2"
5/4
"Sample Output 3"
2HINT
"Data Range"
1< N < = 500
1 < = x, y < = n,0 < v < 30000,x≠y
0 < M < =5000
Source
Analysis
Dine problem.
1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstring>5#include <vector>6#include <utility>7#include <iomanip>8#include <string>9#include <cmath>Ten#include <queue> One#include <map> A - Const intMAXN = -+Ten; - Const intMAXM = the+Ten; the using namespacestd; - structedge{ - intu, V, W; - BOOL operator< (EDGE B)Const{ + returnW <B.W; - } + }EDGE[MAXM]; A intN, M, PARENT[MAXN]; at ints, t; - - intFindintx) {returnPARENT[X] <0? X:PARENT[X] =find (Parent[x]);} - voidMergeintXinty) { - if(Parent[x] >Parent[y]) { -Parent[y] + =Parent[x]; inPARENT[X] =y; -}Else{ toPARENT[X] + =Parent[y]; +Parent[y] =x; - } the } * voidinit () { $scanf"%d%d", &n, &m);Panax Notoginsengmemset (Parent,-1,sizeof(parent)); - for(inti =1; I <= m; i++){ thescanf"%d%d%d", &edge[i].u, &EDGE[I].V, &EDGE[I].W); + } Ascanf"%d%d", &s, &t); theSort (Edge +1, Edge +1+m); + } - intgcdintAintb) {returnb = =0? A:GCD (b, a%b);} $ voidWork () { $ intA =30000, B =1; - //enumerates the edges with the smallest edge length - for(inti =1; I <= m; i++){ thememset (Parent,-1,sizeof(parent)); - intJ;Wuyi for(j = i; J <= M; j + +){ the intU = edge[j].u, v =edge[j].v; -U = Find (u); v =Find (v); Wu if(U! =v) Merge (U, v); - if(Find (s) = = Find (t)) Break; About } $ if(J <=m) { - if(EDGE[I].W * A > EDGE[J].W *B) { -A =EDGE[J].W; -B =EDGE[I].W; A } + } the } - if(A = =30000&& B = =1) {printf ("impossible\n");return;} $ if(A% B = =0) printf ("%d\n"AB); the Elseprintf"%d/%d", A/GCD (A, b), b/gcd (A, B)); the } the the intMain () { - intT; in #ifdef LOCAL theFreopen ("Data.txt","R", stdin); theFreopen ("OUT.txt","W", stdout); About #endif the init (); the Work (); the return 0; +}
View Code
"BZOJ1050" "Enumeration + and check set" Travel COMF