码迷,mamicode.com
首页 > 其他好文 > 详细

poj 1159 -- Hie with the Pie

时间:2014-10-29 18:47:44      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   color   os   ar   for   

Hie with the Pie
 
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 4619   Accepted: 2444

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

题目链接:Hie with the Pie

思路: 状态压缩dp. 第一题借鉴别人代码,为学习...

bubuko.com,布布扣
 1 /*======================================================================
 2  *           Author :   kevin
 3  *            Email :   ubuntu_kevin@126.com
 4  *         Filename :   HieWiththePie.cpp
 5  *       Creat time :   2014-10-26 21:01
 6  *      Description :
 7 ========================================================================*/
 8 #include <iostream>
 9 #include <algorithm>
10 #include <cstdio>
11 #include <cstring>
12 #include <queue>
13 #include <cmath>
14 #define clr(a,b) memset(a,b,sizeof(a))
15 #define M 15
16 #define INF 0x7f7f7f7f
17 using namespace std;
18 inline int min_32(int (a),int (b)){return (a)<(b)?(a):(b);}
19 inline int max_32(int (a),int (b)){return (a)>(b)?(a):(b);}
20 inline long long min_64(long long (a),long long (b)){return (a)<(b)?(a):(b);}
21 inline long long max_64(long long (a),long long (b)){return (a)>(b)?(a):(b);}
22 int s[M][M];
23 int dp[1<<M][M];
24 int n;
25 int main()
26 {
27     while(scanf("%d",&n) != EOF && n){
28         for(int i = 0; i <= n; i++){
29             for(int j = 0; j <= n; j++){
30                 scanf("%d",&s[i][j]);
31             }
32         }
33         for(int k = 0; k <= n; k++){
34             for(int i = 0; i <= n; i++){
35                 for(int j = 0; j <= n; j++){
36                     s[i][j] = min_32(s[i][k] + s[k][j],s[i][j]);
37                 }
38             }
39         }
40         clr(dp,0);
41         for(int state = 0; state < (1<<n); state++){
42             for(int i = 1; i <= n; i++){
43                 if(state & (1<<(i-1))){
44                     if(state == (1<<(i-1))){
45                         dp[state][i] = s[0][i];
46                     }
47                     else{
48                         dp[state][i] = INF;
49                         for(int j = 0; j <= n; j++){
50                             if(i != j && (state & (1<<(j-1)))){
51                                 int t = state^(1<<(i-1));
52                                 dp[state][i] = min_32(dp[state][i],dp[t][j]+s[j][i]);
53                             }
54                         }
55                     }
56                 }
57             }
58         }
59         int ans = INF;
60         for(int i = 1; i <= n; i++){
61             ans = min_32(ans,dp[(1<<n)-1][i] + s[i][0]);
62         }
63         printf("%d\n",ans);
64     }
65     return 0;
66 }
View Code

 

poj 1159 -- Hie with the Pie

标签:des   style   blog   http   io   color   os   ar   for   

原文地址:http://www.cnblogs.com/ubuntu-kevin/p/4059960.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!