You have n computers numbered from 1 to n and you want to connect them to make a small local area network (LAN). All connections are two-way (that is connecting computers i and j is the same as connecting computers j and i). The cost of connecting computer i and computer j is cij. You cannot connect some pairs of computers due to some particular reasons. You want to connect them so that every computer connects to any other one directly or indirectly and you also want to pay as little as possible.
Given n and each cij , find the cheapest way to connect computers.
Input
There are multiple test cases. The first line of input contains an integer T (T <= 100), indicating the number of test cases. Then T test cases follow.
The first line of each test case contains an integer n (1 < n <= 100). Then n lines follow, each of which contains n integers separated by a space. The j-th integer of the i-th line in these n lines is cij, indicating the cost of connecting computers i and j (cij = 0 means that you cannot connect them). 0 <= cij <= 60000, cij = cji, cii = 0, 1 <= i, j <= n.
Output
For each test case, if you can connect the computers together, output the method in in the following fomat:
i1 j1 i1 j1 ......
where ik ik (k >= 1) are the identification numbers of the two computers to be connected. All the integers must be separated by a space and there must be no extra space at the end of the line. If there are multiple solutions, output the lexicographically smallest one (see hints for the definition of "lexicography small") If you cannot connect them, just output "-1" in the line.
Sample Input
2 3 0 2 3 2 0 5 3 5 0 2 0 0 0 0
Sample Output
1 2 1 3 -1
Hints:
A solution A is a line of p integers: a1, a2, ...ap.
Another solution B different from A is a line of q integers: b1, b2, ...bq.
A is lexicographically smaller than B if and only if:
(1) there exists a positive integer r (r <= p, r <= q) such that ai = bi for all 0 < i < r and ar < br
OR
(2) p < q and ai = bi for all 0 < i <= p
题意:求最小生成树并以字典序输出相连两点。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<map>
#include<queue>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
#define INF 0x3fffffff
#define MAX_V 105
int cost[MAX_V][MAX_V]; //边的权值
int mincost[MAX_V]; //把某点连接到树中最小的边
int lowu[MAX_V]; //这个部分是看别人代码的,代表从树中某点延伸出来的连接这个点的最小边且 某点字典序最小的点
bool used[MAX_V]; //代表有没有利用这个点向外延伸更新过mincost
int V;
int cnt=0;
struct Edge{
int u,v;
};
Edge ans[MAX_V*MAX_V]; //存储结果
int prim()
{
for(int i=0;i<V;i++)
{
lowu[i]=0; //一开始更新为最小点
mincost[i]=cost[0][i]; //这是通过第一个点来更新
used[i]=false;
}
used[0]=true;
mincost[0]=0;
int res=0; //最小生成树的大小
while(true)
{
int v=-1;
for(int u=0;u<V;u++)
if(!used[u]&&(v==-1||mincost[u]<mincost[v]||(mincost[u]==mincost[v]&&lowu[u]<lowu[v])))v=u; //重要的是字典序,选择从小点延伸出来的点
if(mincost[v]==INF)return 0; //如果没有未使用过的点与这个点相连,则这个图不连通
if(v==-1)break; //代表全部点都已经作为节点来更新过其他点
//字典序,使得ans中v<u
if(lowu[v]<v)
{
ans[cnt].u=lowu[v]+1;
ans[cnt++].v=v+1;
}
else
{
ans[cnt].u=v+1;
ans[cnt++].v=lowu[v]+1;
}
res+=mincost[v]; //加上这条延伸出来的边的权值
used[v]=true;
for(int u=0;u<V;u++)
{
//字典序并更新 lowu
if(!used[u]&&cost[v][u]!=0&&(cost[v][u]<mincost[u]||(cost[v][u]==mincost[u]&&v<lowu[u])))
{
mincost[u]=cost[v][u];
lowu[u]=v;
}
}
}
return res;
}
bool cmp(Edge a,Edge b){
if(a.u!=b.u)return a.u<b.u;
return a.v<b.v;
}
void init()
{
memset(cost,0,sizeof(cost));
}
int main()
{
// freopen("0.in","r",stdin);
// freopen("0.out","w",stdout);
int t;
cin>>t;
while(t--)
{
init();
cin>>V;
for(int i=0;i<V;i++)
for(int j=0;j<V;j++)
{
cin>>cost[i][j];
if(cost[i][j]==0)
cost[i][j]=INF;
}
cnt=0;
if(prim())
{
sort(ans,ans+cnt,cmp);
int i;
for(i=0;i<cnt-1;++i)
printf("%d %d ",ans[i].u,ans[i].v);
printf("%d %d\n",ans[i].u,ans[i].v);
}
else printf("-1\n");
}
}