Hie with the Pie
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 6436   Accepted: 3470

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 toj 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

Source

 
题意:有N个城市(1~N)和一个PIZZA店(0),要求一条回路,从0出发,又回到0,而且距离最短。
 求距离之和。
 
题解:n的范围很小,先floyd 求出每两点之间的最短距离
    一遍dfs求 能够构成回路的距离最小值
 
简单dfs处理
 /******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#define ll long long
#define mod 1000000007
#define PI acos(-1.0)
using namespace std;
int n;
ll mp[][];
int used[];
ll ans=mod;
void floyd()
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
for(int k=;k<=n;k++)
{
mp[j][k]=min(mp[j][k],mp[j][i]+mp[i][k]);
}
}
}
}
void dfs(int dep,int v,ll c)
{
if(dep==n)
{
ans=min(ans,c+mp[v][]);
return ;
}
for(int i=;i<=n;i++)
{
if(!used[i])
{
used[i]=;
dfs(dep+,i,c+mp[v][i]);
used[i]=;
}
}
}
int main()
{
while((scanf("%d",&n))!=EOF){
ans=mod;
if(n==) break;
memset(mp,,sizeof(mp));
memset(used,,sizeof(used));
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%I64d",&mp[i][j]);
used[]=;
floyd();
dfs(,,);
printf("%I64d\n",ans);
}
return ;
}
状态压缩dp处理   也是第一次接触状态压缩 算是入门了 参考别人的代码 码的。
dfs处理 会有很多重复的运算 考虑dp处理其实就是类似poj滑雪的记忆化
dp[i][j]  代表 状态i下到j城市的最短距离   ans=dp[i][j]+mp[j][0]//回到0点
这里重要处理的就是怎么存储一个状态 共有2^10种
采用状态压缩 对于城市的去或者不去 采用0/1标记 
1~2^10 每一个值代表一个状态  具体看fun()
 /******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#define ll long long
#define mod 1000000007
#define PI acos(-1.0)
using namespace std;
int n;
ll mp[][];
ll dp[<<][];
int used[];
ll ans=mod;
void floyd()
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
for(int k=;k<=n;k++)
{
mp[j][k]=min(mp[j][k],mp[j][i]+mp[i][k]);
}
}
}
}
void fun()
{
for(int s=;s<=(<<n)-;s++)
for(int i=;i<=n;i++)
{
if(s&(<<(i-)))//判断是否经过城市i
{
if(s==(<<(i-)))//只经过i
dp[s][i]=mp[][i];
else
{
dp[s][i]=mod;
for(int j=;j<=n;j++)
{
if((s&(<<(j-)))&&j!=i)//判断绕行其他城市是否更优,取最优
dp[s][i]=min(dp[s^(<<(i-))][j]+mp[j][i],dp[s][i]);
}//类似floyd的处理
}
}
}
ans=dp[(<<n)-][]+mp[][];//回到0点
for(int i=;i<=n;i++)
ans=min(ans,dp[(<<n)-][i]+mp[i][]);
printf("%I64d\n",ans);
}
int main()
{
while((scanf("%d",&n))!=EOF){
ans=mod;
if(n==) break;
memset(mp,,sizeof(mp));
memset(used,,sizeof(used));
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%I64d",&mp[i][j]);
floyd();
fun();
}
return ;
}

poj 3311 floyd+dfs或状态压缩dp 两种方法的更多相关文章

  1. POJ 1691 Painting a Board(状态压缩DP)

    Description The CE digital company has built an Automatic Painting Machine (APM) to paint a flat boa ...

  2. POJ 2686_Traveling by Stagecoach【状态压缩DP】

    题意: 一共有m个城市,城市之间有双向路连接,一个人有n张马车票,一张马车票只能走一条路,走一条路的时间为这条路的长度除以使用的马车票上规定的马车数,问这个人从a出发到b最少使用时间. 分析: 状态压 ...

  3. poj 2411 Mondriaan's Dream_状态压缩dp

    题意:给我们1*2的骨牌,问我们一个n*m的棋盘有多少种放满的方案. 思路: 状态压缩不懂看,http://blog.csdn.net/neng18/article/details/18425765 ...

  4. POJ 1038 Bug Integrated Inc(状态压缩DP)

    Description Bugs Integrated, Inc. is a major manufacturer of advanced memory chips. They are launchi ...

  5. poj 1185 炮兵阵地 [经典状态压缩DP]

    题意:略. 思路:由于每个大炮射程为2,所以如果对每一行状态压缩的话,能对它造成影响的就是上面的两行. 这里用dp[row][state1][state2]表示第row行状态为state2,第row- ...

  6. poj 2411 Mondriaan's Dream(状态压缩dP)

    题目:http://poj.org/problem?id=2411 Input The input contains several test cases. Each test case is mad ...

  7. poj 2686 Traveling by Stagecoach ---状态压缩DP

    题意:给出一个简单带权无向图和起止点,以及若干张马车车票,每张车票可以雇到相应数量的马. 点 u, v 间有边时,从 u 到 v 或从 v 到 u 必须用且仅用一张车票,花费的时间为 w(u, v) ...

  8. POJ 1185 炮兵阵地 (状态压缩DP)

    题目链接 Description 司令部的将军们打算在NM的网格地图上部署他们的炮兵部队.一个NM的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示),也可能是平原(用& ...

  9. POJ 1185 炮兵阵地(状态压缩DP)

    题解:nState为状态数,state数组为可能的状态 代码: #include <map> #include <set> #include <list> #inc ...

随机推荐

  1. aop设计原理

    本文摘自 博文--<Spring设计思想>AOP设计基本原理 0.前言 Spring 提供了AOP(Aspect Oriented Programming) 的支持, 那么,什么是AOP呢 ...

  2. IE6兼容png图片

    <!--[if IE 6]> <script src="/js/DD_belatedPNG.js"></script> <script&g ...

  3. spring-mvc.xml的定时器配置

    <!-- 设置时间 --> <bean id="myJobTrigger" class="org.springframework.scheduling. ...

  4. ajax原生js及readystate/status

    菜鸟教程 ←← GET: <script> function  ajaxGet(){ var  xmlhttp; if(window.XMLHttpRequest){ //TE7+  Fi ...

  5. Oracle 11g R2在 win7 64位的安装流程图解【含常见问题解决方案】

    ORACLE数据库系统是美国ORACLE公司(甲骨文)提供的以分布式数据库为核心的一组软件产品,是目前最流行的客户/服务器(CLIENT/SERVER)或B/S体系结构的数据库之一.Oracle 11 ...

  6. 一件安装lnmp

    wget -c http://soft.vpser.net/lnmp/lnmp1.2-full.tar.gz && tar zxf lnmp1.2-full.tar.gz && ...

  7. Uva12230Crossing Rivers 数学

    Uva12230Crossing Rivers 问题: You live in a village but work in another village. You decided to follow ...

  8. 神经网络的训练和测试 python

    承接上一节,神经网络需要训练,那么训练集来自哪?测试的数据又来自哪? <python神经网络编程>一书给出了训练集,识别图片中的数字.测试集的链接如下: https://raw.githu ...

  9. Java线程和多线程(六)——守护线程

    当我们在Java中创建线程的时候,这个线程在默认的情况下是一个用户线程,并且,如果这个线程在运行,那么JVM就不会终结这个应用.和用户线程不同,当一个线程被标记为守护线程的时候,JVM在用户线程结束的 ...

  10. AD9 设置网络标号作用域

    http://blog.sina.com.cn/s/blog_99c8ec600102uxul.html 1.版本:Altium Designer 10 2.原因:在进行多原理图设计时, 不同原理图之 ...