您的位置:首页 > 其它

POJ 3311 Hie with the Pie (状态压缩DP ,tsp问题)

2017-05-22 16:47 441 查看
Hie with the Pie

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 7471 Accepted: 4014
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


分析:

【题目大意】类似于TSP问题,只是每个点可以走多次,比经典TSP问题不同的是要先用弗洛伊的预处理一下两两之间的距离。求最短距离。
【解析】可以用全排列做,求出一个最短的距离即可。或者用状态压缩DP.用一个二进制数表示城市是否走过
【状态表示】dp[state][i]表示到达i点状态为state的最短距离
【状态转移方程】dp[state][i] =min{dp[state][i],dp[state'][j]+dis[j][i]} dis[j][i]为j到i的最短距离
【DP边界条件】dp[state][i] =dis[0][i]  state是只经过i的状态

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <queue>
#define mem(p,k) memset(p,k,sizeof(p));
#define rep(a,b,c) for(int a=b;a<c;a++)
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define inf 0x6fffffff
#define ll long long
using namespace std;
int mapp[20][20],dp[2000][20];
void floyd(int n){
for(int k=0;k<=n;k++){
for(int i=0;i<=n;i++){
for(int j=0;j<=n;j++){
mapp[i][j]=min(mapp[i][j],mapp[i][k]+mapp[k][j]);
}
}
}

}
int count_1(int i){
int sum=0;
while(i){
if(i&1)sum++;
i>>=1;
}
return sum;
}
int main()
{
int n;
while(scanf("%d",&n) && n){
for(int i=0;i<=n;i++){
for(int j=0;j<=n;j++)scanf("%d",mapp[i]+j);
}
floyd(n);
int tol=1<<n;
for(int i=0;i<tol;i++){
for(int j=0;j<n;j++){
if(i==(1<<j)){
dp[i][j+1]=mapp[0][j+1];
}
else{
dp[i][j+1]=inf;
if(i & (1<<j)){
for(int k=0;k<n;k++){
if(i & (1<<k)){
dp[i][j+1]=min(dp[i][j+1],dp[i ^(1<<j) ][k+1] + mapp[k+1][j+1]);
}

}

}
}
}
}
int minn=inf;
for(int i=1;i<=n;i++)minn=min(minn,dp[tol-1][i]+mapp[i][0]);
cout<<minn<<endl;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: