4525: [Cerc2012]Kingdoms

题意

  n个国家,两两之间可能存在欠债或者被欠债的关系,一个国家破产:其支出大于收入。问一个国家能否坚持到最后。

思路

  很有意思的一道题。

  dp[s]表示在当前国家存在情况为s,1-存活,0-破产。那么起始为11111...。然后枚举一个国家,判断是否可以破产,转移。

  最后,扫一遍所有只有1的情况。

代码

 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream> using namespace std; const int N = ;
int d[N][N],f[(<<)+]; inline int read() {
int x = ,f = ;char ch = getchar();
for (; !isdigit(ch); ch=getchar()) if(ch=='-') f=-;
for (; isdigit(ch); ch=getchar()) x = x*+ch-'';
return x*f;
} int main() {
int T = read();
while (T--) {
int n = read();
for (int i=; i<=n; ++i)
for (int j=; j<=n; ++j)
d[i][j] = read();
int MaxS = (<<n)-;
for (int i=; i<=MaxS; ++i) f[i] = false;
f[MaxS] = true;
for (int s=MaxS; s>=; --s) {
if (!f[s]) continue;
int ans = ;
for (int i=; i<=n; ++i) {
if (!(s&(<<(i-)))) continue;
int sum = ;
for (int j=; j<=n; ++j)
if (s&(<<(j-))) sum += d[j][i]; //j欠i=>i可以获得的
if (sum < ) f[s^(<<(i-))] = true;
}
}
bool flag = true;
for (int i=; i<=n; ++i) {
if (f[(<<(i-))]) {
if (flag) printf("%d",i),flag = false;
else printf(" %d",i);
}
}
if (flag) printf("");
puts("");
}
return ;
}

4525: [Cerc2012]Kingdoms的更多相关文章

  1. BZOJ 4057: [Cerc2012]Kingdoms( 状压dp )

    状压dp.... 我已开始用递归结果就 TLE 了... 不科学啊...我dp基本上都是用递归的..我只好改成递推 , 刷表法 将全部公司用二进制表示 , 压成一个数 . 0 表示破产 , 1 表示没 ...

  2. BZOJ4057 [Cerc2012]Kingdoms

    题意 有一些王国陷入了一系列的经济危机.在很多年以前,他们私底下互相借了许多钱.现在,随着他们的负债被揭发,王国的崩溃不可避免地发生了--现在有n个王国,对于每对王国A和B,A欠B的钱被记为d_AB( ...

  3. BZOJ 4057: [Cerc2012]Kingdoms

    状压DP #include<cstdio> #include<cstring> using namespace std; int F[1200005],A[25][25],st ...

  4. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  5. csuoj 1116: Kingdoms

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1116 1116: Kingdoms Time Limit: 3 Sec  Memory Limit ...

  6. BZOJ 4059: [Cerc2012]Non-boring sequences ( )

    要快速在一段子序列中判断一个元素是否只出现一次 , 我们可以预处理出每个元素左边和右边最近的相同元素的位置 , 这样就可以 O( 1 ) 判断. 考虑一段序列 [ l , r ] , 假如我们找到了序 ...

  7. 4063: [Cerc2012]Darts

    4063: [Cerc2012]Darts Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 85  Solved: 53[Submit][Status] ...

  8. 【BZOJ4061】[Cerc2012]Farm and factory(最短路,构造)

    [BZOJ4061][Cerc2012]Farm and factory(最短路,构造) 题面 BZOJ 然而权限题QwQ. 题解 先求出所有点到达\(1,2\)的最短路,不妨记为\(d_{u,1}, ...

  9. UVA 12507 Kingdoms

    D - Kingdoms Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu A kingdom ha ...

随机推荐

  1. hibernate_HelloWorld

    环境准备 1.下载 hibernate 3.3.2: 2.下载 hibernate 3.4.0: 3.注意阅读 hibernate compatibility matrix(hibernate 网站, ...

  2. 转发-react 性能深度探讨

    作者:尤雨溪链接:https://www.zhihu.com/question/31809713/answer/53544875来源:知乎 这里面有好几个方面的问题. 1. 原生 DOM 操作 vs. ...

  3. 洛谷 P2814 家谱

    题目背景 现代的人对于本家族血统越来越感兴趣. 题目描述 给出充足的父子关系,请你编写程序找到某个人的最早的祖先. 输入输出格式 输入格式: 输入由多行组成,首先是一系列有关父子关系的描述,其中每一组 ...

  4. while循环小例

    # 使用while 循环输入 1 2 3 4 5 6 8 9 10 n = 1 while n <= 10: if n == 7: pass else: print(n) n = n + 1 # ...

  5. UESTC 757 棋盘

    虽然是水题,但是还是很interesting的.(大概就是我最晚出这个题了... 博弈感觉就是靠yy能力啊.这题是对称性. 最后的必败态是白色格子对称的,一旦对称形成,对手怎么选,跟随就好,对手无法摆 ...

  6. Hash模板

    ;//一般为靠近总数的素数 struct Hashtable { int x;//hash存的值 Hashtable * next; Hashtable() { next = ; } }; Hasht ...

  7. CPU体系结构

    http://blog.csdn.net/liuxc0116/article/details/17004313 1.算术逻辑单元ALU(Arithmetic Logic Unit)ALU是运算器的核心 ...

  8. 20145238-荆玉茗 《Java程序设计》第四次实验报告

    20145238<Java程序设计>第四次实验报告 实验四 Android环境搭建 实验内容 1.搭建Android环境 2.运行Android 3.修改代码,能输出学号 实验步骤 搭建A ...

  9. numpy中的inf

    numpy中的inf表示一个无限大的正数 import numpy x = numpy.inf x>9999999999999999999 结果为: True

  10. 【洛谷P3390】矩阵快速幂

    矩阵快速幂 题目描述 矩阵乘法: A[n*m]*B[m*k]=C[n*k]; C[i][j]=sum(A[i][1~n]+B[1~n][j]) 为了便于赋值和定义,我们定义一个结构体储存矩阵: str ...