ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LightOJ - 1040:Donation(Prim)

2019-08-11 10:05:20  阅读:311  来源: 互联网

标签:电线 1040 Prim cable house sum Donation rooms your


https://vjudge.net/problem/LightOJ-1040

A local charity is trying to gather donations of Ethernet cable. You realize that you probably have a lot of extra cable in your house, and make the decision that you will donate as much cable as you can spare.

You will be given the lengths (in meters) of cables between each pair of rooms in your house. You wish to keep only enough cable so that every pair of rooms in your house is connected by some chain of cables, of any length. The lengths are given in n lines, each having n integers, where n is the number of rooms in your house. The jth integer of ith line gives the length of the cable between rooms i and j in your house.

If both the jth integer of ith line and the ith integer of jth line are greater than 0, this means that you have two cables connecting rooms i and j, and you can certainly donate at least one of them. If the ith integer of ith line is greater than 0, this indicates unused cable in room i, which you can donate without affecting your home network in any way. 0 means no cable.

You are not to rearrange any cables in your house; you are only to remove unnecessary ones. Return the maximum total length of cables (in meters) that you can donate. If any pair of rooms is not initially connected by some path, return -1.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case begins with a blank line and an integer n (1 ≤ n ≤ 50) denoting the number of rooms in your house. Then there will be n lines, each having n space separated integers, denoting the lengths as described. Each length will be between 0 and 100.

Output

For each case of input you have to print the case number and desired result.

Sample Input

3

 

2

27 26

1 52

 

4

0 10 10 0

0 0 1 1

0 0 0 2

0 0 0 0

 

4

0 1 0 0

1 0 0 0

0 0 0 1

0 0 1 0

Sample Output

Case 1: 105

Case 2: 12

Case 3: -1

题意分析:

有n个房间用电线连接,你想捐出一些电线,给出一个n*n的矩阵,第 i 行第 j 列代表第 i 个房间和第 j 个房间之间有a[i][j]米长的电线,第 i 行第 i 列如果不为0说明这个房间中有闲置的电线;如果第 i 行第 j 列和第 j 行第 i 列都不为0,说明两个房间不止一条电线连接,则可以选择其中一条,求最多可以捐出来的电线是多少。

解题思路:

全部电线长sum,首先每个房子闲置的电线可以拿出来,假设为s, 那么剩余sum-s, 求出最小生成树为num,那么num是必须要的, 答案为s+sum-s-num,即sum-num。

#include <stdio.h>
#include <algorithm>
#include <string.h>
#define N 120
using namespace std;
int e[N][N], dis[N];
bool book[N];
int main()
{
	int T, t, i, j, n, u, v, mini, sum, sum1, ans, s, count, inf=999999;
	scanf("%d", &T);
	while(T--)
	{
		sum=0;
		s=0;
		scanf("%d", &n);
		for(i=1; i<=n; i++)
			for(j=1; j<=n; j++)
			{
				scanf("%d", &e[i][j]);
				s+=e[i][j];
			}
				
		for(i=1; i<=n; i++)
			for(j=1; j<=i; j++)
			{
				if(i==j)
				{
					sum+=e[i][j];
					e[i][j]=0;
				}
				else if(e[i][j]==0 && e[j][i]==0)
					e[i][j]=e[j][i]=inf;
				else if(e[i][j]!=0 && e[j][i]!=0)
				{
					sum+=max(e[i][j], e[j][i]);
					e[i][j]=e[j][i]=min(e[i][j], e[j][i]);
				}
				else e[i][j]=e[j][i]=max(e[i][j], e[j][i]);
			}
		ans=s-sum;	
		for(i=1; i<=n; i++)
			dis[i]=e[1][i];
		
		memset(book, false, sizeof(book));
		book[1]=true;
		count=1;
		sum1=0;
		int temp=1;
		while(count<n)
		{
			mini=inf;
			for(i=1; i<=n; i++)
			{
				if(mini>dis[i] && book[i]==false)
				{
					u=i;
					mini=dis[i];
				}
			}
			if(mini==inf)
			{
				temp=0;
				break;
			}
			book[u]=true;
			sum1+=dis[u];
			count++;
			for(v=1; v<=n; v++)
				if(dis[v]>e[u][v])
					dis[v]=e[u][v];
		}
		
		if(temp==0)
			printf("Case %d: -1\n", ++t);
		else
			printf("Case %d: %d\n", ++t, sum+ans-sum1);
	}	
	return 0;	
} 

 

标签:电线,1040,Prim,cable,house,sum,Donation,rooms,your
来源: https://blog.csdn.net/qq_41505957/article/details/99166677

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有