POJ 3311
设dp状态为dp[i][j]为当前访问过的结点状态为i且当前停留点为j时的最短路径。用二进制存存储访问过的状态,访问过为1,否则为0。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
const int inf=(1<<30);
int map[12][12];
int dp[1<<11][12],n; struct Status{
int i,j,v;
Status(){}
Status(int ii,int jj,int vv){i=ii,j=jj,v=vv;}
}que[(1<<11)*12];
int head,tail; void slove(){
while(head<tail){
Status tmp=que[head++];
if(tmp.v>dp[tmp.i][tmp.j]) continue;
for(int j=0;j<=n;j++){
int st=(1<<j);
int tst=tmp.i|st;
if(dp[tst][j]>tmp.v+map[tmp.j][j]){
dp[tst][j]=tmp.v+map[tmp.j][j];
que[tail++]=Status(tst,j,dp[tst][j]);
}
}
}
printf("%d\n",dp[(1<<n+1)-1][0]);
} int main(){
while(scanf("%d",&n),n){
for(int i=0;i<=n;i++){
for(int j=0;j<=n;j++)
scanf("%d",&map[i][j]);
}
for(int i=0;i<(1<<(n+1));i++){
for(int j=0;j<=n;j++)
dp[i][j]=inf;
}
dp[1][0]=0;
head=tail=0;
que[tail++]=Status(1,0,0);
slove();
}
return 0;
}
POJ 3311的更多相关文章
- poj 3311(状态压缩DP)
poj 3311(状态压缩DP) 题意:一个人送披萨从原点出发,每次不超过10个地方,每个地方可以重复走,给出这些地方之间的时间,求送完披萨回到原点的最小时间. 解析:类似TSP问题,但是每个点可以 ...
- Hie with the Pie POJ - 3311
Hie with the Pie POJ - 3311 The Pizazz Pizzeria prides itself in delivering pizzas to its customers ...
- Hie with the Pie (POJ 3311) 旅行商问题
昨天想练习一下状态压缩,百度搜索看到有博客讨论POJ 3311,一看就是简单的旅行商问题,于是快速上手写了状态压缩,死活样例都没过... 画图模拟一遍原来多个城市可以重复走,然后就放弃思考了... 刚 ...
- poj 3311(floyd+状态压缩)
题目链接:http://poj.org/problem?id=3311 思路:Floyd + 状态压缩DP 题意是有N个城市(1~N)和一个PIZZA店(0),要求一条回路,从0出发,又回到0,而且 ...
- poj 3311 Hie with the Pie
floyd,旅游问题每个点都要到,可重复,最后回来,dp http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS Me ...
- POJ 3311 Hie with the Pie(DP状态压缩+最短路径)
题目链接:http://poj.org/problem?id=3311 题目大意:一个送披萨的,每次送外卖不超过10个地方,给你这些地方之间的时间,求送完外卖回到店里的总时间最小. Sample In ...
- POJ 3311 Hie with the Pie(状压DP + Floyd)
题目链接:http://poj.org/problem?id=3311 Description The Pizazz Pizzeria prides itself in delivering pizz ...
- [POJ 3311]Hie with the Pie——谈论TSP难题DP解决方法
主题连接: id=3311">http://poj.org/problem?id=3311 题目大意:有n+1个点,给出点0~n的每两个点之间的距离,求这个图上TSP问题的最小解 ...
- POJ 3311 Hie with the Pie floyd+状压DP
链接:http://poj.org/problem?id=3311 题意:有N个地点和一个出发点(N<=10),给出全部地点两两之间的距离,问从出发点出发,走遍全部地点再回到出发点的最短距离是多 ...
- POJ 3311 Hie with the Pie:TSP(旅行商)【节点可多次经过】
题目链接:http://poj.org/problem?id=3311 题意: 你在0号点(pizza店),要往1到n号节点送pizza. 每个节点可以重复经过. 给你一个(n+1)*(n+1)的邻接 ...
随机推荐
- servlet范围:数据共享
数据共享: 请求转发:request.getDispatcher("相对路径").forward(request,response) 重定向:response.sendRedire ...
- 322 Coin Change 零钱兑换
给定不同面额的硬币(coins)和一个总金额(amount).写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合方式能组成总金额,返回-1.示例 1:coins = [1, ...
- Java&Xml教程(一)简介
XML是广泛用于数据传输和存储的技术.Java语言提供个各种各样的API来解析XML,例如DOM.SAX.StAX.JAXB.也还有一些其他的API用于解析XML,例如JDOM.本教程的目的是探索使用 ...
- Count the consecutive zero bits (trailing) on the right with multiply and lookup
我在网上看到了一点神奇的代码,用来计算一个数字末尾连续零的个数. 刚好我在优化一个I2C读写函数(只写入I2C特定bit),觉得这个很有用.经过尝试,确实没问题. 下面我隆重介绍一下: Count t ...
- Less——less基本使用
基本概况 Less 是一门 CSS 预处理语言,它扩充了 CSS 语言,增加了诸如变量.混合(mixin).函数等功能,让 CSS 更易维护.方便制作主题.扩充.Less 可以运行在 Node.浏览器 ...
- SQL基本操作——表的创建
通过代码方式创建数据库 create database MyDatabaseNew on primary ( --名字 name='MyDatabaseNew_data', --路径 filename ...
- dotnetnuke 添加用户属性 Profile
if (DotNetNuke.Entities.Profile.ProfileController.GetPropertyDefinitionByName(this.PortalId, "Q ...
- js 闭包 定时器
; !function (win) { ; //内部私有 , ; //内部私有 //test.prototype.tt1 = 0;//共有变量 var test = function () {}; t ...
- python numpy array 与matrix 乘方
python numpy array 与matrix 乘方 编程语言 waitig 1年前 (2017-04-18) 1272℃ 百度已收录 0评论 数组array 的乘方(**为乘方运算符)是每个元 ...
- zoj 3314 CAPTCHA(纯模拟)
题目 有些人用深搜写的,当然我这弱弱的,只理解纯模拟... 纯模拟,第一次写了那么长的代码,我自己也是够坚韧不拔的,,,,必须留念啊!!! 注意,G包含C,E包含L,R包含P,(照图说O应该不包含C, ...