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 ...
随机推荐
- bootloader svc 模式
bootloader 和操作系统都是工作在svc模式下 /* * set the cpu to SVC32 mode */ mrs r0,cpsr bic r0,r0,#0x1f orr r0,r0, ...
- 如何使用Python脚本
来自官方文档 一.写 python 脚本: import sys import datetime for line in sys.stdin: line = line.strip() userid, ...
- 2,版本控制git --分支
有人把 Git 的分支模型称为它的`‘必杀技特性’',也正因为这一特性,使得 Git 从众多版本控制系统中脱颖而出. 为何 Git 的分支模型如此出众呢? Git 处理分支的方式可谓是难以置信的轻量, ...
- 14,vue+uwsgi+nginx部署路飞学城
有一天,老男孩的苑日天给我发来了两个神秘代码,听说是和mjj的结晶 超哥将这两个代码,放到了一个网站上,大家可以自行下载 路飞学城django代码 https://files.cnblogs.com/ ...
- W/System.err: at android.view.ViewConfiguration.get(ViewConfiguration.java:369)
*11-09 11:48:38.558 13887-13900/? W/System.err: at android.view.WindowManagerGlobal.getWindowManager ...
- python开发记录第一篇
1. 安装pyCharm,下载地址https://www.jetbrains.com/pycharm/ 2. 注册license,修改windwos系统hosts,文件路径为:C:\Windows\S ...
- centos使用--supervisor使用
目录 1 下载程序并安装 2 编辑配置文件 3 supervisor的使用 4 配置文件详细解析 参考资料 supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变 ...
- VS Extension+NVelocity系列(二)——让VS支持 NVelocity的智能提示(上)
一.基础概念 应该庆幸的是,VS的插件是靠着MEF实现而不是MAF,这让你所做的工作减轻了许多.如果在这之前,您已经了解了MEF的原理,我想对于VS插件的编写,您应该是很容易就能理解的.看看几个VS2 ...
- Nuget 异常引用记录
事件描述 Nuget未能将packages.config中的dll成功引入项目中 解决办法 从Nuget中删除对NewtonSoft.Json的引用并重新对NewtonSoft.Json 4.5.0. ...
- iOS笔记059 - 网络总结
网络 基本概念 客户端:client 服务器:server 请求:request 响应:response 过程 客户端 -> 发送请求 -> 服务器(连接数据库) 服务器 -> 发送 ...