Travelling Tom

这道题我是用Floyed做的。

题意是:tom需要环球旅行,第一行输入城市的个数,第二行输入要去的城市的序号,并要按顺序旅行。下面的矩阵即为每个城市之间的花费。题目要求最后回到起始点。

Travelling Tom
Time Limit: 1500ms, Special Time Limit:4000ms, Memory Limit:65366KB
Total submit users: 21, Accepted users: 19
Problem 12519 : No special judgement
Problem description
Tom is a used popsicle salesman. His recent sales at his stand on Glshaugen 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 figure 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 first 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
Judge Tips

 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 correct relative 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.

代码:

#include<iostream>
#define inf 5000000
using namespace std;
int d[201][201];
int a[201];
int n;
int main()
{
    int t,i,j,k,flag,sum;
    scanf("%d",&t);
    while(t--)
    {
      memset(a,0,sizeof(a));
      memset(d,0,sizeof(d));
      scanf("%d",&n);
      for(i=0;i<n;i++) scanf("%d",&a[i]);
      for(i=0;i<n;i++)
       for(j=0;j<n;j++)
       {
         scanf("%d",&d[i][j]);
         if(d[i][j]==-1) d[i][j]=inf;
       }
      for(k=0;k<n;k++)
       for(i=0;i<n;i++)
        for(j=0;j<n;j++)
        {
          if(d[i][j]>d[i][k]+d[k][j])
            d[i][j]=d[i][k]+d[k][j];
        }
       flag=0;
       sum=0;
     for(i=0;i<n;i++)
     {
      if(i<n-1)
      {
        sum+=d[a[i]][a[i+1]];
        if(d[a[i]][a[i+1]]==inf)
           flag=1;
      }
      else
      {
        sum+=d[a[i]][a[0]];
        if(d[a[i]][a[0]]==inf)
          {flag=1;}
      }
   }
   if(flag==1)
     printf("impossible\n");
   else
     printf("%d\n",sum);
  }
 return 0;
}


 


 

你可能感兴趣的:(Travelling Tom)