HDU - 5067 / HDU - 5418 TSP
集合表示多用[0,n)表示方法
HDU - 5067
经典TSP,每个顶点恰经过一次最优
#include<bits/stdc++.h>
#define rep(i,j,k) for(int i=j;i<=k;i++)
using namespace std;
typedef pair<int,int> P;
int dp[1<<12][20];
int G[56][78],r,c;
P biao[23];
inline int dis(P a,P b){
return abs(a.first-b.first)+abs(a.second-b.second);
}
int DP(int S,int v,int cnt){
if(dp[S][v]>=0) return dp[S][v];
if(S==(1<<cnt)-1&&v==0) return dp[S][v]=0;
int ans=0x3f3f3f3f;
rep(u,0,cnt-1){
if(!(S>>u&1)){
ans=min(ans,DP(S|(1<<u),u,cnt)+dis(biao[u],biao[v]));
}
}
return dp[S][v]=ans;
}
int main(){
while(scanf("%d%d",&r,&c)!=EOF){
int cnt=0;
rep(i,0,r-1)rep(j,0,c-1)scanf("%d",&G[i][j]);
rep(i,0,r-1)rep(j,0,c-1){
if(G[i][j]||(i==0&&j==0)){
biao[cnt++]=P(i,j);
}
}
memset(dp,-1,sizeof dp);
printf("%d\n",DP(0,0,cnt));
}
return 0;
}
HDU - 5418
多次遍历TSP
用floyd使它无后效性
然后同上
#include<bits/stdc++.h>
#define rep(i,j,k) for(int i=j;i<=k;i++)
using namespace std;
int dp[1<<18][17];
int G[18][18],n,m,u,v,w;
int DP(int S,int u,int n){
if(dp[S][u]>=0) return dp[S][u];
if(S==(1<<n)-1&&u==0) return dp[S][u]=0;
int ans=0x3f3f3f3f;
rep(v,0,n-1){
if(!(S>>v&1)){
ans=min(ans,DP(S|(1<<v),v,n)+G[u][v]);
}
}
return dp[S][u]=ans;
}
int main(){
int T; scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
memset(G,0x3f,sizeof G);
memset(dp,-1,sizeof dp);
rep(i,1,m){
scanf("%d%d%d",&u,&v,&w);
u--; v--;
G[u][v]=min(G[u][v],w);
G[v][u]=min(G[v][u],w);
}
rep(i,0,n-1) G[i][i]=0;
rep(i,0,n-1) rep(j,0,n-1)if(i!=j){
rep(k,0,n-1)
G[i][j]=min(G[i][j],G[i][k]+G[k][j]);
}
printf("%d\n",DP(0,0,n));
}
return 0;
}
HDU - 5067 / HDU - 5418 TSP的更多相关文章
- HDU 5067 Harry And Dig Machine(状压DP)(TSP问题)
题目地址:pid=5067">HDU 5067 经典的TSP旅行商问题模型. 状压DP. 先分别预处理出来每两个石子堆的距离.然后将题目转化成10个城市每一个城市至少经过一次的最短时间 ...
- HDU 5067 Harry And Dig Machine(状压dp)
HDU 5067 Harry And Dig Machine 思路:因为点才10个,在加上一个起点,处理出每一个点之间的曼哈顿距离,然后用状压dp搞,状态表示为: dp[i][s],表示在i位置.走过 ...
- HDU - 2222,HDU - 2896,HDU - 3065,ZOJ - 3430 AC自动机求文本串和模式串信息(模板题)
最近正在学AC自动机,按照惯例需要刷一套kuangbin的AC自动机专题巩固 在网上看过很多模板,感觉kuangbin大神的模板最为简洁,于是就选择了用kuangbin大神的模板. AC自动机其实就是 ...
- HDU 5067 (状态压缩DP+TSP)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5067 题目大意:蓝翔挖掘机挖石子.把地图上所有石子都运回起点,问最少耗时. 解题思路: 首先得YY出 ...
- HDU 5067 Harry And Dig Machine:TSP(旅行商)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5067 题意: 给你一个n*m的地图,地图上标着对应位置的石子数.你从左上角出发,每次可以向上下左右四个 ...
- HDU 5067
http://acm.hdu.edu.cn/showproblem.php?pid=5067 规定起点和终点的tsp问题,解法依然是状态压缩dp,在初始化和计算答案的时候略做改动即可 #include ...
- BestCoder14 1002.Harry And Dig Machine(hdu 5067) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5067 题目意思:给出一个 n * m 的方格,每一个小方格(大小为1*1)的值要么为 0 要么为一个正 ...
- hdu 5067 Harry And Dig Machine
http://acm.hdu.edu.cn/showproblem.php?pid=5067 思路:问题可以转化成:从某一点出发,遍历网格上的一些点,每个点至少访问一次需要的最小时间是多少.这就是经典 ...
- HDU 3001 Travelling:TSP(旅行商)【节点最多经过2次】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 题意: 有n个城市,m条双向道路,每条道路走一次需要花费路费v.你可以将任意一个城市作为起点出发 ...
随机推荐
- Docker学习笔记_创建和使用Centos容器
实验:创建和使用Centos容器 步骤: 1.搜索 sudo docker search cen ...
- ROS indigo Ubuntu14.04 安装问题
错误信息:Unpacking ros-indigo-desktop-full (1.1.6-0trusty-20181006-135515-0800) ... Errors were encounte ...
- jQuery--修改表单数据并提交
目的: 点击'编辑',弹出对话框,修改数据. 主要知识点: prevAll(),获取同级别本元素前面的所有元素. 代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...
- 235D Graph Game
传送门 题目大意 https://www.luogu.org/problemnew/show/CF235D 分析 我们先考虑它是树的情况 我们设$event(x,y)$表示删除点x是y与x联通这件事对 ...
- hadoop2.2分布式环境搭建
hadoop2.2的分布式环境需要配置的参数更多.但是需要安装的系统软件和单节点环境是一样的. 运行hadoop在非安全环境 hadoop的配置文件有两类: 1:只读的默认配置文件: core-def ...
- Java 并行和并发
并行:指两个或多个事件在同一时刻点进行. 并发:指两个或多个事件在同一时间段进行.
- POJ - 2586 Y2K Accounting Bug (找规律)
Accounting for Computer Machinists (ACM) has sufferred from the Y2K bug and lost some vital data for ...
- Jquery Call ,apply,callee
//call function A() { name = "abc"; this.ShowName = function (val) { alert(name + ",& ...
- WebApi跨域问题解决
因为第一次用webapi,并且还是前后台分离,所以设置到了跨域,在百度上找了很多解决办法,但是基本都存在缺陷,我这里分享一下我自己的经验 1.首先配置Web.config 这样配置发布到服务器就可以跨 ...
- [.net 多线程]Monitor
Monitor 类通过向单个线程授予对象锁来控制对对象的访问.对象锁提供限制访问代码块(通常称为临界区)的能力.当一个线程拥有对象的锁时,其他任何线程都不能获取该锁.还可以使用 Monitor 来确保 ...