Hie with the Pie(POJ 3311状压dp)
题意:披萨店给n个地方送披萨,已知各地方(包括披萨店)之间花费的时间,求送完所有地方并回到店花费的最小时间
分析:状态好确定dp[i][j],i中1表示地方已送过,否则为0,j为当前状态最后一个送过的地方,注意怎么走才算最小时间,当然是走最短路,点很少由floyd求出各点最短路
求回到店的最小时间,从店出发(状态为1)。
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = 1000000007;
int d[15][15],a[15][15],n,dp[2500][15];
void solve(){
int c=(1<<(n+1))-1;
for(int k=0;k<=n;++k)
for(int i=0;i<=n;++i)
for(int j=0;j<=n;++j)
d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
memset(dp,-1,sizeof(dp));
dp[1][0]=0;
for(int i=1;i<=c;++i){
i|=1;
for(int j=0;j<=n;++j){
if(dp[i][j]==-1)continue;
for(int k=0;k<=n;++k)
if(dp[i|(1<<k)][k]==-1&&j!=k||dp[i|(1<<k)][k]>dp[i][j]+d[j][k])
dp[i|(1<<k)][k]=dp[i][j]+d[j][k];
}
}
printf("%d\n",dp[c][0]);
}
int main()
{
while(~scanf("%d",&n)){
if(n==0)break;
for(int i=0;i<=n;++i)
for(int j=0;j<=n;++j)
scanf("%d",&d[i][j]);
solve();
}
return 0;
}
Hie with the Pie(POJ 3311状压dp)的更多相关文章
- POJ 3311 Hie with the Pie 最短路+状压DP
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11243 Accepted: 5963 ...
- poj 3311 状压dp 最短路
C - Hie with the Pie Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64 ...
- poj 3311 状压DP
经典TSP变形 学到:1.floyd O(n^3)处理随意两点的最短路 2.集合的位表示,我会在最后的总结出写出.注意写代码之前一定设计好位的状态.本题中,第0位到第n位分别代表第i个城市,1是已经 ...
- Hie with the Pie POJ - 3311
Hie with the Pie POJ - 3311 The Pizazz Pizzeria prides itself in delivering pizzas to its customers ...
- 状压dp+floyed(C - Hie with the Pie POJ - 3311 )
题目链接:https://cn.vjudge.net/contest/276236#problem/C 题目大意: 给你一个有n+1(1<=n<=10)个点的有向完全图,用矩阵的形式给出任 ...
- POJ 3254 (状压DP) Corn Fields
基础的状压DP,因为是将状态压缩到一个整数中,所以会涉及到很多比较巧妙的位运算. 我们可以先把输入中每行的01压缩成一个整数. 判断一个状态是否有相邻1: 如果 x & (x << ...
- poj 1170状压dp
题目链接:https://vjudge.net/problem/POJ-1170 题意:输入n,表示有那种物品,接下来n行,每行a,b,c三个变量,a表示物品种类,b是物品数量,c代表物品的单价.接下 ...
- Hie with the Pie (POJ 3311) 旅行商问题
昨天想练习一下状态压缩,百度搜索看到有博客讨论POJ 3311,一看就是简单的旅行商问题,于是快速上手写了状态压缩,死活样例都没过... 画图模拟一遍原来多个城市可以重复走,然后就放弃思考了... 刚 ...
- POJ 3254 状压DP
题目大意: 一个农民有一片n行m列 的农场 n和m 范围[1,12] 对于每一块土地 ,1代表可以种地,0代表不能种. 因为农夫要种草喂牛,牛吃草不能挨着,所以农夫种菜的每一块都不能有公共边. ...
随机推荐
- FileOutputStream和FileInputStream
package one.string; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFound ...
- 黑马程序员-- .net基础加强8之委托,事件,程序集
---------------------- ASP.Net+Android+IOS开发..Net培训.期待与您交流! ---------------------- 一.委托 ============ ...
- 欧拉工程第72题:Counting fractions
题目链接:https://projecteuler.net/problem=72 真分数;n/d 当d ≤ 1,000,000时候的真分数有多少个 public class P72{ void run ...
- 欧拉工程第63题:Powerful digit counts
题目链接 The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=8 ...
- *[hackerrank]Jim Beam
https://www.hackerrank.com/contests/infinitum-aug14/challenges/jim-beam 学习了线段相交的判断法.首先是叉乘,叉乘的几何意义是有向 ...
- mvn 安装ojdbc6.jar
mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion= - Dpackaging=jar -Dfile ...
- Nim语言:Pascal的语法,Python的缩进
http://nim-lang.org/ 德国人Andreas Rumpf的作品,原因是他对过去使用的每种语言都不满意(Pascal也不满意?).以前叫Nimrod语言,从0.96版本开始改名为Nim ...
- Android:PopupWindow简单弹窗改进版
Android:PopupWindow简单弹窗 继续上一节的内容,改进一下,目标是点击菜单后把菜单收缩回去并且切换内容,我使用的是PopupWindow+RadioGroup public class ...
- PHP无限极分类生成树方法
你还在用浪费时间又浪费内存的递归遍历无限极分类吗,看了该篇文章,我觉得你应该换换了.这是我在OSChina上看到的一段非常精简的PHP无限极分类生成树方法,整理分享了. function genera ...
- HBase的Shell操作
1.进入命令行 bin/hbase shell 2.输入help 查看各种命令组. 命令是分组的,可以执行help 'general'查看general组的命令. 3.常用命令 --显示有哪些表 li ...