Hie with the Pie (状态dp)
来源:互联网 发布:ubuntu wine 1.9 编辑:程序博客网 时间:2024/06/06 20:05
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 will consist of multiple test cases. The first line will contain a single integern indicating the number of orders to deliver, where 1 ≤ n ≤ 10. After this will ben + 1 lines each containing n + 1 integers indicating the times to travel between the pizzeria (numbered 0) and then locations (numbers 1 to n). The jth value on the ith line indicates the time to go directly from location i to locationj without visiting any other locations along the way. Note that there may be quicker ways to go fromi 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 locationi to j may not be the same as the time to go directly from locationj to i. An input value of n = 0 will terminate input.
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.
30 1 10 101 0 1 210 1 0 1010 2 10 00
8
题目大概:
要给很多店从东西,从一个店到另一个店时间不一样,最后要回到比萨饼店。问最短时间是多少。
思路:
先用弗洛伊德算出最短路径
然后
状态dp。
dp【i】【j】状态 i 的时候到达了 j 店的最短时间
代码:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int ma=11;int dp[1<<ma][11];int col[ma][ma];int main(){ int n; while(~scanf("%d",&n)) { if(n==0)break; memset(dp,0,sizeof(dp)); for(int i=0;i<=n;i++) { for(int j=0;j<=n;j++) { scanf("%d",&col[i][j]); } } for(int k=0;k<=n;k++) for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) col[i][j]=min(col[i][k]+col[k][j],col[i][j]);//弗洛伊德最短路径 int cnt=1<<n; for(int j=1;j<cnt;j++)//枚举所有状态 { for(int r1=0;r1<=n;r1++)//枚举到达的店 { int tem=(1<<(r1-1)); if(tem&j)//给店要合法 { if(tem==j)dp[j][r1]=col[0][r1];//可能只有第一个店 else{ dp[j][r1]=0x3f3f3f; for(int i=1;i<=n;i++)//或者是从其他店来到本店 { if(i!=r1&&j&(1<<(i-1)))//该店不能r1重复,并且要在状态内。 dp[j][r1]=min(dp[j][r1],dp[j^(1<<(r1-1))][i]+col[i][r1]); } } } } } int ans=dp[(1<<n)-1][1]+col[1][0]; for(int i=2;i<=n;i++) if(ans>dp[(1<<n)-1][i]+col[i][0]) ans=dp[(1<<n)-1][i]+col[i][0]; printf("%d\n",ans); } return 0;}
- Hie with the Pie (状态dp)
- POJ3311 Hie with the Pie(状态压缩dp)
- POJ 3311 Hie with the Pie(状态压缩DP)
- poj3311 Hie with the Pie(状态压缩dp)
- POJ 3311 Hie with the Pie(状态压缩DP)
- POJ 3311 Hie with the Pie(状态压缩dp)
- poj 3311 Hie with the Pie(状态压缩dp)
- POJ 3311 Hie with the Pie(Floyd+状态压缩DP)
- POJ 3311 Hie with the Pie //状态压缩DP
- POJ 3311 Hie with the Pie -- TSP 状态压缩dp
- POJ 3311 Hie with the Pie (状态压缩DP)
- (简单) 状态压缩dp HOJ 2421 Hie with the Pie
- 状态压缩DP-Hie With the Pie(POJ 3311)
- POJ3311:Hie with the Pie(floyd+状态压缩DP)
- POJ 3311 Hie with the Pie(状态压缩DP)
- poj 3311 Hie with the Pie(状态压缩DP)
- POJ 3311 Hie with the Pie(Floyd+状态压缩DP)
- poj3311 Hie with the Pie(最短路+状态dp)
- 近日有需要写点C#程序,有用到Dataset数据集和SQLite数据库,由于我从来就不擅长记各种编程语言的语法,所以在查阅一堆资料后,留下以下内容备忘:
- 网络的工具类
- MVC4发送邮件
- 简单Rxjava订阅的源码流程分析
- Mac系统WIFI环境下用idea开发java出现IPv6地址问题
- Hie with the Pie (状态dp)
- SharedPreferences记住信息
- 会话技术-cookie入门(十五)
- 人工智能软件工程师软件清单
- vue 中 滚动条始终定位在底部
- 算法导论笔记(五)
- mabatis中resultMap中的映射小问题
- SpringMVC之数据回显
- Namenode HA原理以及配置