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.你可以将任意一个城市作为起点出发 ...
随机推荐
- Ubuntu 源使用帮助
地址 https://mirrors.ustc.edu.cn/ubuntu/ 说明 Ubuntu 软件源 收录架构 AMD64 (x86_64), Intel x86 其他架构请参考 Ubuntu P ...
- 复习action委托
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- SQL 批量插入有标识列的数据
代码: SET IDENTITY_INSERT 表名 ON SET IDENTITY_INSERT 表名 OFF
- MongoDB整理笔记の安全访问
MongoDB安全访问将从以下三个方面得到控制! 1.绑定IP内网地址访问MongoDB服务 2.设置监听端口 3.使用用户名和密码 绑定IP内网地址访问MongoDB服务 Mon ...
- Oracle 触发器 删除操作时再查询本表数据 功能不正确
背影如下: 表名,WFGTEST create table WFGTEST ( NAME1 ) not null, NAME2 ), CAPACITY ,) ) 表结构如下: NAME1 NAME2 ...
- winform treeview绑定数据 DOM操作
form1 public void treeView() { // datatable 定义变量接收 传归来的值 DataTable Father = new BuMenDA().ConSql(); ...
- WP REST API: 设置和使用OAuth 1.0a Authentication(原文)
In the previous part of the series, we set up basic HTTP authentication on the server by installing ...
- angular 工厂模式依赖注入
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; ...
- 《javascript 高级程序设计》 笔记1 1~7章
chapter 2 在html中使用JavaScript chapter 3 基本概念 EMCAscript 语法 变量,函数名和操作符都区分大小写. 使用var定义的变量将成为定义该变量的作用域中的 ...
- 【AGC013D】Pilling Up dp
Description 红蓝球各无限多个. 初始时随意地从中选择 n 个, 扔入箱子 初始有一个空的序列 接下来依次做 m 组操作, 每组操作为依次执行下述三个步骤 (1) 从箱子中取出一个求插入序列 ...