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 Hij(0<=Hij<=10000) in ith row and jth column indicates that ith resident have a “Happy Value” Hij with jthresident. And Hij(i!=j) is equal to Hji. Hij(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;
}

  

 

最新文章

  1. MySQL中有关TIMESTAMP和DATETIME的总结
  2. .NET导入导出Excel方法总结
  3. Windows 下安装cryptography-1.6
  4. centos&#39;的yum安装php的memcache扩展
  5. 什么是 jsonp ?
  6. 原生DOM探究 -- NodeList v.s. HTMLCollection
  7. 淘宝账号基于OAuth2.0的登录验证授权登陆第三方网站
  8. C++中operator关键字(重载操作符)
  9. Java之final、finalize、finally的区别
  10. 一个mapreduce得到需要计算单词概率的基础数据
  11. 201521123109《java程序设计》第一周学习总结
  12. Django之验证码
  13. Hillstone设备管理-设备软件Stone-OS升级
  14. docker常用命令(自用)
  15. 你了解大O符号(big-O notation)么?你能给出不同数据结构的例子么?
  16. springboot Autowired BeanNotOfRequiredTypeException
  17. 01List.ashx(班级列表动态页面)
  18. TAF /tars必修课(一):整体架构理解
  19. Vim正则表达式匹配替换字符串
  20. Speech Synthesis

热门文章

  1. js闭包(二)
  2. JVM实用参数(三)打印所有XX参数及值
  3. var_dump — 打印变量的相关信息
  4. C++面向对象类的实例题目八
  5. SpringBoot05 数据操作01 -&gt; JPA的基本使用、基本使用02
  6. php学习笔记-if else
  7. can通信实验
  8. MySQL导出导入命令的用例
  9. matlab任务:FCM分类
  10. c++ 类成员变量初始化总结