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 ...
随机推荐
- 读键值对封装成Map
描述: 有配置文件address_relation.properties,记录地址关系,有如下数据:ZSSS=ZS%,ZSPD, 封装到Map代码如下: public static void main ...
- elasticsearch 5.x 系列之七 基于索引别名的零停机升级服务
一,写在前面的话,elasticsearch 建立索引时的Mapping 设置 建议你在设计索引的初期,就把索引的各个字段设计好,因为,elasticsearch 的各个字段,定义好类型后,就无法进行 ...
- POJ:3616-Milking Time
Milking Time Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12324 Accepted: 5221 Descrip ...
- PHP.TP框架下商品项目的优化1-时间插件、鼠标所在行高亮、布局规划页面
1.优化搜索表单中按时间搜索的功能 添加一个时间插件datetimepicker,在lst.html中,注意要导入jquery.min.js,此处从前文的在线编辑器中导入 <!-- 导入 --& ...
- 4,MongoDB 之 $关键字 及 $修改器 $set $inc $push $pull $pop MongoDB
MongoDB中的关键字有很多, $lt $gt $lte $gte 等等,这么多我们也不方便记,这里我们说说几个比较常见的 一.查询中常见的 等于 大于 小于 大于等于 小于等于 等于 : 在Mon ...
- 直接插入排序&希尔排序
1.直接插入排序 时间复杂度O(n2) 工作原理: 通过构建有序序列,对于未排序数据,在已排序的序列中,从后向前扫描,找到相应的位置并插入. 插入排序在实现上,在从后向前扫描的过程中,需要反复把已排序 ...
- centos使用--防火墙
目录 1 切换到zsh 1.1 查看系统当前的shell 1.2 查看bin下是否有zsh包 1.3 安装zsh包 1.4 切换shell至zsh 2 安装oh-my-zsh 2.1 oh-my-zs ...
- shell判断文件是否存在[转]
原文出处: http://canofy.iteye.com/blog/252289 shell判断文件,目录是否存在或者具有权限 #!/bin/sh myPath="/var/log/htt ...
- Pascal小游戏 随机函数
一个被人写滥了的小程序,新手学习,Pascal By Chaobs 初学者可以用它来学习随机函数的运用,当然你完全可以自己写一个随机函数. var player1,player2:longint; ...
- DataSource的设置
1.Centos和redhat,Fedora等版本无须设置,直接在cloud.cfg指定,默认是EC2 datasource_list: ['ConfigDrive','OpenStack'] 2.u ...