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 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
i
th 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

经典TSP问题,状压dp外加floyd

#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int inf = 0x3f3f3f3f;
int dp[2100][15];
int floyd[15][15];

int main()
{
	int n;
	while (~scanf("%d", &n), n)
	{
		memset ( floyd, inf, sizeof(floyd) );
		memset ( dp, inf, sizeof(dp) );
		for (int i = 0; i <= n; ++i)
		{
			for (int j = 0; j <= n; ++j)
			{
				scanf("%d", &floyd[i][j]);
			}
		}
		for (int k = 0; k <= n; ++k)
		{
			for (int i = 0; i <= n; ++i)
			{
				for (int j = 0; j <= n; ++j)
				{
					floyd[i][j] = min(floyd[i][j], floyd[i][k] + floyd[k][j]);
				}
			}
		}
		dp[0][0] = 0;
		for (int i = 0; i < (1 << (1 + n)); ++i)
		{
			for (int j = 0; j <= n; ++j)
			{
				for (int k = 0; k <= n; ++k)
				{
					if ( (i & (1 << j)) == 0 )
					{
						dp[i | (1 << j)][j] = min(dp[i | (1 << j )][j], dp[i][k] + floyd[k][j]);
					}
				}
			}
		}
		printf("%d\n", dp[(1 << (n + 1)) - 1][0]);
	}
	return 0;
}
时间: 2024-10-28 21:19:45

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: 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 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