hdu3001(状态压缩DP)
hdu3001
题意
选择从任意一点出发,经过所有点的最小花费(经过每个点的次数不能多于 2 次)。
分析
类似于 poj3311
经过每个点的次数有限制,考虑用三进制数存储每个点被访问过的次数,其它和上面一题几乎相同。
code
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int MAXN = 60000;
const int INF = 1e9;
int dp[MAXN][12];
int dis[12][12];
int main() {
int m, n;
while(cin >> n >> m) {
memset(dis, 0x3f, sizeof dis);
memset(dp, 0x3f, sizeof dp);
for(int i = 0; i < m; i++) {
int x, y, c;
cin >> x >> y >> c; x--; y--;
dis[y][x] = dis[x][y] = min(c, dis[x][y]);
}
int total = 1;
for(int i = 0; i < n; i++) {
dis[i][i] = 0;
total *= 3;
}
for(int i = 0; i < total; i++) {
for(int j = 0; j < n; j++) {
int s = i, add = 1;
int t = j;
while(t--) {
s /= 3;
add *= 3;
}
s %= 3;
if(s < 2) {
dp[add][j] = 0; // 可以从任意一点出发
s = i;
for(int k = 0; k < n; k++) {
if(s % 3 > 0) {
dp[i + add][j] = min(dp[i + add][j], dp[i][k] + dis[k][j]);
}
s /= 3;
}
}
}
}
int ans = INF;
for(int j = 0; j < n; j++) {
ans = min(ans, dp[total - 1][j]);
}
if(ans == INF) ans = -1;
cout << ans << endl;
}
return 0;
}
hdu3001(状态压缩DP)的更多相关文章
- DP大作战—状态压缩dp
题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...
- 状态压缩dp相关
状态压缩dp 状态压缩是设计dp状态的一种方式. 当普通的dp状态维数很多(或者说维数与输入数据有关),但每一维总 量很少是,可以将多维状态压缩为一维来记录. 这种题目最明显的特征就是: 都存在某一给 ...
- 状态压缩DP(大佬写的很好,转来看)
奉上大佬博客 https://blog.csdn.net/accry/article/details/6607703 动态规划本来就很抽象,状态的设定和状态的转移都不好把握,而状态压缩的动态规划解决的 ...
- hoj2662 状态压缩dp
Pieces Assignment My Tags (Edit) Source : zhouguyue Time limit : 1 sec Memory limit : 64 M S ...
- POJ 3254 Corn Fields(状态压缩DP)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4739 Accepted: 2506 Descr ...
- [知识点]状态压缩DP
// 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...
- HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP
题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...
- 状态压缩dp问题
问题:Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Ev ...
- BZOJ-1226 学校食堂Dining 状态压缩DP
1226: [SDOI2009]学校食堂Dining Time Limit: 10 Sec Memory Limit: 259 MB Submit: 588 Solved: 360 [Submit][ ...
- Marriage Ceremonies(状态压缩dp)
Marriage Ceremonies Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
随机推荐
- 《算法》C++代码 快速排序
快速排序,简称快排,常称QuickSort.QSort.在排序算法中非常常用,其编程复杂度低,时间复杂度O(NlogN),空间复杂度O(N),执行效率稳定,而且常数很低. 基本思想就是二分,例如你要将 ...
- 【Pascal's Triangle】cpp
题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,R ...
- chromedriver版本支持的Chrome版本
下载chromedriver,链接:http://chromedriver.storage.googleapis.com/index.html chromedirver版本 支持的Chrome版本 ...
- C# Socket通信的服务器与客户端
客户端代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...
- MVC3使用Area解耦项目
源代码 1.增加AreasChildRegistration类,类继承PortableAreaRegistration 2.增加引用MvcContrib 3.主项目中Area文件夹下增加Web.con ...
- shell中的>&1和 >&2是什么意思?
当初在shell中, 看到">&1"和">&2"始终不明白什么意思.经过在网上的搜索得以解惑.其实这是两种输出. 在 shell 程 ...
- 【转】在Unity中读写文件数据:LitJSON快速教程
作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 介绍 JSON是一个简单的,但功能强大的序列 ...
- C#中不用安装Oracle客户端连接Oracle数据库(转)
原文地址:http://www.cnblogs.com/jiangguang/archive/2013/02/19/2916882.html 0.首先,从Oracle网站上下载对应版本的Oracle ...
- 静态编译zsummerX
下载 https://github.com/zsummer/zsummerX 下载 http://ftp.gnu.org/gnu/glibc/ ../configure --prefix=/home/ ...
- BZOJ1855 [Scoi2010]股票交易 【单调队列优化dp】
题目链接 BZOJ1855 题解 设\(f[i][j]\)表示第\(i\)天结束时拥有\(j\)张股票时的最大收益 若\(i \le W\),显然在这之前不可能有交易 \[f[i][j] = max\ ...