LightOJ 1040

诶~  你们说什么? 1038和1039 没掉了?~   这个嘛~后面有时间再把题解补上吧,虽然从通过率上来说的话 1038和1039应该更有说的必要一点点。

昨天的话 因为前天比赛太困  也没在实验室呆的太久,大概9点这样就滚回宿舍了。今天的话,早晨和中午被高代虐的挺惨的,但是既然要重新来一遍就无论如何都要昨晚对不对~就像现在也一直在写题目一样,虽然每天或多或少,质量或高或低,这都是后话了~关键是要有这样的心情。言归正传


1040 - Donation
Time Limit: 2 second(s)Memory Limit: 32 MB

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

Output for 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

Case 1: 105

Case 2: 12

Case 3: -1



现在稍微有意识到一点OJ题目和平时写CF和TC比赛的最大不同了吧。CF和TC的比赛绝大多数比较着重的考察idea的题目比较多。而在OJ做的话可以巩固基础算法,提升coding速度XD,至少我是这么想的。


题意:给一个图 0 点表示 点i至点j不存在一条边,需要求的就是,除了最小生成树之外,剩下的边总和是多少 (这个已经是经过题意理解变化数学模型之后的东西啦)


思路:诶 并没有什么好说的,要注意的地方就是:通过并查集过后father数组如果不统一求一次getfather有可能存的不是自己的祖先,而仅仅是自己的父亲,因此在判断连通性的时候一定要注意哟,至少我wa的一次是贡献给这个小细节了。 之后贴上代码 (其实每天写blog的目的大概也只是告诉自己“嗯 ,你还在做题”这样的心情而已吧)


#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int father[55];

struct node{
	int ui,vi,dis;
};
node edge[3005];
int cmp(node a,node b){
	if (a.dis!=b.dis)
		return (a.dis<b.dis);
	else if (a.ui!=b.ui)
		return (a.ui<b.ui);
	else
		return (a.vi<b.vi);
}
int cnt;
int ret;
int n;
int getfather(int now){
	if (father[now]!=now)
		father[now]=getfather(father[now]);
	return (father[now]);
}
int unio(int x,int y){
	int fx=getfather(x);
	int fy=getfather(y);
	if (fx!=fy){
		father[fx]=fy;
		return (1);
	}
	return (0);
}
void init(int cas){
	ret=0;
	cnt=0;
	scanf("%d",&n); 
	for (int i=0;i<n;i++)
		for (int j=0;j<n;j++){
			int tmp;
			scanf("%d",&tmp);
			ret+=tmp;
			if (tmp==0 || i==j)
				continue;
			edge[cnt].ui=i;edge[cnt].vi=j;edge[cnt].dis=tmp;
			cnt++;
		}
	memset(father,-1,sizeof(father));
	for (int i=0;i<n;i++)
		father[i]=i;
	sort(edge,edge+cnt,cmp);
	int sum=0;
	for (int i=0;i<cnt;i++){
		int tmp=unio(edge[i].ui,edge[i].vi);
		if (tmp==1)
			sum+=edge[i].dis;
	}
	bool check=true;
	for (int i=1;i<n;i++)
		if (getfather(i)!=getfather(i-1))
			check=false;
	if (check)
		printf("Case %d: %d\n",cas,ret-sum);
	else
		printf("Case %d: -1\n",cas);
}
int main(){
	int t;
	scanf("%d",&t);
	for (int cas=1;cas<=t;cas++)
		init(cas);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值