Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 3994 | Accepted: 2118 |
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
题意就是有n个点得走完,可以重复走,从0点出发,最后要回到0点,问最小的距离是多少。
题解,由于数据范围小先可以将两两之间的距离用floyd算法求出来,然后可以用全排列枚举到每个点的顺序,最后再加上最后一个点到0点的最小距离即可取到最短距离。用状态压缩dp的方法跟这思想差不多,枚举1<<n个状态,dp[sta][i]表示到达i点状态为sta的最短距离,边界值为dp[0][i]=dis[0][i]代表从0到i的距离。然后也要加上最后到的点到0的距离即可。
全排列:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
using namespace std;
#define LL __int64
int mp[15][15];
int a[15],n,i,j,k;
int main()
{
while(1==scanf("%d",&n)&&n)
{
for (i=0;i<=n;i++)
{
if (i!=0) a[i-1]=i;
for (j=0;j<=n;j++)
scanf("%d",&mp[i][j]);
}
for (k=0;k<=n;k++)
for (i=0;i<=n;i++)
for (j=0;j<=n;j++)
if (mp[i][k]+mp[k][j]<mp[i][j])
mp[i][j]=mp[i][k]+mp[k][j];
sort(a,a+n);
int ans=0x7fffffff;
do
{
int sum=mp[0][a[0]];
for (i=1;i<n;i++)
sum+=mp[a[i-1]][a[i]];
sum+=mp[a[n-1]][0];
if (sum<ans)
ans=sum;
}while (next_permutation(a,a+n));
cout<<ans<<endl;
}
return 0;
}
状态压缩+dp.
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
using namespace std;
#define LL __int64
#define INF 0x7fffffff
#define min(a,b) a>b?b:a
int dis[20][20];
int dp[2050][20];
int main()
{
int n,i,j,k,t;
while (~scanf("%d",&n)&&n)
{
for (i=0;i<=n;i++)
for (j=0;j<=n;j++)
scanf("%d",&dis[i][j]);
for (k=0;k<=n;k++)
for (i=0;i<=n;i++)
for (j=0;j<=n;j++)
if (dis[i][k]+dis[k][j]<dis[i][j])
dis[i][j]=dis[i][k]+dis[k][j];
for (t=0;t<(1<<n);t++)
{
for (i=1;i<=n;i++)
if (t & (1<<(i-1)))
if (t==(1<<(i-1))) dp[t][i]=dis[0][i];
else
{
dp[t][i]=INF;
for (j=1;j<=n;j++)
if((t &(1<<(j-1))) && (j!=i))
dp[t][i]=min(dp[t][i],dp[(1<<(i-1))^t][j]+dis[j][i]);
}
}
int ans=dp[(1<<n)-1][1]+dis[1][0];
for (i=2;i<=n;i++)
if (dp[(1<<n)-1][i]+dis[i][0]<ans)
ans=dp[(1<<n)-1][i]+dis[i][0];
printf("%d\n",ans);
}
return 0;
}