最短路径问题

                                                                                    Travelling Tom

Tom is a used popsicle salesman. His recent sales at his stand on Gloshaugen haven't been very good, so he's decided that he wants to travel the world selling his merchandise. He has already compiled a list of cities to visit, and have also gathered the costs of the plane trips between them. Now he just have to gure out if he has enough money for the trip. He has of course heard that this travelling salesman problem is really really hard, so the task is down to the smartest people he knows. That's you (if you don't feel very smart at the moment, keep in mind that Tom doesn't know that many people). You need to write a program that computes the lowest cost of Tom visiting all these cities in the given order.

Input

The first line of input contains a single number T, the number of test cases to follow. Each test case begins with a line containing a single number, N, the number of cities Tom will visit. The second line of each test case contains N numbers ai, the order in which Tom wants to visit the N cities. He starts his trip in the rst city described, and will travel back there once he has visited the last one. Then follow N lines, each containing N integers, describing the cost cij of travelling from city i to each of the N cities.

Output

For each test case, output a line containing a single number, the lowest possible cost of visiting the cities. If it is not possible to visit all the cities, output impossible instead.

Sample Input

2
3
0 2 1
0 1 2
1 0 1
1 3 0
2
0 1
0 -1
1 0

Sample Output

5
impossible

Hint

  • 0 < T <= 100
  • 0 < N <= 200
  • 0 <= ai < N
  • -1 <= cij <= 10000
  • The list of cities visited will always be a permutation of the numbers from 0 to N - 1, inclusive.
  • The cost of travelling from a city to itself is always 0.
  • If and only if there is no plane going from city i to city j, then the j-th number of the i-th line of costs will be -1.
  • Note that Tom is returning to the starting city at the end of the tour.
  • Cities can be visited several times, but will only count when (also) in the correctrelative order. For the order 1 0 2, for instance, 1 2 0 2 1 is a valid tour, while 1 2 0 1 is not.

Source

#include<iostream>
#include
<stdio.h>
#include
<cstring>
using namespace std;
#define INF 5000000
int a[201];
int c[201][201];
int d[201][201];

int main()
{
int n;
int i,j,m,kk,k,p;
int max1;

scanf(
"%d",&n);
int l;
for(l=1;l<=n;l++)
{ memset(a,
0,sizeof(a));
memset(c,
0,sizeof(c));
memset(d,
0,sizeof(d));
scanf(
"%d",&m);
for( i=0;i<m;i++)
scanf(
"%d",&a[i]);
for(i=0;i<m;i++)
for(j=0;j<m;j++)
{
scanf(
"%d",&d[i][j]);
if(d[i][j]==-1)
d[i][j]
=INF;
}
max1
=0;
for(k=0;k<m;k++)
for(i=0;i<m;i++)
for(j=0;j<m;j++)
{

if(d[i][j]>(d[i][k]+d[k][j]))
d[i][j]
=d[i][k]+d[k][j];
}
kk
=0;

for(p=0;p<m;p++)
{

if(p<m-1)
{
max1
+=d[a[p]][a[p+1]];
if(d[a[p]][a[p+1]]==INF)
kk
=1;
}
else
{
max1
+=d[a[p]][a[0]];
if(d[a[p]][a[0]]==INF)
{kk
=1;}
}

}
if(kk==1)
printf(
"impossible\n");
else
printf(
"%d\n",max1);
}
return 0;
}
posted @ 2011-05-16 23:31  Crazy_yiner  阅读(396)  评论(0编辑  收藏  举报