ZOJ 3204 Connect them 【最小生成树+输出选取的边】

本文探讨如何通过最小生成树算法解决连接所有电脑的最小花费问题,并详细阐述了如何确保输出的连接方案在字典序上是最小的。文章提供了完整的AC代码实现,包括对边权排序的特殊考虑,以及最终输出连接方案的排序方法。

摘要生成于 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 iand 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


Author: CAO, Peng
Source: The 6th Zhejiang Provincial Collegiate Programming Contest


题目大意:连接两台电脑需要花费对应需要的花费,问连接所有电脑的情况下,最小的花费,并且输出选取连接的电脑编号。


分析:连接所有电脑的最小花费其实就是让你求最小生成树的值,输出选取的编号我们在克鲁斯卡尔算法里边就可以存进去。至于最小字典序,我们只需要排序即可。


坑点:在求最小生成树的值的时候呢,我们很容易思维定式直接就按照边权值排序就完事了,会忘记如果两个权值相同的时候,选取的边没有按照字典序最小的方案来(其实也就是说最小生成树不唯一的时候,会有这样一个坑)。


口述这个题可能有点不太清晰,我来把这关键的两个点单独拆出来写代码和注释:

int cmp(zuobiao a,zuobiao b)//在克鲁斯卡尔算法之前对边权值的排序
{
    if(a.w!=b.w)
    return a.w<b.w;
    else return a.x<b.x;//防止最小生成树不唯一的情况时候对字典序最小进行排序。
}
int cmp2(zuobiao a,zuobiao b)//在输出编号之前的排序,保证输出字典序最小
{
    if(a.x!=b.x)
    return a.x<b.x;
    else return a.y<b.y;
}

剩下的部分就是把邻接矩阵存入结构体中,然后调用cmp排序,然后进行克鲁斯卡尔算法,一边贪心的选取边,一边记录编号,然后判定能否生成一颗树,如果不能输出-1.如果能,调用cmp2函数对记录的编号排序,保证输出的字典序,然后输出。


完整的AC代码:

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
struct zuobiao
{
    int x,y,w;
}a[105*105],ans[105*105];
int map[105][105];
int f[105*105];
int cmp(zuobiao a,zuobiao b)
{
    if(a.w!=b.w)
    return a.w<b.w;
    else return a.x<b.x;
}
int cmp2(zuobiao a,zuobiao b)
{
    if(a.x!=b.x)
    return a.x<b.x;
    else return a.y<b.y;
}
int find(int a)
{
    int r=a;
    while(f[r]!=r)
    r=f[r];
    return r;
}
void merge(int x,int y)
{
    int xx=find(x);
    int yy=find(y);
    if(xx!=yy)
    {
        f[yy]=xx;
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
        {
            int cont=0;
            int n;
            scanf("%d",&n);
            for(int i=1;i<=n;i++)
            {
                f[i]=i;
            }
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    scanf("%d",&map[i][j]);
                    if(map[i][j]>0)
                    {
                        a[cont].x=i;a[cont].y=j;a[cont].w=map[i][j];
                        cont++;
                    }
                }
            }
            int k=0;
            int f=0;
            int cont2=0;
            sort(a,a+cont,cmp);
            for(int i=0;i<cont;i++)
            {
                if(find(a[i].x)!=find(a[i].y))
                {
                    merge(a[i].x,a[i].y);
                    k++;
                    ans[cont2].x=a[i].x;
                    ans[cont2].y=a[i].y;
                    cont2++;
                }
            }
            sort(ans,ans+cont2,cmp2);
            if(k!=n-1)printf("-1\n");
            else
            {
                printf("%d %d",ans[0].x,ans[0].y);
                for(int i=1;i<cont2;i++)
                {
                    printf(" %d %d",ans[i].x,ans[i].y);
                }
                printf("\n");
            }
        }
}










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值