给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000)。给你两个顶点S和T,求一条路径,使得路径上最大边和最小边的比值最小。如果S和T之间没有路径,输出”IMPOSSIBLE”,否则输出这个比值,如果需要,表示成一个既约分数。 备注: 两个顶点之间可能有多条路径。
标签:des style c class blog code
给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000)。给你两个顶点S和T,求一条路径,使得路径上最大边和最小边的比值最小。如果S和T之间没有路径,输出”IMPOSSIBLE”,否则输出这个比值,如果需要,表示成一个既约分数。 备注: 两个顶点之间可能有多条路径。
第一行包含两个正整数,N和M。 下来的M行每行包含三个正整数:x,y和v。表示景点x到景点y之间有一条双向公路,车辆必须以速度v在该公路上行驶。 最后一行包含两个正整数s,t,表示想知道从景点s到景点t最大最小速度比最小的路径。s和t不可能相同。
如果景点s到景点t没有路径,输出“IMPOSSIBLE”。否则输出一个数,表示最小的速度比。如果需要,输出一个既约分数。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 |
#include<cstdio>#include<cstdlib>#include<algorithm>const
int N = 510;const
int M = 5010;const
int Maxint = 2147483647;using
namespace std;#define For(i,n) for(int i=1;i<=n;i++)#define Rep(i,l,r) for(int i=l;i<=r;i++)struct
EDGE{ int
s,t,next,w;}E[M];int
head[N],es=1,fa[N];int
Max,Min = Maxint,n,m,x,y,w,s,t;double
MIN = Maxint;void
makelist(int
s,int t,int w){ E[es].s = s;E[es].t = t;E[es].w = w; E[es].next = head[s]; head[s]=es++;}void
init(){ scanf("%d%d",&n,&m); For(i,m){ scanf("%d%d%d",&x,&y,&w); makelist(x,y,w); } scanf("%d%d",&s,&t);}bool
cmp(EDGE A,EDGE B){ return
A.w<B.w;}int
find(int
root){ if(root!=fa[root]) fa[root] = find(fa[root]); return
fa[root];}int
gcd(int
a,int b){ if(!b) return
a; else
return gcd(b,a%b);}int
main(){ init(); sort(E+1,E+m+1,cmp); For(i,m){ For(j,n) fa[j] = j; Rep(j,i,m){ int
fx = find(E[j].s) , fy = find(E[j].t); if(fx!=fy) fa[fx] = fy; fx = find(s); fy = find(t); if(fx==fy) if((double)E[j].w/(double)E[i].w<MIN){ MIN = (double)E[j].w / (double)E[i].w; Min = E[i].w; Max = E[j].w; break; } } } if(Min==Maxint) puts("IMPOSSIBLE"); else{ int
gcds = gcd(Max,Min); if(Max%Min) printf("%d/%d\n",Max/gcds,Min/gcds); else
printf("%d\n",Max/Min); } return
0;} |
HAOI2006(BZOJ1050) 旅行comf,布布扣,bubuko.com
标签:des style c class blog code
原文地址:http://www.cnblogs.com/kjerome/p/3746757.html