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

    Description

    魔术师的桌子上有n个杯子排成一行,编号为1,2,…,n,其中某些杯子底下藏有一个小球,如果你准确地猜出是哪些杯子,你就可以获得奖品。花费c_ij元,魔术师就会告诉你杯子i,i+1,…,j底下藏有球的总数的奇偶性。
    采取最优的询问策略,你至少需要花费多少元,才能保证猜出哪些杯子底下藏着球?

    Input

    第一行一个整数n(1<=n<=2000)。
    第i+1行(1<=i<=n)有n+1-i个整数,表示每一种询问所需的花费。其中c_ij(对区间[i,j]进行询问的费用,1<=i<=j<=n,1<=c_ij<=10^9)为第i+1行第j+1-i个数。

    Output

    输出一个整数,表示最少花费。

    Sample Input

    5
    1 2 3 4 5
    4 3 2 1
    3 4 5
    2 1
    5

    Sample Output

    7

    正解:最小生成树。

    与狡猾的商人类似,知道一个区间的奇偶性,实际上就是连了一条边,如果这个图连通,那么所有的点就都知道奇偶性了。

    于是我们把$[l,r+1]$连上对应边权,做一遍最小生成树即可,好像要用$prim$。

     1 #include <bits/stdc++.h>
     2 #define il inline
     3 #define RG register
     4 #define ll long long
     5 
     6 using namespace std;
     7 
     8 int e[2010][2010],dis[2010],vis[2010],n;
     9 ll ans;
    10 
    11 il int gi(){
    12   RG int x=0,q=1; RG char ch=getchar();
    13   while ((ch<'0' || ch>'9') && ch!='-') ch=getchar();
    14   if (ch=='-') q=-1,ch=getchar();
    15   while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar();
    16   return q*x;
    17 }
    18 
    19 il void prim(){
    20   memset(dis,0x3f3f3f,sizeof(dis)),dis[1]=0;
    21   for (RG int i=1,now,mn;i<=n;++i){
    22     now=0,mn=2147483640;
    23     for (RG int j=1;j<=n;++j)
    24       if (!vis[j] && mn>dis[j]) now=j,mn=dis[j];
    25     ans+=mn,vis[now]=1;
    26     for (RG int j=1;j<=n;++j)
    27       if (!vis[j] && e[now][j]) dis[j]=min(dis[j],e[now][j]);
    28   }
    29   return;
    30 }
    31 
    32 int main(){
    33 #ifndef ONLINE_JUDGE
    34   freopen("Kuglarz.in","r",stdin);
    35   freopen("Kuglarz.out","w",stdout);
    36 #endif
    37   n=gi();
    38   for (RG int i=1;i<=n;++i)
    39     for (RG int j=i;j<=n;++j) e[i][j+1]=e[j+1][i]=gi();
    40   ++n,prim(),cout<<ans; return 0;
    41 }
  • 相关阅读:
    注册时按钮上的时间倒计时
    不能修改/删除/添加数据.(NTFS问题)
    站在2009年的门槛上
    超强PHP分页类(转自PHPCHINA)
    System.Web.Caching.Cache类 缓存 各种缓存依赖
    Wxpython快速构建GUI窗口程序
    Python2 和 Python3 有哪些差别
    12306数据库遭泄露,请尽快修改密码
    王欣复出后的第一款产品
    在命令行打开安卓UI界面
  • 原文地址:https://www.cnblogs.com/wfj2048/p/7668060.html
Copyright © 2011-2022 走看看