zoukankan      html  css  js  c++  java
  • bzoj3714[PA2014]Kuglarz*

    bzoj3714[PA2014]Kuglarz

    题意:

    n个杯子排成一行,花费c_ij元,可以知道杯子i,i+1,…,j底下藏有球的总数的奇偶性。求问至少需要花费多少元才能保证猜出哪些杯子底下藏着球。

    题解:

    令杯子1..i的和为sum[i],那么当知道sum[i]和sum[i-1]即可推算出i下是否有球。故可以连边(i-1,j,c_ij),然后求最小生成树即可。注意点数会变为n+1,因为0也是点。

    代码:

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #define inc(i,j,k) for(int i=j;i<=k;i++)
     5 #define maxn 2010
     6 using namespace std;
     7 
     8 inline int read(){
     9     char ch=getchar(); int f=1,x=0;
    10     while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
    11     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    12     return f*x;
    13 }
    14 struct e{int f,t,w;}; e es[maxn*maxn]; int ess;
    15 bool cmp(e a,e b){return a.w<b.w;}
    16 int n,fa[maxn],cnt; long long ans;
    17 int find(int x){
    18     return x==fa[x]?x:fa[x]=find(fa[x]);
    19 }
    20 int main(){
    21     n=read(); inc(i,1,n)inc(j,i,n){es[++ess]=(e){i-1,j,read()};}
    22     sort(es+1,es+ess+1,cmp); inc(i,1,n)fa[i]=i;
    23     inc(i,1,ess){
    24         int x=find(es[i].f),y=find(es[i].t); if(x!=y)fa[x]=y,cnt++,ans+=es[i].w;
    25         if(cnt==n)break;
    26     }
    27     printf("%lld",ans); return 0;
    28 }

    20160830

  • 相关阅读:
    学习笔记——SQL SERVER2014内存数据库
    学习笔记——WCF
    线程
    文件内容操作类-RandomAccessFile
    文件操作类-file-创建文件夹
    同步方法解决同步问题
    同步代码块
    停止线程
    使用泛型来优化坐标类
    数据操作流-DataOutputStream
  • 原文地址:https://www.cnblogs.com/YuanZiming/p/5838050.html
Copyright © 2011-2022 走看看