HDU计算机学院大学生程序设计竞赛(2015’12)Happy Value

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 ij with j thresident. 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<bits/stdc++.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<sstream>
#include<set>
#include<queue>
//#include<map>
#include<vector>
#include<algorithm>
#include<limits.h>
#define inf 0x3fffffff
#define INF 0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define ULL unsigned long long
using namespace std;
int map[110][110];
int dis[110];
int flag[110];
int n;
int Prime()
{
    int i,j;
    int pos,sum=0;
    memset(flag,0,sizeof(flag));
    for(i=1; i<=n; i++)
    {
        dis[i]=map[1][i];
    }
    flag[1]=1;
    for(i=1; i<n; i++)
    {
        int mid=inf;
        for(j=1; j<=n; j++)
        {
            if(!flag[j]&&dis[j]<mid)
            {
                mid=dis[j];
                pos=j;
            }
        }
        flag[pos]=1;
        sum+=mid;
        for(j=1; j<=n; j++)
        {
            if(!flag[j]&&map[pos][j]<dis[j])
            {
                dis[j]=map[pos][j];
            }
        }
    }
    return sum;
}
int main()
{
    int i,j;
    int q,a,b;
    while(~scanf("%d",&n))
    {
        memset(map,0,sizeof(map));
        int ans;
        for(i=1; i<=n; i++)
        {
            for(j=1; j<=n; j++)
            {
                scanf("%d",&ans);
                map[i][j]=-ans;
            }
        }
        printf("%d\n",-Prime());
    }
    return 0;
}

  

 

转载于:https://www.cnblogs.com/yinghualuowu/p/5087151.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值