poj 3311 状压dp 最短路
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
System Crawler (2014-05-15)
Description
The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutbacks, they can afford to hire only one driver to do the deliveries. He will wait for 1 or more (up to 10) orders to be processed before he starts any deliveries. Needless to say, he would like to take the shortest route in delivering these goodies and returning to the pizzeria, even if it means passing the same location(s) or the pizzeria more than once on the way. He has commissioned you to write a program to help him.
Input
Input will consist of multiple test cases. The first line will contain a single integer n indicating the number of orders to deliver, where 1 ≤ n ≤ 10. After this will be n + 1 lines each containing n + 1 integers indicating the times to travel between the pizzeria (numbered 0) and the n locations (numbers 1 to n). The jth value on the ith line indicates the time to go directly from location i to location j without visiting any other locations along the way. Note that there may be quicker ways to go from i to j via other locations, due to different speed limits, traffic lights, etc. Also, the time values may not be symmetric, i.e., the time to go directly from location i to j may not be the same as the time to go directly from location j to i. An input value of n = 0 will terminate input.
Output
For each test case, you should output a single number indicating the minimum time to deliver all of the pizzas and return to the pizzeria.
Sample Input
3
0 1 10 10
1 0 1 2
10 1 0 10
10 2 10 0
0
Sample Output
8
题意转自:http://blog.csdn.net/xymscau/article/details/6709588
题意:一个送披萨的,每次送外卖不超过10个地方,给你这些地方之间的时间,求送完外卖回到店里的总时间最小。
最近有点sb,经常犯sb的错误,今天又是,蛋疼啊。这题很明显的dp,dp[i][j]:表示在i状态(用二进制表示城市有没有经过)时最后到达j城市的最小时间,转移方程:dp[i][j]=min(dp[i][k]+d[k][j],dp[i][j]) d[k][j]是k城市到j城市的最短距离,显然要先用flody处理一下。
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<vector> #define N 105
#define M 100000
#define inf 1000000007
#define mod 1000000007
#define mod2 100000000
#define ll long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n;
int a[N][N];
int d[N][N];
int dp[ (<<)+ ][N];
int mi; void flody()
{
int i,j,k;
for(k=;k<n+;k++){
for(i=;i<n+;i++){
for(j=;j<n+;j++){
d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
}
}
}
} void ini()
{
int i,j;
// m=n+1;
mi=inf;
memset(dp,-,sizeof(dp));
for(i=;i<=n;i++){
for(j=;j<=n;j++){
scanf("%d",&a[i][j]);
d[i][j]=a[i][j];
}
}
flody();
} void solve()
{
int o,j,p,te;
dp[][]=;
for(o=;o<(<<n);o++){
for(j=;j<n;j++){
if( ( (<<j) & o)== ) continue;
if( (<<j)==o ){
dp[o][j+]=d[][j+];continue;
}
dp[o][j+]=inf;
te=o ^ (<<j);
for(p=;p<n;p++){
if( ( (<<p) & te)== ) continue;
dp[o][j+]=min(dp[o][j+],dp[te][p+]+d[p+][j+]);
}
}
} for(j=;j<n;j++){
mi=min(mi,dp[ (<<n)- ][j+]+d[j+][]);
} //for(o=0;o<(1<<n);o++){
// for(j=1;j<=n;j++) printf(" o=%d j=%d dp=%d\n",o,j,dp[o][j]);
//} } int main()
{
// freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
//scanf("%d",&T);
// for(int cnt=1;cnt<=T;cnt++)
// while(T--)
while(scanf("%d",&n)!=EOF)
{
if(n==) break;
ini();
solve();
printf("%d\n",mi);
}
return ;
}
poj 3311 状压dp 最短路的更多相关文章
- Hie with the Pie(POJ 3311状压dp)
		题意:披萨店给n个地方送披萨,已知各地方(包括披萨店)之间花费的时间,求送完所有地方并回到店花费的最小时间 分析:状态好确定dp[i][j],i中1表示地方已送过,否则为0,j为当前状态最后一个送过的 ... 
- poj 3311  状压DP
		经典TSP变形 学到:1.floyd O(n^3)处理随意两点的最短路 2.集合的位表示,我会在最后的总结出写出.注意写代码之前一定设计好位的状态.本题中,第0位到第n位分别代表第i个城市,1是已经 ... 
- 旅游(CSUST省赛选拔赛2+状压dp+最短路)
		题目链接:http://csustacm.com:4803/problem/1016 题目: 思路:状压dp+最短路,比赛的时候有想到状压dp,但是最短路部分写挫了,然后就卡死了,对不起出题人~dis ... 
- POJ 3254 (状压DP) Corn Fields
		基础的状压DP,因为是将状态压缩到一个整数中,所以会涉及到很多比较巧妙的位运算. 我们可以先把输入中每行的01压缩成一个整数. 判断一个状态是否有相邻1: 如果 x & (x << ... 
- poj 1170状压dp
		题目链接:https://vjudge.net/problem/POJ-1170 题意:输入n,表示有那种物品,接下来n行,每行a,b,c三个变量,a表示物品种类,b是物品数量,c代表物品的单价.接下 ... 
- Codeforces 375C - Circling Round Treasures(状压 dp+最短路转移)
		题面传送门 注意到这题中宝藏 \(+\) 炸弹个数最多只有 \(8\) 个,故考虑状压,设 \(dp[x][y][S]\) 表示当前坐标为 \((x,y)\),有且仅有 \(S\) 当中的物品被包围在 ... 
- POJ 3254 状压DP
		题目大意: 一个农民有一片n行m列 的农场 n和m 范围[1,12] 对于每一块土地 ,1代表可以种地,0代表不能种. 因为农夫要种草喂牛,牛吃草不能挨着,所以农夫种菜的每一块都不能有公共边. ... 
- POJ 2411 状压DP经典
		Mondriaan's Dream Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 16771 Accepted: 968 ... 
- poj 3254   状压dp入门题
		1.poj 3254 Corn Fields 状态压缩dp入门题 2.总结:二进制实在巧妙,以前从来没想过可以这样用. 题意:n行m列,1表示肥沃,0表示贫瘠,把牛放在肥沃处,要求所有牛不能相 ... 
随机推荐
- (五)使用Docker镜像(上)
			1. 获取镜像 # 获取镜像 docker pull image:tag // 不使用tag 默认下载latest标签的镜像,即最新的镜像. 2. 查看镜像信息 # 查看镜像信息docker imag ... 
- JavaScript --  语法和数据类型
			前戏 前面学了HTML和CSS相关的知识,那JavaScript是做什么的呢?你在网页上看到的那些炫酷的特效都是通过JS来实现的,所以,想要开发一个逼格满满的web页面,JS是必须要会的 什么是Jav ... 
- Mysql插入中文时提示:ERROR 1366 (HY000): Incorrect string value: '\xE5\x8F\xB0\xE5\xBC\x8F...' fo
			Mysql插入数据时提示:ERROR 1366 (HY000): Incorrect string value: ‘\xE5\x8F\xB0\xE5\xBC\x8F…’ fo 分析如下: 首先通过语句 ... 
- 经典的7种排序算法 原理C++实现
			排序是编程过程中经常遇到的操作,它在很大程度上影响了程序的执行效率. 7种常见的排序算法大致可以分为两类:第一类是低级排序算法,有选择排序.冒泡排序.插入排序:第二类是高级排序算法,有堆排序.排序树. ... 
- 一次下载多个文件的解决思路-JS
			一次下载多个文件的解决思路(iframe) - Eric 真实经历 最近开发项目需要做文件下载,想想挺简单的,之前也做过,后台提供下载接口,前端使用window.location.href就行了呗.不 ... 
- js常见面试题
			1.大小写转化,将字符串转化成驼峰的方法 例:border-bottom-color转化为:borderBottomColor var str="border-bottom-color&qu ... 
- 爬虫练习四:爬取b站番剧字幕
			由于个人经常在空闲时间在b站看些小视频欢乐一下,这次就想到了爬取b站视频的弹幕. 这里就以番剧<我的妹妹不可能那么可爱>第一季为例,抓取这一番剧每一话对应的弹幕. 1. 分析页面 这部番剧 ... 
- S3C6410串口平台设备注册流程分析
			1.mdesc->map_io() start_kernel -->setup_arch(&command_line); -->paging_init(mdesc); --& ... 
- java  excutors 四种类型的线程
			http://blog.csdn.net/ochangwen/article/details/53044733 
- Java-计算程序运行时间
			package com.tj; @SuppressWarnings("unused") public class CountTime { public static void ma ... 
