该死的字典序。。。 排序抄党的。。。我了个去。。。自己想的还是有问题,不了解那个qsort里面的比较函数是怎么实现的。。。 中秋,大家要快乐~~哈哈哈哈哈哈哈哈哈哈,不爽,吃饭去。 #include <stdio.h> #include <stdlib.h> #include <iostream> #define N 110 using namespace std; struct c { int x,y,len; }pc[N*N],put[N]; typedef struct c NODE; int n,p,pre[N]; void input() { int len; cin >> n; p = 0; for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) { cin >> len; if( j <= i ) continue; if( len == 0 ) continue; pc[p].x = i; pc[p].y = j; pc[p].len = len ; p++; } } int cmp1(const void *a,const void *b) { NODE aa = *(NODE*)a; NODE bb = *(NODE*)b; if( aa.len != bb.len ) return aa.len>bb.len; else if( aa.x != bb.x ) return aa.x-bb.x; else return aa.y-bb.y; } int cmp2(const void *a,const void *b) { NODE aa = *(NODE*)a; NODE bb = *(NODE*)b; if( aa.x != bb.x ) return aa.x-bb.x; else return aa.y-bb.y; } int find(int x) { while( x!= pre[x] ) x = pre[x]; return x; } void kruskal() { int a,b,count = 0; qsort( pc, p, sizeof(NODE), cmp1); for(int i=1; i<=n; i++) pre[i] = i; for(int i=0; i<p; i++) { a = find( pc[i].x ); b = find( pc[i].y ); if( a!=b ) { pre[b] = a; put[count++] = pc[i]; } } if( count == n-1 ) { qsort( put, count, sizeof(NODE), cmp2); cout << put[0].x << " "<< put[0].y; for(int i=1; i<count; i++) cout << " " << put[i].x << " " << put[i].y ; cout << endl; } else cout << "-1" << endl; } int main(void) { int ncases; cin >> ncases; while( ncases-- ) { input(); kruskal(); } return 0; }