POJ-3311 Hie with the Pie
地址:http://poj.org/problem?id=3311
思路:状态压缩dp,dp[i][j]:表示在状态i以点j为结尾时的最小距离。状态i表示已经走过了点,对于n个点,一共有0 -> (1<<n)-1个状态,因此
dp[i][j]=min{dp[i-去除的点j][k]+G[k][j]}
dp[i+新加入的点j][j]=min{dp[i][k]+G[k][j]}
Code1 :
#include<iostream>
#include<cstring>
using namespace std;const int MAX_N=15;
const int MAX_S=1050;
int n;
int G[MAX_N][MAX_N];
int dp[MAX_S][MAX_N];void Floyd();
int main()
{ios::sync_with_stdio(false);while(cin>>n&&n){memset(dp,0,sizeof(dp));for(int i=0;i<=n;++i)for(int j=0;j<=n;++j)cin>>G[i][j];Floyd();for(int i=1;i<=n;++i) dp[1<<(i-1)][i]=G[0][i];int s=(1<<n)-1;for(int i=1;i<=s;++i)for(int j=1,p=i;p;p>>=1,++j)if(p&1){for(int k=1,pp=i;pp;pp>>=1,++k)if(pp&1&&j!=k){if(!dp[i][j]) dp[i][j]=dp[i-(1<<(j-1))][k]+G[k][j];else dp[i][j]=min(dp[i][j],dp[i-(1<<(j-1))][k]+G[k][j]);}}int ans=dp[s][1]+G[1][0];for(int i=2;i<=n;++i)ans=min(ans,dp[s][i]+G[i][0]);cout<<ans<<endl;}return 0;
}void Floyd()
{for(int k=0;k<=n;++k)for(int i=0;i<=n;++i)for(int j=0;j<=n;++j)G[i][j]=min(G[i][j],G[i][k]+G[k][j]);
}
Code 2:
#include<iostream>
#include<cstring>
using namespace std;const int MAX_N=15;
const int MAX_S=1<<MAX_N;
int n;
int G[MAX_N][MAX_N];
int dp[MAX_S][MAX_N];void Floyd();
int main()
{ios::sync_with_stdio(false);while(cin>>n&&n){memset(dp,-1,sizeof(dp));for(int i=0;i<=n;++i)for(int j=0;j<=n;++j)cin>>G[i][j];Floyd();dp[1][0]=0;int s=(1<<(n+1))-1;for(int i=1;i<=s;++i)for(int j=0,p=i|1;p;p>>=1,++j)if((p&1)&&dp[i][j]!=-1){for(int k=0;k<=n;++k)if(dp[i|(1<<k)][k]==-1) dp[i|(1<<k)][k]=dp[i][j]+G[j][k];else dp[i|(1<<k)][k]=min(dp[i|(1<<k)][k],dp[i][j]+G[j][k]);}cout<<dp[s][0]<<endl;}return 0;
}void Floyd()
{for(int k=0;k<=n;++k)for(int i=0;i<=n;++i)for(int j=0;j<=n;++j)G[i][j]=min(G[i][j],G[i][k]+G[k][j]);
}
POJ-3311 Hie with the Pie相关推荐
- [POJ 3311] Hie with the Pie
这是状压DP中常见的一类问题(TSP问题)(废话) 题目大意: 给你一个n+1个点的有向完全图,现在要你从0号节点出发走过1-n号点至少一次,再返回0号点的最少时间,1<=n<=10 题意 ...
- POJ 3311 Hie with the Pie (flyod + DFS枚举)
先用floyd算法求出 2点间的最短距离,然后再用深搜枚举即可. #include<cstdio> #include<cstring> #include<iostream ...
- POJ - 3311 Hie with the Pie
题目: The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unf ...
- PKU 3311 Hie with the Pie 最短路 folyd
题意/Description: The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast ...
- Hie with the Pie(Floyd 状压DP)
POJ 3311 Hie with the Pie 题目大意 一个披萨店要请司机来送披萨,这家店最多接受10个订单,由于预算的问题,只能雇佣一名司机,要求用最短的时间,从披萨店出发送完最后再回到披萨店 ...
- 【POJ P3311】【状压DP】Hie with the Pie
Hie with the Pie 题目 解题思路 Code POJ P3311 Hie with the Pie 题目 Description The Pizazz Pizzeria prides i ...
- 【POJ 3311】Hie with the Pie(状压DP)
Hie with the Pie 题目链接:POJ 3311 题目大意 给你 n+1 个点,其中 0 号点是特殊点. 然后两个点之间都有路径可以走. 然后你要从特殊点出发走过所有点回到特殊点,问你最短 ...
- Hie with the Pie(poj3311)
题目链接:http://poj.org/problem?id=3311 学习博客:https://blog.csdn.net/u013480600/article/details/19692985 H ...
- Hie with the Pie(旅行商问题)
Hie with the Pie 题目链接 Description The Pizazz Pizzeria prides itself in delivering pizzas to its cust ...
- Hie with the Pie
Hie with the Pie poj-3311 题目大意:n+1个点,伪旅行商问题. 注释:n<=10. 想法:咳咳,第一道状压dp,下面我来介绍一下状压dp. 所谓dp,就是动态性决策规划 ...
最新文章
- 某字节程序员求助:家庭条件比leader好很多,leader还没买房,但自己已准备买车买房,会不会被仇富?网友:收敛点,别露富!...
- Android中的Touch事件
- php俩个字符串合并,php分割合并两个字符串的函数实例
- pytorch基础知识整理(五) 优化器
- SCNCHECKPOINT
- 工作170:删除做个判断操作 成功删除 取消取消
- MATLAB基础教程(9)——高级积分运算、二重积分
- jmeter mysql驱动jar包_用 Jmeter 做 Web 接口测试
- ubuntu linux修改ip地址命令,如何在ubuntu桌面通过命令行,更改ip地址
- pandas数据处理操作大全
- (转载)一级域名(顶级域名),二级域名,主域名,次域名,父域名,子域名联系与区别及域名带不带www的区别
- 论文解读《使用支持向量机和PSSM谱预测蛋白质中的RNA结合位点》
- 从算法工程师到全栈开发者,我终于明白了数据科学的真谛
- Codeforces 1324D Pair of Topics
- netsh interface portproxy listenaddress
- 使用css绘制弹幕,实现弹幕效果的方法总结(css和canvas)
- 【matlab数字图像处理】 uint8和double类型
- RSA数据加密解密(各种语言)
- 卡拉ok应用Soulo Karaoke评测
- Scrapy+redis+mongodb分布式爬虫抓取小说《冰与火之歌1-5》