因为边的数目很少,只有5000,那么我们不妨对边的权值排序,然后5000*5000的进行枚举即可,判断连通性就可以了。
#include <iostream> #include <cstdio> #include <cmath> #include <string> #include <cstring> #include <algorithm> #include <limits> #include <vector> #include <stack> #include <queue> #include <set> #include <map> #include <bitset> #include <unordered_map> #include <unordered_set> #define lowbit(x) ( x&(-x) ) #define pi 3.141592653589793 #define e 2.718281828459045 #define INF 0x3f3f3f3f #define HalF (l + r)>>1 #define lsn rt<<1 #define rsn rt<<1|1 #define Lson lsn, l, mid #define Rson rsn, mid+1, r #define QL Lson, ql, qr #define QR Rson, ql, qr #define myself rt, l, r #define pii pair<int, int> #define MP(a, b) make_pair(a, b) using namespace std; typedef unsigned long long ull; typedef unsigned int uit; typedef long long ll; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } bool operator < (pii a, pii b) { return 1LL * a.first * b.second < 1LL * b.first * a.second; } pii min(pii a, pii b) { return a < b ? a : b; } const int maxN = 505; int N, M, root[maxN], S, T; int fid(int x) { return x == root[x] ? x : root[x] = fid(root[x]); } void init() { for(int i = 1; i <= N; i ++) root[i] = i; } vector<pii> vt[30005]; int lsan[5005]; int main() { scanf("%d%d", &N, &M); for(int i = 1, u, v, w; i <= M; i ++) { scanf("%d%d%d", &u, &v, &w); lsan[i] = w; vt[w].push_back(MP(u, v)); } scanf("%d%d", &S, &T); sort(lsan + 1, lsan + M + 1); int _UP = (int)(unique(lsan + 1, lsan + M + 1) - lsan - 1); pii ans = MP(INF, 1); for(int i = 1, j, u, v; i <= _UP; i ++) { init(); for(j = i; j <= _UP; j ++) { for(pii edge : vt[lsan[j]]) { u = edge.first; v = edge.second; u = fid(u); v = fid(v); if(u ^ v) root[u] = v; } if(fid(S) == fid(T)) break; } if(fid(S) != fid(T)) break; ans = min(ans, MP(lsan[j] / gcd(lsan[j], lsan[i]), lsan[i] / gcd(lsan[j], lsan[i]))); } if(ans.first == INF) printf("IMPOSSIBLE\n"); else { if(ans.second == 1) printf("%d\n", ans.first); else printf("%d/%d\n", ans.first, ans.second); } return 0; }