标签:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 4860 | Accepted: 2581 |
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
题目大意,0代表商店,1到n代表去送货的地点,Map[i][j]代表从i到j的距离,每个点可以多次访问。问从商店出发,将商品送到n个地点再回到商店的最短时间。
首先floyd找出任意两点之间的最短距离。
状态代表访问了那几个地点,二进制数中第i个1表示第i个地点被访问,dp[i][j]在状态i时,以j为当前点的最短时间。
最后在找出 最后到达的地点+该地点到商店的 最小值。
#include <cstdio> #include <cstring> #include <algorithm> using namespace std ; #define INF 0x3f3f3f int Map[15][15] , a[15] ; int dp[5000][15] ; void floyd(int n) { int i , j , k ; for(k = 0 ; k <= n ; k++) for(i = 0 ; i <= n ; i++) for(j = 0 ; j <= n ; j++) if( Map[i][j] > Map[i][k] + Map[k][j]) Map[i][j] = Map[i][k] + Map[k][j] ; } int main() { int n , i , j , k , x , ans ; while( scanf("%d", &n) && n ) { for(i = 0 ; i <= n ; i++) for(j = 0 ; j <= n ; j++) scanf("%d", &Map[i][j]) ; floyd(n) ; x = 1<<n ; for(i = 0 ; i < x ; i++) for(j = 0 ; j < n ; j++) dp[i][j] = INF ; for(i = 0 ; i < n ; i++) dp[ 1<<i ][i] = Map[0][i+1] ; for(i = 1 ; i < x ; i++) { //printf("%d----", i) ; for(j = 0 ; j < n ; j++) { if( i & 1<<j ) a[j] = 1 ; else a[j] = 0 ; //printf("%d ", a[j]) ; } //printf("\n") ; for(j = 0 ; j < n ; j++) { if( a[j] ) { for(k = 0 ; k < n ; k++) { if( !a[k] ) { dp[i+(1<<k)][k] = min( dp[i+(1<<k)][k],dp[i][j] + Map[j+1][k+1] ) ; } } } } /*for(j = 0 ; j < n ; j++) printf("%d ", dp[i][j]) ; printf("\n") ;*/ } int min1 = INF ; for(i = 0 ; i < n ; i++) min1 = min(min1,dp[x-1][i]+Map[i+1][0]); printf("%d\n", min1) ; } return 0; }
poj3311--Hie with the Pie(状压dp)
标签:
原文地址:http://blog.csdn.net/winddreams/article/details/42586231