最小生成树,我用的是并查集+贪心的写法。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int maxn = ;
int c[maxn][maxn];
int father[maxn];
int flag[maxn];
struct abc{
int fei, a, b;
}dt[maxn*maxn];
struct ans{
int aa, bb;
}anss[maxn*maxn];
bool cmp(const abc&a, const abc&b)
{
if (a.fei == b.fei&&a.a == b.a) return a.b < b.b;
if (a.fei == b.fei) return a.a < b.a;
return a.fei < b.fei;
}
bool cmp2(const ans&a, const ans&b)
{
if (a.aa == b.aa) return a.bb < b.bb;
return a.aa < b.aa;
}
int find(int x)
{
if (x != father[x]) father[x] = find(father[x]);
return father[x];
}
int main()
{
int sb;
scanf("%d", &sb);
while (sb--)
{
int n, i, j;
scanf("%d", &n);
memset(c, , sizeof(c));
memset(father, , sizeof(father));
memset(flag, , sizeof(flag));
int q = ;
for (i = ; i <= ; i++) father[i] = i;
for (i = ; i <= n; i++) for (j = ; j <= n; j++) scanf("%d", &c[i][j]);
for (i = ; i <= n; i++)
{
for (j = i + ; j <= n; j++)
{
if (c[i][j] != )
{
dt[q].a = i;
dt[q].b = j;
dt[q].fei = c[i][j];
q++;
}
}
}
sort(dt, dt + q, cmp);
int qq = ;
for (i = ; i < q; i++)
{
int x = find(dt[i].a);
int y = find(dt[i].b);
if (x != y)
{
father[x] = y;
anss[qq].aa = dt[i].a;
anss[qq].bb = dt[i].b;
qq++;
}
}
for (i = ; i <= n; i++){int u = find(i);flag[u]++;}
int dd = ;
for (i = ; i <= n; i++){if (flag[i] > && flag[i] == n){dd = ;break;}}
if (dd==)
{
sort(anss, anss + qq, cmp2);
for (i = ; i < qq; i++)
{
if (i < qq-)printf("%d %d ", anss[i].aa, anss[i].bb);
else printf("%d %d\n", anss[i].aa, anss[i].bb);
}
}
else if (dd == ) printf("-1\n");
}
return ;
}