poj 3311 floyd+dfs或状态压缩dp 两种方法
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
/******************************
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 ;
}
/******************************
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 两种方法的更多相关文章
- POJ 1691 Painting a Board(状态压缩DP)
Description The CE digital company has built an Automatic Painting Machine (APM) to paint a flat boa ...
- POJ 2686_Traveling by Stagecoach【状态压缩DP】
题意: 一共有m个城市,城市之间有双向路连接,一个人有n张马车票,一张马车票只能走一条路,走一条路的时间为这条路的长度除以使用的马车票上规定的马车数,问这个人从a出发到b最少使用时间. 分析: 状态压 ...
- poj 2411 Mondriaan's Dream_状态压缩dp
题意:给我们1*2的骨牌,问我们一个n*m的棋盘有多少种放满的方案. 思路: 状态压缩不懂看,http://blog.csdn.net/neng18/article/details/18425765 ...
- POJ 1038 Bug Integrated Inc(状态压缩DP)
Description Bugs Integrated, Inc. is a major manufacturer of advanced memory chips. They are launchi ...
- poj 1185 炮兵阵地 [经典状态压缩DP]
题意:略. 思路:由于每个大炮射程为2,所以如果对每一行状态压缩的话,能对它造成影响的就是上面的两行. 这里用dp[row][state1][state2]表示第row行状态为state2,第row- ...
- poj 2411 Mondriaan's Dream(状态压缩dP)
题目:http://poj.org/problem?id=2411 Input The input contains several test cases. Each test case is mad ...
- poj 2686 Traveling by Stagecoach ---状态压缩DP
题意:给出一个简单带权无向图和起止点,以及若干张马车车票,每张车票可以雇到相应数量的马. 点 u, v 间有边时,从 u 到 v 或从 v 到 u 必须用且仅用一张车票,花费的时间为 w(u, v) ...
- POJ 1185 炮兵阵地 (状态压缩DP)
题目链接 Description 司令部的将军们打算在NM的网格地图上部署他们的炮兵部队.一个NM的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示),也可能是平原(用& ...
- POJ 1185 炮兵阵地(状态压缩DP)
题解:nState为状态数,state数组为可能的状态 代码: #include <map> #include <set> #include <list> #inc ...
随机推荐
- linux环境下nginx配置
1.反向代理配置 # nginx/conf/nginx.conf
- Docker学习之镜像操作
使用Docker镜像 以下都是Docker镜像的一系列重要名操作,包括获取.查看.搜索.删除.创建.存出或载入.上传等.可使用docker image help命令查看帮助. 1.获取镜像(pull) ...
- 文件权限管理命令chmod,chown与文本搜索命令grep
1.复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限. [root@bogon home]# cp -r /etc/sk ...
- Element-ui组件--pagination分页
一般写后台系统都会有很多的列表,有列表就相应的要用到分页,根据项目中写的几个分页写一下我对分页的理解,就当是学习笔记了. 这是Element-ui提供的完整的例子 <template> ...
- MySQL字段属性介绍
引言 这次Qi号分享MySQL字段属性简介.下面资料是Qi号搜集大量资料与个人理解的整理. MySQL提供了一组可以赋给表中各个列的数据类型,每个类型都强制数据满足为该数据类型预先确定的一组规则,例如 ...
- php扩展开发-MINFO
我们在用PHPinfo函数或命令行的php -i命令查看php环境相关的信息,当我们开发完成一个自己的扩展,除非这个扩展就是你自己所使用,否则你就需要对扩展进行相关的介绍,或者显示扩展用到的ini配置 ...
- QWidget 自带的最大化,最小化,关闭按键的设置
使用函数 setWindowFlags 参数: CustomizeWindowHint 去掉窗口所有自带按钮 Qt::CustomizeWindowHint | Qt::WindowCloseButt ...
- 29-自己动手构建RequestDelegate管道
1-使用vsCode新建个项目 2-新建RequestDelegate和Context public delegate Task RequestDelegate(Context context); p ...
- Android面试收集录6 事件分发机制
转自:秋招面试宝典. 一. 基础认知 1.1 事件分发的对象是谁? 答:事件 当用户触摸屏幕时(View或ViewGroup派生的控件),将产生点击事件(Touch事件). Touch事件相关细节(发 ...
- 10 class封装 ORM
1.版本1:初始化 # -*- coding:utf-8 -*- from MySQLdb import * class MysqlHelper: def __init__(self,host,por ...