传送门

Description

A kingdom has n cities numbered 1 to n, and some bidirectional roads connecting cities. The capital is always city 1.
After a war, all the roads of the kingdom are destroyed. The king wants to rebuild some of the roads to connect the cities, but unfortunately, the kingdom is running out of money. The total cost of rebuilding roads should not exceed K.
Given the list of m roads that can be rebuilt (other roads are severely damaged and cannot be rebuilt), the king decided to maximize the total population in the capital and all other cities that are connected (directly or indirectly) with the capital (we call it "accessible population"), can you help him?

Input

The first line of input contains a single integer T (T<=20), the number of test cases. 
Each test case begins with three integers n(4<=n<=16), m(1<=m<=100) and K(1<=K<=100,000). 
The second line contains n positive integers pi (1<=pi<=10,000), the population of each city. 
Each of the following m lines contains three positive integers u, v, c (1<=u,v<=n, 1<=c<=1000), representing a destroyed road connecting city u and v, whose rebuilding cost is c. 
Note that two cities can be directly connected by more than one road, but a road cannot directly connect a city and itself.

Output

For each test case, print the maximal accessible population.

Sample Input

2
4 6 6
500 400 300 200
1 2 4
1 3 3
1 4 2
4 3 5
2 4 6
3 2 7
4 6 5
500 400 300 200
1 2 4
1 3 3
1 4 2
4 3 5
2 4 6
3 2 7 

Sample Output

1100
1000 

--------------------------------------------------------

状压dp

状压dp易写错的地方

1. for循环变量比较多,i, j, k 什么的容易和全局变量搞混,而且 循环变量/循环体 本身也容易写错。

2. 位运算操作符容易和对应的逻辑运算操作符搞混。

----------------------------------------------------------------

此题有坑---重边

----------------------------------------------------------------

