hdu3001(状态压缩dp)
来源:互联网 发布:影像医疗软件代理 编辑:程序博客网 时间:2024/05/21 07:50
题目链接:点击打开链接
题目:
题目:
After coding so many days,Mr Acmer wants to have a good rest.So travelling is the best choice!He has decided to visit n cities(he insists on seeing all the cities!And he does not mind which city being his start station because superman can bring him to any city at first but only once.), and of course there are m roads here,following a fee as usual.But Mr Acmer gets bored so easily that he doesn't want to visit a city more than twice!And he is so mean that he wants to minimize the total fee!He is lazy you see.So he turns to you for help.
分析:
每天路径最多访问两次,可以使用三进制状态压缩,d[S][j] 表示在状态s下最后一个访问j时的最小花费;
状态转移方程:d[S][k] = min(d[S][k], d[S-state[k]][j] + G[j][k]); 不过这里通过状态d[S][j] 转移到 ---> d[S+state[k]][k] = min(d[S+state[k]][k], d[S][j]+G[j][k]);
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>using namespace std;const int maxn = 11;int n, m;int G[60000][maxn], state[11], vis[60000][maxn];const int INF = 0x3f3f3f3f;int d[60000][maxn];void init(){ state[0] = 1; for(int i = 1; i <= 10; ++i){ state[i] = 3*state[i-1]; } for(int i = 0; i < state[10]; ++i){ int tmp = i; for(int j = 0; j < 10; ++j){ vis[i][j] = tmp%3; tmp /= 3; } }}int deal(){ memset(d, 0x3f, sizeof(d)); for(int i = 0; i < n; ++i) d[state[i]][i] = 0; for(int i = 1; i < state[n]; ++i){ for(int j = 0; j < n; ++j){ if(d[i][j] == INF) continue; for(int k = 0; k < n; ++k){ if(k==j) continue; if(vis[i][k] < 2 && G[j][k]!=INF){ d[i+state[k]][k] = min(d[i+state[k]][k], d[i][j]+G[j][k]); } } } } int ans = INF; for(int i = 1; i < state[n]; ++i){ int ok = 1; for(int j = 0; j < n; ++j){ if(vis[i][j] == 0){ ok = 0; break; } } if(ok){ for(int j = 0; j < n; ++j){ ans = min(ans, d[i][j]); } } } return ans;}int main(){ init(); while(~scanf("%d%d", &n, &m)){ int u, v, w; memset(G, 0x3f, sizeof(G)); for(int i = 0; i < m; ++i){ scanf("%d%d%d", &u, &v, &w); u--, v--; if(w < G[u][v]){ G[u][v] = G[v][u] = w; } } int ans = deal(); if(ans == INF) ans = -1; printf("%d\n", ans); }}
0 0
- hdu3001(状态压缩dp)
- hdu3001 Travelling(状态压缩dp,三进制)
- hdu3001(状态压缩dp,三进制!)
- hdu3001(三进制状态压缩DP)
- hdu3001 三进制状态压缩+dp
- hdu3001 状态压缩dp+三进制
- hdu3001(三进制状态压缩dp)
- hdu3001 三进制 状态压缩dp
- HDU3001——Travelling(状态压缩DP)
- 三进制状态压缩DP(旅行商问题TSP)HDU3001
- (状态压缩) Travelling (HDU3001)
- hdu3001 Traveling (三进制状态压缩dp)
- hdu3001 Travelling 三进制状态压缩dp
- hdu3001(3进制状态压缩dp)
- HDU3001(状态压缩TSP)
- HDU3001-Travelling-状态压缩
- 状态压缩DP总结【POJ3311】【HDU3001】【POJ2288】【ZOJ4257】【POJ2411】【HDU3681】
- hdu3001 Travelling 三进制状态压缩
- MySQL 常用函数 数学函数
- HDU 4565【二阶递推】
- Xshell秘钥登录Linux服务器: root && 普通用户
- Gallery
- MySQL5.6免安装版的安装教材教程
- hdu3001(状态压缩dp)
- SQLite3 API 学习
- h5设计稿尺寸详解
- CyberLink YouCam 新人求教一个绿色背景提取红色小球的问题
- C# 汉字转拼音(支持GB2312字符集中所有汉字)
- php字符串与字符替换函数
- 【Android】Android 中ListView setOnItemClickListener点击无效原因分析
- Maven多模块项目介绍和搭建
- JVM 系列: