POJ 2485

最基本的最小生成树问题,最后求树种的最大边长

我用的Kruskal。

还有一个教训,POJ上用scanf要挂<stdio.h>, sort 要挂<algorithm>

 

#include <iostream> #include <vector> #include <stdio.h> #include <algorithm> #define PB push_back #define F(i,a,b) for (int i=a;i<=b;i++) using namespace std; int n, ne, f[502]; class edge { public: int s, t, l; edge() {} edge(int a, int b, int c) { s=a, t=b, l=c; } bool operator < (const edge& b) const { return l<b.l; } }E[250002]; vector<edge> res; int find(int x) { if (f[x]==x) return x; int t; t=find(f[x]); f[x]=t; return t; } void merge(int a, int b) { int aa=find(a), bb=find(b); f[aa]=bb; } void Kruskal() { int now=0; edge e; F(i,1,n) f[i]=i; sort(&E[1], &E[ne+1]); while ( res.size()!=n-1 ) { e=E[++now]; if ( find(e.s) != find(e.t) ) { merge(e.s, e.t); res.PB( e ); } } } int main() { //freopen ("in.txt", "r", stdin); // freopen ("out.txt", "w", stdout); int T, l; scanf("%d", &T); F(t,1,T) { res.clear(); ne=0; scanf("%d", &n); F(i,1,n) { F(j,1,i) scanf("%d", &l); F(j,i+1,n) { scanf("%d", &l); if (l!=0) E[++ne]=edge(i, j, l); } } Kruskal(); vector<edge>::iterator it=res.begin(); int max_=0; while (it!=res.end() ) { if ( (*it).l>max_) max_=(*it).l; it++; } cout << max_ << endl; } return 0; }  

你可能感兴趣的:(POJ 2485)