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 Hij(0<=Hij<=10000) in ith row and jth column indicates that ith resident have a “Happy Value” Hijwith 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
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 Hij(0<=Hij<=10000) in ith row and jth column indicates that ith resident have a “Happy Value” Hijwith 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
2
0 1
1 0
3
0 1 5
1 0 3
5 3 0
Sample Output
1 8
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int casenum,casei;
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
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;
}a[M];
//排序
bool cmp(A p1,A p2)
{
return p1.z>p2.z;
}
int f[N];
int find(int x)
{
return f[x]==x?x:f[x]=find(f[x]);
}
int main()
{
while(cin>>n)
{
m=0;
for(int i=1;i<=n;i++)
{
f[i]=i;
for(int j=1;j<=n;j++)
{
int x;
cin>>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,cmp);
//排序
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;
}
}
cout<<ans<<endl;
}
}
const int N=105;
const int M=105*105;
int n,m;
struct A
{
int x,y,z;
}a[M];
//排序
bool cmp(A p1,A p2)
{
return p1.z>p2.z;
}
int f[N];
int find(int x)
{
return f[x]==x?x:f[x]=find(f[x]);
}
int main()
{
while(cin>>n)
{
m=0;
for(int i=1;i<=n;i++)
{
f[i]=i;
for(int j=1;j<=n;j++)
{
int x;
cin>>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,cmp);
//排序
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;
}
}
cout<<ans<<endl;
}
}
最后
以上就是寒冷飞鸟最近收集整理的关于prim算法的水题的全部内容,更多相关prim算法内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复