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

来源:互联网 发布:linux 进程调度命令 编辑:程序博客网 时间:2024/06/05 18:33
Hie with the Pie
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 4703 Accepted: 2494

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

East Central North America 2006


题意:
给你一个(n+1)*(n+1)的矩阵,mp[i][j]表示城市i到j的时间,求经过所有城市的最少时间,并且得回到原点。

思路:
经典的TSP 问题,先floyd,再DP.

AC Code:
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<string>#include<algorithm>#include<cstdlib>#include<set>#include<queue>#include<stack>#include<vector>#include<map>#define N 100010#define Mod 10000007#define lson l,mid,idx<<1#define rson mid+1,r,idx<<1|1#define lc idx<<1#define rc idx<<1|1const double EPS = 1e-11;const double PI = acos ( -1.0 );const double E = 2.718281828;typedef long long ll;const int INF = 1000010;using namespace std;int n;int dp[ ( 1 << 11 ) + 1][12];int mp[13][13];void floyed(){    for ( int k = 0; k < n; k++ )        for ( int i = 0; i < n; i++ )            for ( int j = 0; j < n; j++ )                mp[i][j] = min ( mp[i][j], mp[i][k] + mp[k][j] );}int main(){    while ( ~scanf ( "%d", &n ) && n )    {        n++;        for ( int i = 0; i < n; i++ )            for ( int j = 0; j < n; j++ )                scanf ( "%d", &mp[i][j] );        floyed();        for ( int i = 0; i < ( 1 << n ); i++ )            for ( int j = 0; j < n; j++ )                dp[i][j] = INF;        dp[0][0] = 0;        for ( int i = 0; i < ( 1 << n ); i++ )        {            for ( int j = 0; j < n; j++ )            {                if ( dp[i][j] == INF )                    continue;                for ( int k = 0; k < n; k++ )                {                    if ( i & ( 1 << k ) )                        continue;                    if ( j == k )                        continue;                    dp[ ( 1 << k ) | i][k] = min ( dp[ ( 1 << k ) | i][k], dp[i][j] + mp[j][k] );                }            }        }        int ans = INF;        int p=(1<<n)-1;        for ( int i = 0; i < n; i++ )            if ( dp[p][i] + mp[i][1] < ans )                ans = dp[p][i] + mp[i][0];  cout<<ans<<endl;    }    return 0;}


0 0
原创粉丝点击