//poj 2485 kruskal算法+并查集 TLE 922MS擦线(1000MS)过
#include <iostream>
#include <algorithm>
#define inf 65537
using namespace std;
int max1;
int father[501];
int rank[501];
struct edge{
int st;
int ed;
int w;
}e[125255];
int top = 0;
void addEdge(int x, int y, int z)
{
e[top].st = x;
e[top].ed = y;
e[top].w = z;
top++;
}
void makeset(int n)
{
for(int i = 0; i < n; i++)
{
father[i] = i;
rank[i] = 0;
}
}
int find(int x)
{
if(x != father[x])
father[x] = find(father[x]);
return father[x];
}
bool Union(int x, int y)
{
int px = find(x);
int py = find(y);
if(px != py)
{
if(rank[px] > rank[py])
father[py] = px;
else
{
if(rank[px] == rank[py])
rank[py]++;
father[px] = py;
}
return true;
}
return false;
}
int cmp(const void* a, const void* b)
{
return ((edge*)a)->w - ((edge*)b)->w;
}
int kruskal(int n)
{
//排序
qsort(e, top, sizeof(e[0]), cmp);
for(int i = 0; i < top; i++)
{
if(Union(e[i].st, e[i].ed))
{
if(e[i].w > max1)
max1 = e[i].w;
}
}
return max1;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
top = 0;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
int w;
cin>>w;
addEdge(i, j, w);
}
}
makeset(n);
max1 = 0;
cout<<kruskal(n)<<endl;
}
return 0;
}
poj2485 Kruskal算法+并查集 922MS险过
猜你喜欢
转载自blog.csdn.net/chchlh/article/details/41776423
今日推荐
周排行