HDU2682 Tree 题解 【最小生成树】【图论】【Kruskal】

Problem Description
There are N (2<=N<=600) cities,each has a value of happiness,we consider two cities A and B whose value of happiness are VA and VB,if VA is a prime number,or VB is a prime number or (VA+VB) is a prime number,then they can be connected.What’s more,the cost to connecte two cities is Min(Min(VA , VB),|VA-VB|).
Now we want to connecte all the cities together,and make the cost minimal.
Input
The first will contain a integer t,followed by t cases.
Each case begin with a integer N,then N integer Vi(0<=Vi<=1000000).
Output
If the all cities can be connected together,output the minimal cost,otherwise output “-1”;
Sample Input
2
5
1
2
3
4
5

4
4
4
4
4
Sample Output
4
-1
解题报告
题意就是有N座城市,每个城市有一定的幸福值a[i]。对于任意两个城市i和j,如果a[i],a[j],a[i]+a[j]中任意一者的值为素数,那么他们的边权就是min(min(a[i],a[j]),abs(a[i]-a[j]))。问题就是这一幅图的最小生成树。
显然,边一旦建出来了,这就是一道裸题。

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=600;
const int M=600*600;
const int H=1000000*2;
const int INF=522133279;
int head[N+5],num,father[N+5];
int n;
int hap[N+5];
bool prime[H*2+5];
int T;
struct edge
{
    int u,v;
    int w;
    int next;
    edge(){next=-1;}
    bool operator<(const edge& b)const
    {return w<b.w;}
}ed[M<<2];
inline void build(int u,int v,int w)
{
    ed[++num].u=u;
    ed[num].v=v;
    ed[num].w=w;
    ed[num].next=head[u];
    head[u]=num;
}
int getfather(int x)  
{
    return father[x]!=x?(father[x]=getfather(father[x])):x;  
}  
inline int unionn(int x,int y)  
{
    return ((x=getfather(x))!=(y=getfather(y)))&&(father[x]=y);  
}
void pri()
{
    prime[0]=1,prime[1]=1,prime[2]=0;
    for(int i=2;i<=H;i++)
    if(!prime[2])
    {
        for(int j=i+i;j<=H;j+=i)
        prime[j]=1;
    }
}  
inline int kruskal()
{
    int ans=0;
    int tot=0;
    for(int i=1;i<=n;++i)
    father[i]=i;
    sort(ed+1,ed+1+num);
    for(int i=1;i<=num;++i)
    {
        int u=getfather(ed[i].v),v=getfather(ed[i].u);
        if(u!=v)
        {
            ++tot,ans+=ed[i].w;
            unionn(u,v);
        }
        if(tot==n-1)return ans;
    }
    return -1;
}
inline int w(int i,int j)
{
    if(!prime[i]||!prime[j]||!prime[i+j])
    return min(min(i,j),abs(i-j));
}
int main()
{
    pri();
    for(scanf("%d",&T);T;T--)
    {
        num=0;
        memset(head,-1,sizeof(head));
        memset(hap,0,sizeof(hap));
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&hap[i]);
            for(int j=1;j<=i-1;j++)
            {
                if(!prime[hap[i]]||!prime[hap[j]]||!prime[hap[i]+hap[j]])
                {
                    build(i,j,w(hap[i],hap[j]));
                    build(j,i,w(hap[i],hap[j]));
                }
            }
        }
        printf("%d\n",kruskal());
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值