【杭电2015年12月校赛D】【水题 最小生成树】Happy Value 最小生成树裸题

Happy Value

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1388    Accepted Submission(s): 408


Problem Description
In an apartment, there are N residents. The Internet Service Provider (ISP) wants to connect these residents with N – 1 cables.  
However, the friendships of the residents are different. There is a “Happy Value” indicating the degrees of a pair of residents. The higher “Happy Value” is, the friendlier a pair of residents is. So the ISP wants to choose a connecting plan to make the highest sum of “Happy Values”.
 

Input
There are multiple test cases. Please process to end of file.
For each case, the first line contains only one integer N (2<=N<=100), indicating the number of the residents.
Then N lines follow. Each line contains N integers. Each integer H ij(0<=H ij<=10000) in i th  row and j th  column indicates that i th  resident have a “Happy Value” H ijwith j th  resident. And H ij(i!=j) is equal to H ji. H ij(i=j) is always 0.
 

Output
For each case, please output the answer in one line.
 

Sample Input
  
  
2 0 1 1 0 3 0 1 5 1 0 3 5 3 0
 

Sample Output
  
  
1 8
 
#include<stdio.h>
#include<algorithm>
#include<ctype.h>
#include<string.h>
using namespace std;
int casenum,casei;
typedef long long LL;
const int N=105;
const int M=105*105;
int n,m;
struct A
{
    int x,y,z;
    bool operator < (const A& b)const
    {
        return z>b.z;
    }
}a[M];
int f[N];
int find(int x)
{
	return f[x]==x?x:f[x]=find(f[x]);
}
int main()
{
    while(~scanf("%d",&n))
    {
        m=0;
        for(int i=1;i<=n;++i)
        {
            f[i]=i;
            for(int j=1;j<=n;++j)
            {
                int x;
                scanf("%d",&x);
                if(i<j)
                {
                    ++m;
                    a[m].x=i;
                    a[m].y=j;
                    a[m].z=x;
                }
            }
        }
        int ans=0;
        sort(a+1,a+m+1);
        for(int i=1;i<=m;++i)
        {
            int x=find(a[i].x);
            int y=find(a[i].y);
            if(x!=y)
            {
                ans+=a[i].z;
                f[y]=x;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
/*
【题意】
就是一个完全图的最小生成树。什么也不说了= = 

*/


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值