bzoj3714 [PA2014]Kuglarz

题目

一道神题,一开始还想了很多,什么博弈论之类的,233,还是太年轻啊。

看了看别人的题解后,茅塞顿开,可以说是很可以的。

我们要知道每个杯子下的情况,就要知道每一个(废话),所以就是最小代价知道所有,一个区间[i,j],知道[1,i-1],[1,j],就知道了,而且三个是知二推一,所以我们把0~n连起来就好了(感性的认识一下),想到了什么?

对,最小生成树!连i-1到j,再跑一遍就是答案了。

#include<bits/stdc++.h>
#define N 2000
using namespace std;
int n,m,f[N+1];
long long ans;
struct edge{
    int x;
    int y;
    int len;
    bool operator < (const edge &A) const
    {
        return len<A.len;
    }
};edge A[N*N+1];
inline char nc()
{
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int read()
{
    int x=0,b=1;
    char c=nc();
    for(;!(c<='9'&&c>='0');c=nc())if(c=='-')b=-1;
    for(;c<='9'&&c>='0';c=nc())x=x*10+c-'0';
    return x*b;
}
inline int find(int x)
{
    return x==f[x]?f[x]:f[x]=find(f[x]);
} 
int main()
{
    //freopen("in.txt","r",stdin);
    n=read();
    for(int i=1;i<=n;i++)
        for(int j=i;j<=n;j++)
            A[++m].x=i-1,A[m].y=j,A[m].len=read();
    for(int i=0;i<=n;i++)f[i]=i;
    sort(A+1,A+m+1); 
    for(int i=1;i<=m;i++)
    {
        int fx=find(A[i].x),fy=find(A[i].y);
        if(fx==fy)continue;
        f[fx]=fy;
        ans+=A[i].len;
    }
    cout<<ans;
    return 0;
} 


话说fread真是好东西,怎么写怎么快。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值