#include <bits/stdc++.h>using namespace std;
int g[18][18], p[18];
const int N(1<<18);
int dp[N], co[N];
int T, n, m, k, u, v, c, ans;
inline int ones(int x){int res=0;for(int i=1; i<=n; i++){if(x&1<<i) res++;}return res;
}
inline void trans(int s, int i){for(int j=1; j<=n; j++){if(g[i][j]&&!(s&1<<j)&&co[s]+g[i][j]<=k){int t=s|1<<j;dp[t]=dp[s]+p[j];ans=max(ans, dp[t]);co[t]=co[t]?min(co[t], co[s]+g[i][j]):co[s]+g[i][j];}}
}
int main(){freopen("in", "r", stdin);for(cin>>T; T--;){cin>>n>>m>>k;for(int i=1; i<=n; i++) cin>>p[i];for(;m--;){cin>>u>>v>>c;if(!g[u][v]||g[u][v]>c) g[u][v]=g[v][u]=c;}dp[1<<1]=ans=p[1], co[1<<1]=0;for(int i=1; i<n; i++){ //iterate over onesfor(int j=1<<1, tot=1<<n+1; j<tot; j++){ //iterate over stateif(ones(j)==i&&dp[j]){for(int k=1; k<=n; k++){ //iterate over bitsif(j&1<<k){trans(j, k);}}}}}printf("%d\n", ans);memset(dp, 0, sizeof(dp));memset(co, 0, sizeof(co));memset(g, 0, sizeof(g));}
}

转载于:https://www.cnblogs.com/Patt/p/4730925.html

CSU 1116 Kingdoms相关推荐

  1. qt5 linux 控制台 乱码,qt5.12 解决显示中文乱码问题

    Python之Mac上搭建集成开发环境 首先下载一个东西: 找到下载地址:https://download.jetbrains.8686c.com/python/pycharm-professiona ...

  2. CSU 1113 Updating a Dictionary(map容器应用)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1113 解题报告:输入两个字符串,第一个是原来的字典,第二个是新字典,字典中的元素的格式为 ...

  3. csu 1554: SG Value 思维题

    http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1554 这题在比赛的时候居然没想出来,然后发现居然是做过的题目的变种!!!! 先不考虑插入操作, ...

  4. 模拟 CSU 1562 Fun House

    题目传送门 1 /* 2 题意:光线从 '*' 发射,遇到 '/' 或 '\' 进行反射,最后射到墙上,将 'x' 变成 '&' 3 模拟:仔细读题,搞清楚要做什么,就是i,j的移动,直到撞到 ...

  5. 组合数学(全排列)+DFS CSU 1563 Lexicography

    题目传送门 1 /* 2 题意:求第K个全排列 3 组合数学:首先,使用next_permutation 函数会超时,思路应该转变, 4 摘抄网上的解法如下: 5 假设第一位是a,不论a是什么数,ax ...

  6. js进阶 11-16 jquery如何查找元素的父亲、祖先和子代、后代

    js进阶 11-16 jquery如何查找元素的父亲.祖先和子代.后代 一.总结 一句话总结:过滤或者查找的方法里面可以带参数进行进一步的选择. 1.parent()和parents()方法的区别是什 ...

  7. PAT甲级1116 Come on! Let‘s C:[C++题解]哈希表、素数

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析:使用哈希表存一下每个id获得奖,然后查询输出即可. ac代码 #include<bits/stdc++.h> using ...

  8. CSU 1337 搞笑版费马大定理(2013湖南省程序设计竞赛J题)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1337 解题报告:虽然x和y的范围都是10^8,但是如果a 是大于1000的话,那么a^3 ...

  9. LeetCode 多线程 1116. 打印零与奇偶数

    1116. 打印零与奇偶数 Ideas 有几个线程就用几个信号量,最先开始的信号量初始化为1,其它初始化为0,然后根据条件判断实现同步. 多线程的问题好多都是:锁自己,解锁别人. Code from ...

最新文章

  1. nodejs报错解决:Error: Can only perform operation while paused. - undefined
  2. 联合国发布AI报告:自动化和AI对亚洲有巨大影响【附报告下载】
  3. mysql预处理 更新_MySQL 预处理方法更新删除-2018年04月27日00时59分
  4. 深入java并发包源码(三)AQS独占方法源码分析
  5. s3c2440的内存管理机制
  6. 世界是个班,美国是班长,中国是团支书(太经典了!)
  7. idea 快速导入实现父类方法_三步快速提高物理成绩!准初三生暑假实现逆袭的实用方法...
  8. mac git配置 idea
  9. 银行业应用系统监控的维度与目标
  10. 转录组和蛋白质组结合分析-入门笔记
  11. 苹果商城怎么调成中文_海豚加速器拳头账号中文注册下载-海豚加速器拳头账号注册下载 v2020...
  12. python 股票估值_隐藏价值的角落:限售股AAP估值及PYTHON实现方法(上)
  13. 让你彻底理解线性代数中的概念——《线性代数的本质》系列视频笔记
  14. 手机控制树莓派驱动投影仪DLPDLCR230NPEVM
  15. 修改chrome滚动条的样式
  16. 转-Linux下装飞秋
  17. 优动漫PAINT入门宝典——素材的导出与上传
  18. TeamViewer服务器怎么设置?
  19. MICCAI Proceedings(MICCAI会议论文)的TEX模板下载
  20. 《你好,李焕英》爆红,Python爬虫+数据分析告你票房为什么这么高?

热门文章

  1. C++中图的简单表示法
  2. python存储数据丢失的存储器是_数据缺失值的4种处理方法
  3. 梯度下降法的三种形式批量梯度下降法、随机梯度下降以及小批量梯度下降法
  4. Photoshop cs6中kuler和mini bridge打开是空白的解决方法
  5. 【图像处理】透视变换 Perspective Transformation(小细节修正和推导流程补充)
  6. rocketmq 有哪些监控工具_Kafka和RocketMQ底层存储之那些你不知道的事
  7. jmeter一个线程组多个请求_分享一些我在实际项目中使用jmeter压测的一些技术点跟一些踩过的坑吧...
  8. 知道路程时间求加速度_凸轮分割器的出力轴加速度是怎么算的
  9. 学计算机的人玩什么游戏,亲戚眼中的大学专业:学的计算机啊,游戏打的肯定贼好吧...
  10. python播放网络音乐_python使用Tkinter实现在线音乐播放器