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)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...
随机推荐
- Spring核心框架 - AOP的原理及源码解析
一.AOP的体系结构 如下图所示:(引自AOP联盟) 层次3语言和开发环境:基础是指待增加对象或者目标对象:切面通常包括对于基础的增加应用:配置是指AOP体系中提供的配置环境或者编织配置,通过该配置A ...
- Python Geospatial Development reading note(1)
chapter 1, Summary: In this chapter, we briefly introduced the Python programming language and the m ...
- gdb在Mac上的安装步骤
到gdb官网下载gdb安装包,解压后,在“终端”中进入其目录:然后依次执行如下命令行: 1st:CFLAGS='-Wno-string-plus-int -g -O2' ./configure --d ...
- Python属性、方法和类管理系列之----描述符类
什么是描述符类? 根据鸭子模型理论,只要具有__get__方法的类就是描述符类. 如果一个类中具有__get__和__set__两个方法,那么就是数据描述符,. 如果一个类中只有__get__方法,那 ...
- uva 1476 - Error Curves
对x的坐标三分: #include<cstdio> #include<algorithm> #define maxn 10009 using namespace std; do ...
- Android之监测手机网络状态的广播
Android之监测手机网络状态的广播 Android 监控网络状态 Android利用广播监听设备网络连接(断网)的变化情况
- Chapter 17. Objects and Inheritance(对象与继承)
javascript面向对象编程有几个层面: 1: 单一对象 (covered in Layer 1: Single Objects) 2: 对象之间的 prototype (described i ...
- [jobdu]孩子们的游戏(圆圈中最后剩下的数)
这道题就是典型的约瑟夫环问题.http://blog.csdn.net/wuzhekai1985/article/details/6628491 一开始想了一下用数组来做,就是模拟方法,同时记录一下是 ...
- Linux中断处理流程
http://blog.csdn.net/dianhuiren/article/details/7468956
- tlplayer for android V2.7(支持变速不变调) 2014-07-20更新
2014-07-20新版本已经修复部分视频倾斜问题,已经更新到服务器,需要的朋友自行下载. 此版本修正了倾斜问题,增加水印等功能.可以动态添加水印. tlplayer for android V2.6 ...