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 72 73 74 75 76 77 78 79 80 81 82 83
| #include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring>
using namespace std;
const int MAX = 1100; const int oo = 0x3f3f3f3f;
struct Edge { int u, v, len; bool operator<(const Edge& e) const { return len > e.len; } void read() { scanf("%d%d%d", &u, &v, &len); } } e[MAX * MAX]; int p[MAX], rank[MAX], n, m;
void init() { for (int i = 1; i <= n; i++) { p[i] = i; rank[i] = 0; } }
int find(int x) { return p[x] = x == p[x] ? p[x] : find(p[x]); }
void Link(int x, int y) { if (rank[x] > rank[y]) { p[y] = x; } else { p[x] = y; if (rank[x] == rank[y]) { rank[y]++; } } }
bool merge(int x, int y) { int px = find(x); int py = find(y); if (px == py) { return false; } Link(px, py); return true; }
int Kruskal() { int ret = 0; int cnt = 1; init(); sort(e, e + m); for (int i = 0; i < m && cnt < n; i++) { if (merge(e[i].u, e[i].v)) { cnt++; ret = e[i].len; } if (find(1) == find(n)) { break; } } return ret; }
int main() { int t, cnt = 1; bool done = false; scanf("%d", &t); while (t--) { if (done) { puts(""); } else { done = true; } scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { e[i].read(); } printf("Scenario #%d:\n", cnt++); printf("%d\n", Kruskal()); } return 0; }
|