Test instructions
To the starting point and N points, from the starting point to go through the N points each point at least once back to the beginning of the shortest.
Analysis:
Similar to the postman problem, directly with the STL enumeration access order of violence resolved.
Code:
POJ 2907
//sep9
#include <iostream>
#include <algorithm>
using namespace std;
int x[16],y[16];
int d[16][16];
int a[16];
int n;
int main ()
{
int cases;
scanf ("%d", &cases);
while (cases--) {
scanf ("%*d%*d%d%d", &x[0],&y[0]);
int i,j,n;
scanf ("%d", &n);
for (i=1;i<=n;++i)
scanf ("%d%d", &x[i],&y[i]);
for (I=0;i<=n;++i) for
(j=i;j<=n;++j)
d[i][j]=d[j][i]=abs (X[i]-x[j]) +abs (Y[i]-y[j]);
int Ans=int_max;
for (i=0;i<=n;++i)
a[i]=i;
do{
int sum=0;
for (int i=1;i<=n;++i)
sum+=d[a[i-1]][a[i]];
Sum+=d[a[n]][a[0]];
Ans=min (ans,sum);
} while (Next_permutation (a+1,a+1+n));
printf ("The shortest path has length%d\n", ans);
}
return 0;