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)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...
随机推荐
- Unity3d Shader开发(三)Pass(Texturing )
纹理在基本的顶点光照被计算后被应用.在着色器中通过SetTexture 命令来完成. SetTexture 命令在片面程序被使用时不会生效:这种模式下像素操作被完全描述在着色器中. 材质贴图可以用 ...
- winform 项目获取app.config 中appSettings节点数据
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSe ...
- python mongodb MapReduce
# -*- coding: utf-8 -*-import osimport csvimport pymongofrom pymongo import MongoClientfrom bson.cod ...
- 【弱省胡策】Round #5 Handle 解题报告
这个题是我出的 sb 题. 首先,我们可以得到: $$A_i = \sum_{j=i}^{n}{j\choose i}(-1)^{i+j}B_j$$ 我们先假设是对的,然后我们把这个关系带进来,有: ...
- codeforces 390D Inna and Sweet Matrix
几个小结论: 1.路径长度=i+j-1; 2.最简单的走法是先横走再竖着走或者先竖着走再横着走 #include<cstdio> #include<cstring> using ...
- Fast CGI 工作原理
http://www.cppblog.com/woaidongmao/archive/2011/06/21/149092.html 一.FastCGI是什么? FastCGI是语言无关的.可伸缩架构的 ...
- su: /bin/bash: Permission denied
https://bbs.archlinux.org/viewtopic.php?id=105541 New user created as: groupadd mygroupuseradd -s /b ...
- vs2012+opencv2.4.7 实现单张人脸识别
参考:http://blog.sina.com.cn/s/blog_593c85f20100ncnj.html OpenCV的库中带有检测正面人脸的 Haar迭代算法Haar Cascade Face ...
- HTTP Header 入门详解
什么是HTTP Headers HTTP是"Hypertext Transfer Protocol"的所写,整个www都在使用这种协定,几乎你在流览器里看到的大部分内容都是通过ht ...
- 134. Gas Station
题目: There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. ...