POJ3311 Hie with the Pie

Hie with the Pie

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7624   Accepted: 4106

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

3
0 1 10 10
1 0 1 2
10 1 0 10
10 2 10 0
0

Sample Output

8

Source

East Central North America 2006

大意如下:

给定一张有n + 1个节点的有向图,每两个点之间均有两条方向不一的边,权值不一定相同。问你从0号节点出发,走过所有1...n个节点并回到0号节点所花费的时间。

首先Floyd预处理多源最短路。

状压方程:

f[S][i]表示从0号点出发,走过集合S中的点并到达i点的最短路长度。

转移:

f[S][i] = f[S/i][j] + g[j][i];

初始状态:

f[(1 << (i - 1))][i] = g[0][i]

答案:

min(f[(1 << n) - 1][i] + g[i][0]),i为1....n

Code

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <vector>
 7 #include <queue>
 8 #include <stack>
 9 inline void read(int &x){x = 0;char ch = getchar();char c = ch;while(ch < ‘0‘ || ch > ‘9‘)c = ch, ch = getchar();while(ch <= ‘9‘ && ch >= ‘0‘)x = x * 10 + ch - ‘0‘, ch = getchar();if(c == ‘-‘)x = -x;}
10 inline int max(int a, int b){return a > b ? a : b;}
11 inline int min(int a, int b){return a < b ? a : b;}
12
13 const int INF = 0x3f3f3f3f;
14 const int MAXN = 10 + 4;
15
16 int n,g[MAXN][MAXN],dp[1 << MAXN][MAXN],ans;
17
18 inline void init()
19 {
20     //清空
21     memset(g, 0, sizeof(g));
22     memset(dp, 0, sizeof(dp));
23     //读入
24     for(int i = 0;i <= n;++ i)
25         for(int j = 0;j <= n;j ++)
26             read(g[i][j]);
27 }
28
29 inline void Floyd()
30 {
31     for(int k = 0;k <= n;++ k)
32         for(int i = 0;i <= n;++ i)
33             for(int j = 0;j <= n;++ j)
34                 if(g[i][j] > g[i][k] + g[k][j])
35                     g[i][j] = g[i][k] + g[k][j];
36 }
37
38 //dp[S][j]  表示已经经过点集i到达j点的最短时间   1表示经过,0表示未经过
39 //dp[S][j] = min(dp[S/k][k] + g[k][j])  k∈S  iS/k表示i去掉k
40
41 inline void DP()
42 {
43     //枚举点集
44     for (int S = 1;S < (1 << n); ++ S)
45         //枚举出发点
46         for (int i = 1;i <= n;++ i)
47         {
48             if(!(S & (1 << (i - 1))))continue;
49             //如果集合中只有出发点一个点  到达它需要g[0][i]的时间
50             if (S == (1 << (i - 1)))
51             {
52                 dp[S][i] = g[0][i];
53                 break;
54             }
55             dp[S][i] = INF;
56             for (int j = 1;j <= n;++ j)
57             {
58                 if (j == i)continue;
59                 if (!(S & (1 << (j - 1))))continue;
60                 dp[S][i] = min(dp[S][i], dp[S ^ (1 << (i - 1))][j] + g[j][i]);
61             }
62         }
63     ans = INF;
64     for (int i = 1;i <= n;++ i)
65         ans = min(ans, dp[(1 << n) - 1][i] + g[i][0]);
66 }
67
68 inline void out()
69 {
70     printf("%d\n", ans);
71 }
72
73 int main()
74 {
75     while (scanf("%d", &n))
76     {
77         if (!n)break;
78         //初始化操作
79         init(); Floyd();DP(); out();
80     }
81     return 0;
82 }

时间: 2024-10-30 14:11:36

POJ3311 Hie with the Pie的相关文章

poj3311(Hie with the Pie)状压dp

题目链接:http://poj.org/problem?id=3311 解法:标准的状压dp类型,先floyd获得两两之间最短距离.然后dp[i][j]表示剩下集合i没走,已经走到j的最短距离: 代码: /****************************************************** * @author:xiefubao *******************************************************/ #pragma comment(

状压DP Poj3311 Hie with the Pie

本人水平有限,题解不到为处,请多多谅解 本蒟蒻谢谢大家观看 题目: Problem C: Poj3311 Hie with the Pie Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 58  Solved: 29[Submit][Status][Web Board] Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast

poj3311 Hie with the Pie 旅行商问题(TSP)

题目链接 http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5094   Accepted: 2716 Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortuna

POJ3311——Hie with the Pie

Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4646   Accepted: 2457 Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutbacks, they can affo

poj3311 Hie with the Pie,状态压缩

题目链接:http://poj.org/problem?id=3311 Floyd + 状态压缩DP 题意是有N个城市(1~N)和一个PIZZA店(0),要求一条回路,从0出发,又回到0,而且距离最小. 状态:dp[S][v]表示从v出发访问剩余的所有顶点(集合S),最终回到顶点0的路径的权重总和最小值. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; cons

Hie with the Pie(poj3311)

题目链接:http://poj.org/problem?id=3311 学习博客:https://blog.csdn.net/u013480600/article/details/19692985 Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9954   Accepted: 5368 Description The Pizazz Pizzeria prides itself in de

POJ 3311 Hie with the Pie (Floyd + 状压dp 简单TSP问题)

Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5019   Accepted: 2673 Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutbacks, they can affo

POJ 3311 Hie with the Pie (状压DP)

状态压缩DP dp[i][j]表示在i状态(用二进制表示城市有没有经过)时最后到达j城市的最小时间 转移方程dp[i][j]=min(dp[i][k]+d[k][j],dp[i][j]) d[k][j]是k城市到j城市的最短距离 要先用flody处理 #include<bits.stdc++.h> using namespace std; int d[20][20],dp[1<<11][20]; int n,m; void flody() { for(int k=0;k<=n

poj 3311 Hie with the Pie dp+状压

Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4671   Accepted: 2471 Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutbacks, they can affo