zoj3204(最小生成树)

本文介绍了一个经典的图论问题——如何构建成本最低的网络连接方案,即寻找最小生成树,并给出了一种实现方法,通过Prim算法解决该问题的同时考虑了字典序最小的要求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Connect them

Time Limit: 1 Second      Memory Limit: 32768 KB

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 = cjicii = 0, 1 <= ij <= 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: a1a2, ...ap.
Another solution B different from A is a line of q integers: b1b2, ...bq.
A is lexicographically smaller than B if and only if:
(1) there exists a positive integer r (r <= pr <= 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");
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值