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;

const int maxn = 12;
const int INF = 0x3f3f3f3f;

int d[maxn][maxn];
int dp[1<<maxn][maxn];

int main()
{
	int n;
	while(~scanf("%d", &n)){
		if(n==0) break;
		n++;
		for(int i=0; i<n; ++i){
			for(int j=0; j<n; ++j)
				scanf("%d", &d[i][j]);
		}
		for(int k=0; k<n; ++k)
		for(int i=0; i<n; ++i)
		for(int j=0; j<n; ++j)
		{
			d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
		}
		int S = 1<<n;
		for(int i=0; i<S; ++i){
			fill(dp[i], dp[i]+n, INF);
		}
		dp[S-1][0] = 0;
		for(int i=S-2; i>=0; --i){
			for(int v=0; v<n; ++v){
				for(int u=0; u<n; ++u){
					if(!(i>>u&1) ){
						dp[i][v] = min(dp[i][v], dp[i|1<<u][u]+d[v][u]);
					}
				}
			}
		}
		printf("%d\n", dp[0][0]);
	}
	return 0;
}
时间: 2024-10-10 10:09:47

poj3311 Hie with the Pie,状态压缩的相关文章

poj 3311 Hie with the Pie (状态压缩+最短路)

Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4491   Accepted: 2376 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 (状态压缩)

题目大意:有一间披萨店,要送n个披萨去不同的地方 现在给出每个位置之间的距离,每个位置都可以重复经过,问送完所有披萨再回到店里需要走的最短距离是多少 解题思路:这题的话,有两个状态,一个是现所在地点,另一个是已经经过的地点,所以dp数组是二维的 设dp[i][j]为现所在地为i,经过的城市的状态为j的最短路线 那么dp[i][state | (1 << i)] = min(dp[i][state | (1 << i)], dp[j][state] + g[j][i]) ...这题我

状压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)状压dp

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

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

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

poj 3311 Hie with the Pie(状态压缩dp)

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

POJ 3311 Hie with the Pie(Floyd+状态压缩DP)

贴一个TSP讲解:点击打开链接 错误的转移方程 dp[i][j] 把i当作了步数,以为至多走N步就可以了.作死啊 #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #define maxn 1100 #define inf 0x3f3f3f3f const double eps=1e-8; using namespace std; int dp[12][1<