HDU5418.Victor and World(状压DP)
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm> using namespace std; typedef long long ll;
const int INF = 0x5f5f5f5f;
const int MAX = 1 << 17; int mp[20][20];
int dp[MAX + 1][20]; // dp[s][v] 走过s点,现在在v点 dp[S][0] int bit[1<<17]; int main()
{
//freopen("in.txt", "r", stdin); for(int i = 0; i<(1<<16); i++) // bit[i] := i的二进制有多少个1
{
bit[i] = 0;
for (int j = 0; j <= 16; j++)
if (i & (1 << j))
bit[i]++;
} int t;
int m, n;
scanf("%d", &t);
while (t--) {
memset(mp, -1, sizeof mp);
for (int i = 0; i < MAX; ++i)
for (int j = 0; j < 16; ++j)
dp[i][j] = INF; scanf("%d%d", &n, &m);
int a, b, c;
for (int i = 0; i < m; ++i) {
scanf("%d%d%d", &a, &b, &c);
if (mp[a - 1][b - 1] == -1 || mp[a - 1][b - 1] > c)
mp[a - 1][b - 1] = mp[b - 1][a - 1] = c;
} int maxn = (1 << n) - 1; dp[1][0] = 0;
for (int s = 1; s <= maxn; ++s) {
for(int k = 0; k <= bit[s]; ++k) {
for (int v = 0; v < n; ++v) {
if (dp[s][v] != INF) {
for (int u = 0; u < n; ++u) {
if (mp[u][v] != -1) {
if (dp[s | (1 << u)][u] > dp[s][v] + mp[u][v])
dp[s | (1 << u)][u] = dp[s][v] + mp[u][v];
}
}
}
}
}
} printf("%d\n", dp[maxn][0]);
} return 0;
} /**
Input:
2
3 2
1 2 2
1 3 3
5 4
1 2 1
2 3 1
3 4 1
4 5 1 Output:
10
8
*/
wa了好久,我只想知道
for(int k = 0; k <= bit[s]; ++k)
什么意思,为什么要加这句?
HDU5418.Victor and World(状压DP)的更多相关文章
- HDOJ 5418 Victor and World 状压DP
水状压DP Victor and World Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K (Java ...
- [hdu5418 Victor and World]floyd + 状压DP 或 SPFA
题意:给n个点,m条边,每次只能沿边走,花费为边权值,求从1出发经过所有其它点≥1次最后回到1的最小花费. 思路: 状压DP.先用Floyd得到任意两点间的最短距离,转移时沿两个点的最短路转移.此时的 ...
- POJ 3311 Hie with the Pie (状压DP)
题意: 每个点都可以走多次的TSP问题:有n个点(n<=11),从点1出发,经过其他所有点至少1次,并回到原点1,使得路程最短是多少? 思路: 同HDU 5418 VICTOR AND WORL ...
- BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3336 Solved: 1936[Submit][ ...
- nefu1109 游戏争霸赛(状压dp)
题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1109 //我们校赛的一个题,状压dp,还在的人用1表示,被淘汰 ...
- poj3311 TSP经典状压dp(Traveling Saleman Problem)
题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...
- [NOIP2016]愤怒的小鸟 D2 T3 状压DP
[NOIP2016]愤怒的小鸟 D2 T3 Description Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可 ...
- 【BZOJ2073】[POI2004]PRZ 状压DP
[BZOJ2073][POI2004]PRZ Description 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 桥已经很旧了, 所以它不能承受太重的东西. 任何时候队伍 ...
- bzoj3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(spfa+状压DP)
数据最多14个有宝藏的地方,所以可以想到用状压dp 可以先预处理出每个i到j的路径中最小权值的最大值dis[i][j] 本来想用Floyd写,无奈太弱调不出来..后来改用spfa 然后进行dp,这基本 ...
- HDU 1074 Doing Homework (状压dp)
题意:给你N(<=15)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...
随机推荐
- ArcGIS API for JavaScript 学习笔记(一)
终于开始了 噩梦一般的ArcGIS 我特别不习惯这种结构化的程序写法 写起来特别吃力 特别是把几个功能整合到同一个页面去的时候. 写程序的时候,一般我喜欢先写个Demo然后再把Demo上面的功能加到页 ...
- how to run demo city bars using sencha architect
1. create a project using city bars template in sencha architect 2. save your project name as CityBa ...
- makefile懒人版(单个文件编译)
.PHONY:clean all CC=gcc CFLAGS=-Wall -g ###replace your bin BIN=1 2 3 4 all:$(BIN) %.o:%.c $(CC) $(C ...
- asp.net 文件复制或删除用相对路径,File.Copy中用相对路径,巧用相对路径复制文件
再复制文件 File.Copy(Server.MapPath("被复制的文件相对路径"), Server.MapPath("目的位置相对路径"), true); ...
- 百度:在O(1)空间复杂度范围内对一个数组中前后连段有序数组进行归并排序
一.题目理解 题目:数组al[0,mid-1]和al[mid,num-1]是各自有序的,对数组al[0,num-1]的两个子有序段进行merge,得到al[0,num-1]整体有序.要求空间复杂度为O ...
- LibLinear(SVM包)使用说明之(三)实践
LibLinear(SVM包)使用说明之(三)实践 LibLinear(SVM包)使用说明之(三)实践 zouxy09@qq.com http://blog.csdn.net/zouxy09 我们在U ...
- c语言的自动类型转换
转自c语言的自动类型转换 自动转换遵循以下规则: 1) 若参与运算量的类型不同,则先转换成同一类型,然后进行运算. 2) 转换按数据长度增加的方向进行,以保证精度不降低.如 ...
- cxf2.4.3中jaxb-api.jar、jaxws-api.jar与jdk1.6.0_02不兼容问题
http://chxiaowu.iteye.com/blog/1243475 Exception in thread "main" java.lang.NoClassDefFoun ...
- 【NOIP 2015 DAY2 T3】 运输计划 (树链剖分-LCA)
题目背景 公元 2044 年,人类进入了宇宙纪元. 题目描述 L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之间,这 n-1 条航道连通了 L 国的所有星球. 小 P 掌管一家 ...
- easyui源码翻译1.32--DateTimeBox(日期时间输入框)
前言 扩展自$.fn.datebox.defaults,使用$.fn.datetimebox.defaults重写默认值对象.下载该插件翻译源码 和日期输入框类似,日期时间输入框允许用户选择日期和指定 ...