2015’12杭电校赛1004 Happy Value (最大生成树)
2016-01-06 21:51
232 查看
题解
最大生成树,用kruskal算法把排序函数符合改一下就是最大生成树了
代码
#include <cstdio> #include <queue> #include <cstring> #include <iostream> #include <cstdlib> #include <algorithm> #include <vector> #include <map> #include <string> #include <set> #include <ctime> #include <cmath> #include <cctype> using namespace std; #define MAX 100000 #define LL long long struct Edge { int from,to,dist; Edge(){} Edge(int u,int v,int d):from(u),to(v),dist(d){} }edge[110*110]; int f[110]; bool cmp(Edge a,Edge b) { return a.dist>b.dist; } int Find(int x){ if(f[x] == x) return x; return f[x] = Find(f[x]); } int main() { int n; while(scanf("%d",&n)!=EOF) { int num = 0; for(int i=1;i<=n;i++) f[i] = i; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { int w; scanf("%d",&w); edge[num++] = Edge(i,j,w); } } sort(edge,edge+num,cmp); int ans = 0; for(int i=0;i<num;i++) { int u = edge[i].from,v = edge[i].to,w = edge[i].dist; u = Find(f[u]); v = Find(f[v]); if(u==v) continue; f[u] = v; ans += w; } printf("%d\n",ans); } }
题目
Happy Value
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1455 Accepted Submission(s): 429
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 jth resident. 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
20 1
1 0
3
0 1 5
1 0 3
5 3 0
Sample Output
18
相关文章推荐
- 【Objective-C学习记录】第三十六天
- Swift入门———逻辑分支
- 微信朋友圈技术之道:三个人的后台团队与每日十亿的发布量
- Android 上层应用读写设备节点
- android框架--xUtils
- 基于iOS,Android的服务器证书失效检测
- android之旅24 多媒体编程:音乐、视频、拍照、视频
- 关于android应用闪屏的几种情况
- ios crash的原因与抓取crash日志的方法
- android学习日记(五):ViewPager详解上
- js中call与apply的用法小结
- Android自定义View
- android AsyncTask介绍
- iOS 更轻量级的控制器 数据请求和解析的封装
- Android中的Loaders机制
- libuv在cocos2d-x中的使用
- 那些年踩过的坑之移动端
- iOS地球坐标,火星坐标,百度坐标互转
- Android使用Volley解析网易云音乐Api
- Spring Cloud App(Service) Pom示例