Hie with the Pie
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 4671   Accepted: 2471

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

题意

从0出发送汉堡,送完每一个点后回到0点

矩阵 表示各个点间距离。

思路

由于能够各个点多次经过。所以先求下floyd 来更新点点之间最短距离。

把每一步。到各个点的状态和最后一步所在的位置还有所花的距离保存下来。

把最后到的各个地方,再加个回零点的距离。求个最小值。

dp[15][15]   一维表示已经走的步数,二维表示当前最后一步到的点。  map的x表示状态,y表示已经花费的时间。

#include<stdio.h>
#include<map>
#include<algorithm>
using namespace std;
map<int,int>::iterator it;
map<int,int> dp[15][15];//ceng 结尾
int mp[15][15];
void floyd(int n)
{
int i,j,k;
for(k=0;k<=n;k++)
{
for(i=0;i<=n;i++)
{
for(j=0;j<=n;j++)
{
mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);
}
}
}
}
int main()
{
int n,i,j,k,mn,x,y;
while(scanf("%d",&n),n)
{
for(i=0;i<=n;i++)
{
for(j=0;j<=n;j++)
{
scanf("%d",&mp[i][j]);
}
}
floyd(n);
/*for(i=0;i<=n;i++)
{
for(j=0;j<=n;j++)
{
printf("%d ",mp[i][j]);
}
puts("");
}*/ //clear
dp[0][0][1]=0;
for(i=1;i<=n;i++)//第几处了
{
for(k=0;k<=n;k++)//终点
{
dp[i][k].clear();
for(j=0;j<=n;j++)//起点
{
for(it=dp[i-1][j].begin();it!=dp[i-1][j].end();it++)//上一层的各个状态
{
x=it->first;
y=it->second;
if((x&(1<<k))==0)//还没走过
{
if(dp[i][k].count(x|(1<<k))==0)
dp[i][k][x|(1<<k)]=y+mp[j][k];
else
dp[i][k][x|(1<<k)]=min(dp[i][k][x|(1<<k)],y+mp[j][k]);
}
}
}
}
} i=n;//最后一层
mn=999999999;
for(j=1;j<=n;j++)
{
for(it=dp[i][j].begin();it!=dp[i][j].end();it++)//上一层的各个状态
{
mn=min(mn,it->second+mp[j][0]);
}
}
printf("%d\n",mn); }
return 0;
}
/*
3
0 1 10 10
1 0 1 2
10 1 0 10
10 2 10 0 3
0 5 6 7
5 0 8 9
6 8 0 10
7 9 10 0
*/

poj 3311 Hie with the Pie dp+状压的更多相关文章

  1. POJ 3311 Hie with the Pie (状压DP)

    题意: 每个点都可以走多次的TSP问题:有n个点(n<=11),从点1出发,经过其他所有点至少1次,并回到原点1,使得路程最短是多少? 思路: 同HDU 5418 VICTOR AND WORL ...

  2. POJ 3311 Hie with the Pie(状压DP + Floyd)

    题目链接:http://poj.org/problem?id=3311 Description The Pizazz Pizzeria prides itself in delivering pizz ...

  3. POJ 3311 Hie with the Pie floyd+状压DP

    链接:http://poj.org/problem?id=3311 题意:有N个地点和一个出发点(N<=10),给出全部地点两两之间的距离,问从出发点出发,走遍全部地点再回到出发点的最短距离是多 ...

  4. POJ 3311 Hie with the Pie 【状压DP】

    Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possi ...

  5. poj 3311 Hie with the Pie (状压dp) (Tsp问题)

    这道题就是Tsp问题,稍微加了些改变 注意以下问题 (1)每个点可以经过多次,这里就可以用弗洛伊德初始化最短距离 (2)在循环中集合可以用S表示更清晰一些 (3)第一维为状态,第二维为在哪个点,不要写 ...

  6. POJ3311 Hie with the Pie 【状压dp/TSP问题】

    题目链接:http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total ...

  7. poj 3311 Hie with the Pie

    floyd,旅游问题每个点都要到,可重复,最后回来,dp http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS   Me ...

  8. POJ 3311 Hie with the Pie 最短路+状压DP

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11243   Accepted: 5963 ...

  9. POJ 3311 Hie with the Pie (状压DP)

    dp[i][j][k] i代表此层用的状态序号 j上一层用的状态序号 k是层数&1(滚动数组) 标准流程 先预处理出所有合法数据存在status里 然后独立处理第一层 然后根据前一层的max推 ...

随机推荐

  1. PHP之 xampp 安装环境

    1.安装XAMPP  需要注意以下几点: (1):必须已管理员身份运行: (2):先点击安装Apache和mysql(如果apche端口被占用,先停止服务里面的apche服务) (3):别忘记切换PH ...

  2. Python基础篇:从0开始学python

    目录 数据类型 基本数据类型 整形Int的内置方法 字符串Str的内置方法 列表(待补充) 流程控制 分支结构if...else... for循环 循环控制 while循环 函数 函数的名称与格式 参 ...

  3. QTREEⅠ SPOJ

    树剖模板,注意把边化为点后要查到y的儿子. #include<bits/stdc++.h> using namespace std; ; int f[N],bel[N],w[N],id[N ...

  4. [BZOJ4887][TJOI2017]可乐(DP+矩阵快速幂)

    题目描述 加里敦星球的人们特别喜欢喝可乐.因而,他们的敌对星球研发出了一个可乐机器人,并且放在了加里敦星球的1号城市上.这个可乐机器人有三种行为: 停在原地,去下一个相邻的城市,自爆.它每一秒都会随机 ...

  5. 我是如何从一个xss到某个浏览器的远程命令执行

    0x01 前言:其实我是个小白平时就喜欢瞎搞,无意间碰到一个浏览器就想一探究竟,好了废话不多说开始!!! 0x02 可以看到我打开的新标签是怎么一个链接页面,既然是页面我是不可以XSS它呢? 于是我就 ...

  6. 2017-2018-1 JAVA实验站 冲刺 day01

    2017-2018-1 JAVA实验站 冲刺 day01 各个成员在 Alpha 阶段认领的任务 小组成员 分工 任务量 张韵琪 写博客.后期市场推广,营销.打杂.各职能的配合 齐力锋 提供宣传用图. ...

  7. 厄拉多塞筛法和普通方法求素数表(python实现)

    厄拉多赛筛法(sieve of Eratosthenes): 想要得到一个不大于N的数所有素数,可以先找到不超过根号N的所有素数,设2 = p1 < p2 < ......<pk ≤ ...

  8. keras实现mnist数据集手写数字识别

    一. Tensorflow环境的安装 这里我们只讲CPU版本,使用 Anaconda 进行安装 a.首先我们要安装 Anaconda 链接:https://pan.baidu.com/s/1AxdGi ...

  9. MariaDB 10 (MySQL DB) 多主复制并实现读写分离

    ----本文大纲 简介 资源配置 拓扑图 实现过程 ==================== 一.简介 MMM 即Master-Master Replication Manager for MySQL ...

  10. C# dataGridView根据数据调整列宽

    //自适应列宽 this.dgvBaoming.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.A ...