POJ 3311 Hie with the Pie (Floyd+状压DP)
来源:互联网 发布:淘宝客服岗位说明 编辑:程序博客网 时间:2024/06/06 16:42
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 to j 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
30 1 10 101 0 1 210 1 0 1010 2 10 00
Sample Output
8
Source
East Central North America 2006
dp[i][j]表示到达点i,状态为j时的最短距离
状态转移方程:dp[i][j]=min(dp[i][j],dp[k][j^(1<<(i-1))]+dis[k][i]);
(dp[k][i]表示k到i的最短距离,dp[k][j^(1<<(i-1))]表示j状态不经过i时的状态到达k点的最短距离)
边界条件:dp[i][j]=dis[0][i];(j是只经过点i的状态)
#include <stdio.h>#include <string.h>#include <math.h>#include <algorithm>#define mod 100000000int const inf=0xfffffff;using namespace std;int dp[15][(1<<10)+10],dis[15][15],n;void floyd(){int i,j,k;for(k=0;k<=n;k++){for(i=0;i<=n;i++){for(j=0;j<=n;j++){if(dis[i][j]>dis[i][k]+dis[k][j])dis[i][j]=dis[i][k]+dis[k][j];}}}}int main (){int i,j,k;while(scanf("%d",&n)!=EOF && n){for(i=0;i<=n;i++){for(j=0;j<=n;j++){scanf("%d",&dis[i][j]);}}floyd();for(j=0;j<(1<<n);j++){for(i=1;i<=n;i++){if(j==(1<<(i-1)))dp[i][j]=dis[0][i];else{dp[i][j]=inf;for(k=1;k<=n;k++){if(k!=i && (j&(1<<(k-1))))dp[i][j]=min(dp[i][j],dp[k][j^(1<<(i-1))]+dis[k][i]);}}}}int ans=dp[1][(1<<n)-1]+dis[1][0];for(i=2;i<=n;i++)ans=min(ans,dp[i][(1<<n)-1]+dis[i][0]);printf("%d\n",ans);}return 0;}
- poj 3311 Hie with the Pie(floyd + 状压dp)
- POJ 3311 Hie with the Pie floyd+状压DP
- POJ 3311 Hie with the Pie(状压DP + Floyd)
- POJ 3311-Hie with the Pie(floyd+TSP 状压DP)
- POJ 3311 Hie with the Pie(状压dp+floyd)
- poj 3311 Hie with the Pie(Floyd+状压dp)
- Poj 3311 Hie with the Pie【Floyd+状压dp】
- [POJ 3311]Hie with the Pie(状压DP+Floyd)
- POJ 3311 Hie with the Pie Floyd+状压dp
- POJ 3311 Hie with the Pie(Floyd + 状压dp)
- POJ 3311 Hie with the Pie (Floyd+状压DP)
- POJ 3311 Hie with the Pie(Floyd+状态压缩DP)
- POJ 3311 Hie with the Pie(Floyd+状态压缩DP)
- POJ 3311 Hie with the Pie(Floyd+状态压缩DP)
- poj 3311 Hie with the Pie floyd+状态压缩dp
- POJ 3311 Hie with the Pie(Floyd+状态压缩DP)
- poj 3311 Hie with the Pie(状态压缩DP+floyd)
- POJ 3311 Hie with the Pie 状态压缩DP+floyd
- 图像及其它坐标系
- 网络流 增广路 入门很好的文章
- Farm Irrigation HDU
- http,socket,tcp/ip 网络传输与通讯知识总结
- K9F1G08U0E-SCB0 兼容的芯片
- POJ 3311 Hie with the Pie (Floyd+状压DP)
- UVALive
- The processing instruction target matching "[xX][mM][lL]" is not allowed.错误
- Cascading——针对Hadoop MapReduce的数据处理API
- 【Idea】——非Maven项目导入jar包
- 并查集知识图解
- mapreduce实现ItemCF——基于物品的协同过滤
- leetcode--Trapping Rain Water
- 多线程并发