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 ...
随机推荐
- LVM(扩展)
LVM(扩展)======================== [root@aminglinux newdir]# fdisk -l /dev/sdb 磁盘 /dev/sdb:10.7 GB, 107 ...
- miniui IE对省略号即text-overflow:ellipsis显示不一样的问题
做miniui项目中发现,IE对文本以英文或数字结尾的是英文的省略号,以汉字结尾的就是中文的省略号.只要将字体变为统一宋体即可解决.即 .mini-grid-cell-inner { ...
- laravel5.5表单验证
1. 在第一次验证失败后停止 有时,你希望在某个属性第一次验证失败后停止运行验证规则.为了达到这个目的,附加 bail 规则到该属性: $this->validate($request, [ ' ...
- react书写规范小记
1.对齐方式 //如果没有属性,在自闭和标签前添加一个空格: <Footer /> //如果可以放在一行,放在一行上即可: <Footer bar="bar" / ...
- 剑指Offer - 九度1355 - 扑克牌顺子
剑指Offer - 九度1355 - 扑克牌顺子2014-01-30 23:19 题目描述: LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^). ...
- ASP.NET Core API ---状态码
摘录自:https://www.cnblogs.com/cgzl/p/9047626.html 状态码是非常重要的,因为只有状态码会告诉API的消费者: 请求是否如预期的成功,或者失败 如果出现了错误 ...
- Linux(Ubuntu 命令大全)
Ubuntu 一. Ubuntu简介 Ubuntu(乌班图)是一个基于Debian的以桌面应用为主的Linux操作系统,据说其名称来自非洲南部祖鲁语或科萨语的“ubuntu”一词,意思是“人性”.“我 ...
- PHP 比较运算符 var_dump("a" == 0) 为 true
这篇文章主要讲解一下 PHP 使用比较运算符容易出错的地方 $a == $b 等于 TRUE,如果类型转换后 $a 等于 $b.$a === $b 全等 TRUE,如果 $a 全等于 $b,并且它们的 ...
- codeblocks17.12 debug 报错:ERROR: You need to specify a debugger program in the debuggers's settings.
DebugERROR: You need to specify a debugger program in the debuggers's settings.(For MinGW compilers, ...
- Android之SQLite总结
SQLite 是一个轻量级的数据库,常用于各种嵌入式设备当中.android 提供了SQLiteOpenHelper的抽象类用于帮助开发数据库.在实际使用中经常定义一个类继承SQLiteOpenHel ...