poj 3311 状态压缩
来源:互联网 发布:ubuntu如何安装deb软件 编辑:程序博客网 时间:2024/05/17 04: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
#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <iostream>#include <cmath>using namespace std;const int INF = 1e7;int n;int mp[15][15];int d[15][15];int dp[3000][15];void run(){ while(scanf("%d",&n),n){ for(int i = 0 ;i < n + 1; ++i) for(int j = 0;j < n + 1; ++j) scanf("%d",&d[i][j]); //floyd for(int i = 0;i < n + 1; ++i) for(int j = 0 ; j < n + 1; ++j) for(int k = 0; k < n + 1; ++k) d[i][j] = min(d[i][j],d[i][k] + d[k][j]); int ans = INF; int num = 1<<n; for(int i = 1; i < num ; i ++) for(int j = 1; j <= n + 1 ; j++) dp[i][j] = INF; //dp for(int i = 1;i < num; ++i){ for(int j = 1; j < n + 1; ++j) if(i == (1 << (j-1) ) ) dp[i][j] = d[0][j]; else if(i & (1 << (j-1) ) ) for(int k = 1; k <= n + 1; ++k) if(k != j && (i&(1<<(k-1)))) dp[i][j] = min(dp[i][j],dp[i^(1<<(j-1))][k] + d[k][j]); } for(int j = 1; j <= n + 1; ++j) ans = min(ans,dp[num - 1][j] + d[j][0]); printf("%d\n",ans); }}int main(){ run(); return 0;}
- poj 3311 状态压缩
- (3311)POJ-状态压缩
- poj 3311(状态压缩dp)
- poj 3311(floyd+状态压缩)
- poj 3311 状态压缩dp
- poj 3311 状态压缩DP
- poj 3311 状态压缩 hdu 4284 TSP
- hdu 4284(状态压缩)&& poj 3311 &&,,,
- POJ 3311 状态压缩DP简单题
- poj 3311 (状态压缩dp)
- POJ 3311 旅行商问题 状态压缩
- POJ 3311 (状态压缩dp)
- POJ 2923 状态压缩
- poj 2411 (状态压缩)
- POJ 3254 状态压缩
- (3254)POJ-状态压缩
- (1185)POJ-状态压缩
- (2817)POJ-状态压缩
- HDU 1043 八数码问题 A*搜索
- 【CareerCup】Trees and Graphs—Q4.3
- C# Socket通讯实例
- INT 13H - Wikipedia, the free encyclopedia
- java代码面试常见的算法-mark
- poj 3311 状态压缩
- 初学Java,包的用法(九)
- js数组和splice的用法
- springMvc简单实现
- 程序员需要走的路
- yii中attributes属性的巧妙应用
- 讲解ORA-00837错误,修改11G spfile
- 利用RMI技术开发聊天案例(尚有疑问,待解决!!!)
- 微信公众平台开发笔记