Salesmen LA4256 Seoul2008


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>   
#include <map>
#include <string>  
#include <climits> 
#include <set>
#include <string> 
#include <sstream>
#include <utility>
#include <ctime>
 
using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::istringstream;
using std::make_pair;
using std::greater;

const int MAXN(110);
const int INFI((INT_MAX-1) >> 1);

bool mp[MAXN][MAXN];
int arr[210];
int table[210][MAXN];

int main()
{
	int T;
	scanf("%d", &T);
	while(T--)
	{
		int n1, n2, n3;
		scanf("%d%d", &n1, &n2);
		memset(mp+1, 0, sizeof(mp[0])*n1);
		int tu, tv;
		for(int i = 0; i < n2; ++i)
		{
			scanf("%d%d", &tu, &tv);
			mp[tu][tv] = true;
			mp[tv][tu] = true;
		}
		for(int i = 1; i <= n1; ++i)
			mp[i][i] = true;
		scanf("%d", &n3);
		for(int i = 1; i <= n3; ++i)
			scanf("%d", arr+i);
		for(int i = 1; i <= n1; ++i)
			table[1][i] = 1;
		table[1][arr[1]] = 0;
		for(int i = 2; i <= n3; ++i)
		{
			for(int j = 1; j <= n1; ++j)
				table[i][j] = 1;
			table[i][arr[i]] = 0;
			for(int j = 1; j <= n1; ++j)
			{
				int temp = INFI;
				for(int k = 1; k <= n1; ++k)
					if(mp[j][k])
						temp = min(temp, table[i-1][k]);
				table[i][j] += temp;
			}
		}
		int ans = INFI;
		for(int i = 1; i <= n1; ++i)
			ans = min(ans, table[n3][i]);
		printf("%d\n", ans);
	}
	return 0;
}


你可能感兴趣的:(Salesmen LA4256 Seoul2008)