题意:
给出一张图,然后给出一个序列,修改序列中一些数字,要求使这个序列相邻的两个点.要么是相同的点,要么在图中是相邻点;
思路:
dp[i][j]代表序列前i个,并且第i个的值是j ,满足这一关系,最少修改几次几次;
那么dp[i][j] = min ( dp[i - 1][k]) (k与j是相连的);
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int N = 105;
int INF = 0x3f3f3f3f;
int n1,n2,l;
int suq[2 * N];
int dp[2 * N][N];
vector<int> v[N];
int main(){
int t;
scanf("%d",&t);
while(t--) {
memset(dp, 0 ,sizeof(dp));
scanf("%d%d",&n1,&n2);
for(int i = 0; i <= n1; i++) {
v[i].clear();
v[i].push_back(i);
}
int x,y;
for(int i = 0; i < n2; i++) {
scanf("%d%d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
scanf("%d",&l);
for(int i = 1; i <= l; i++) {
scanf("%d",&suq[i]);
}
for(int i = 1; i <= l; i++) {
for(int j = 1; j <= n1; j++) {
dp[i][j] = INF;
for(int k = 0; k < v[j].size(); k++) {
if(j == suq[i])
dp[i][j] = min(dp[i][j], dp[i - 1][v[j][k]]);
else
dp[i][j] = min(dp[i][j], dp[i - 1][v[j][k]] + 1);
}
}
}
int m = INF;
for(int i = 1; i <= n1; i++) {
m = min(m, dp[l][i]);
}
printf("%d\n",m);
}
return 0;
}