-
Notifications
You must be signed in to change notification settings - Fork 2
/
UVA 01424 - Salesmen.cpp
67 lines (54 loc) · 1.27 KB
/
UVA 01424 - Salesmen.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <bits/stdc++.h>
using namespace std;
#define inf 1000000000
#define ii pair<int, int>
#define vi vector<int>
#define vii vector< vector<int> >
#define mod 1000000007
vii adj;
int in[200], p, memo[200][100];
int dp(int u, int last)
{
if(u == p) return 0;
int &ret = memo[u][last];
if(ret != -1) return ret;
int temp, x = 1;
if(in[u] == last) x = 0;
ret = dp(u + 1, last) + x;
for(int i=0; i<adj[last].size(); i++){
temp = dp(u + 1, adj[last][i]);
if(in[u] != adj[last][i]) temp++;
if(ret > temp) ret = temp;
}
return ret;
}
int main()
{
int t;
scanf("%d", &t);
while(t--){
int n, m;
scanf("%d%d", &n, &m);
adj.assign(n, vi(0) );
int x, y;
for(int i=0; i<m; i++){
scanf("%d%d", &x, &y);
x--; y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
scanf("%d", &p);
for(int i=0; i<p; i++){
scanf("%d", &in[i]);
in[i]--;
}
memset(memo, -1, sizeof memo);
int mn = inf, temp;
for(int i=0; i<n; i++){
temp = dp(1, i);
if(in[0] != i) temp++;
if(mn > temp) mn = temp;
}
printf("%d\n", mn);
}
}