给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000)。给你两个顶点S和T,求
一条路径,使得路径上最大边和最小边的比值最小。如果S和T之间没有路径,输出”IMPOSSIBLE”,否则输出这个
比值,如果需要,表示成一个既约分数。 备注: 两个顶点之间可能有多条路径。
标签:mit 最小 input output content gcd cst printf max
#include <cstdio> #include <algorithm> using namespace std; inline int readint(){ int n = 0; char ch = getchar(); while(ch < ‘0‘ || ch > ‘9‘) ch = getchar(); while(ch <= ‘9‘ && ch >= ‘0‘){ n = (n << 1) + (n << 3) + ch - ‘0‘; ch = getchar(); } return n; } const int maxn = 500 + 10, maxm = 5000 + 10; struct Edge{ int u, v, w; Edge(){} bool operator < (const Edge &x) const { return w < x.w; } }e[maxm]; int fa[maxn]; int Find(int x){ return x == fa[x] ? x : fa[x] = Find(fa[x]); } int Gcd(int a, int b){ int t; if(a > b){ t = a; a = b; b = t; } while(a){ t = a; a = b % a; b = t; } return b; } int main(){ int n, m; n = readint(); m = readint(); for(int i = 1; i <= m; i++){ e[i].u = readint(); e[i].v = readint(); e[i].w = readint(); } sort(e + 1, e + m + 1); int s, t, ans1 = 30000, ans2 = 0; s = readint(); t = readint(); for(int st = 1; st <= m; st++){ for(int i = 1; i <= n; i++) fa[i] = i; for(int i = st; i <= m; i++){ fa[Find(e[i].u)] = Find(e[i].v); if(Find(s) == Find(t)){ if(ans1 * e[st].w > ans2 * e[i].w){ ans1 = e[i].w; ans2 = e[st].w; } break; } } } if(ans2 == 0) puts("IMPOSSIBLE"); else{ int t = Gcd(ans1, ans2); if(ans2 != t) printf("%d/%d\n", ans1 / t, ans2 / t); else printf("%d\n", ans1 / t); } return 0; }
标签:mit 最小 input output content gcd cst printf max
原文地址:http://www.cnblogs.com/ruoruoruo/p/7450129.html