Traveling salesmen of nhn. (the prestigious Korean internet company) report their current location to the company on a regular basis. They also have to report their new location to the company if they are moving to another location. The company keep each salesman's working path on a map of his working area and uses this path information for the planning of the next work of the salesman. The map of a salesman's working area is represented as a connected and undirected graph, where vertices represent the possible locations of the salesman an edges correspond to the possible movements between locations. Therefore the salesman's working path can be denoted by a sequence of vertices in the graph. Since each salesman reports his position regularly an he can stay at some place for a very long time, the same vertices of the graph can appear consecutively in his working path. Let a salesman's working path be correct if two consecutive vertices correspond either the same vertex or two adjacent vertices in the graph.

For example on the following graph representing the working area of a salesman,

<tex2html_verbatim_mark>

a reported working path [1 2 2 6 5 5 5 7 4] is a correct path. But a reported working path [1 2 2 7 5 5 5 7 4] is not a correct path since there is no edge in the graph between vertices 2 a 7. If we assume that the salesman reports his location every time when he has to report his location (but possibly incorrectly), then the correct path could be [1 2 2 4 5 5 5 7 4], [1 2 4 7 5 5 5 7 4], or [1 2 2 6 5 5 5 7 4].

The length of a working path is the number of vertices in the path. We define the distance between two pathsA = a1a2...an <tex2html_verbatim_mark>and B = b1b2...bn <tex2html_verbatim_mark>of the same length n <tex2html_verbatim_mark>as

dist(AB) = d (aibi)

<tex2html_verbatim_mark>

where

d (ab) = 

<tex2html_verbatim_mark>

Given a graph representing the working area of a salesman and a working path (possible not a correct path),A <tex2html_verbatim_mark>, of a salesman, write a program to compute a correct working path, B <tex2html_verbatim_mark>, of the same length where the distance dist(AB) <tex2html_verbatim_mark>is minimized.

Input

The program is to read the input from standard input. The input consists of T <tex2html_verbatim_mark>test cases. The number of test cases (T) <tex2html_verbatim_mark>is given in the first line of the input. The first line of each test case contains two integers n1<tex2html_verbatim_mark>, n2 <tex2html_verbatim_mark>(3n1100, 2n24, 950) <tex2html_verbatim_mark>where n1 <tex2html_verbatim_mark>is the number of vertices of the graph representing the working map of a salesman and n2 <tex2html_verbatim_mark>is the number of edges in the graph. The input graph is a connected graph. Each vertex of the graph is numbered from 1 to n1 <tex2html_verbatim_mark>. In the following n2 <tex2html_verbatim_mark>lines, each line contains a pair of vertices which represent an edge of the graph. The last line of each test case contains information on a working path of the salesman. The first integer n <tex2html_verbatim_mark>(2n200) <tex2html_verbatim_mark>in the line is the length of the path and the following n integers represent the sequence of vertices in the working path.

Output

Your program is to write to standard output. Print one line for each test case. The line should contain the minimum distance of the input path to a correct path of the same length.

Sample Input

2
7 9
1 2
2 3
2 4
2 6
3 4
4 5
5 6
7 4
7 5
9 1 2 2 7 5 5 5 7 4
7 9
1 2
2 3
2 4
2 6
3 4
4 5
5 6
7 4
7 5
9 1 2 2 6 5 5 5 7 4

Sample Output

1
0
这题说的是给了一个图,如上,一个人从图中的任意位置出发,总共返回了n次自己的位置,这些位置可能是不合法的也就是所两点之间不相邻但是他们却在返回的点中相邻,返回的点中允许有相同的点相邻,(表明他也一直在这个点上),
输入 给n个点,表示返回的n个点。
输出 找出一条路径同样拥有n个点(合法的),使得这两序列的最长公共子序列最长,输出差异的几个数
先用floyd 求出任意点之间的距离,然后dp[i] 表示到i为止合法的最小差异, 初始化的是dp[i]=i;
然后 当他们的距离小于等于他们在序列中出现的顺序的时候 dp[j]=min(dp[j],dp[i]+j-i-1);
输出dp[n-1]
#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn=;
const int INF =;
int dist[maxn][maxn];
int sa[maxn*];
int dp[maxn*];
void inti(int n1){
for(int k=; k<=n1; ++k)
for(int i=; i<=n1; ++i)
for(int j=; j<=n1; ++j)
if(dist[i][k]<INF&&dist[k][j]<INF)
dist[i][j]=dist[i][j]<(dist[i][k]+dist[k][j])?dist[i][j]:(dist[i][k]+dist[k][j]); }
int main()
{
int cas;
scanf("%d",&cas);
while(cas--){
int n1,n2;
scanf("%d%d",&n1,&n2);
for(int i=; i<=n1; i++)
for(int j=; j<=n1; ++j )
dist[i][j]=i==j?:INF;
for(int i=; i<n2; ++i){
int a,b;
scanf("%d%d",&a,&b);
dist[a][b]=dist[b][a]=;
}
inti(n1);
int n;
scanf("%d",&n);
for(int i=; i<n; ++i)
scanf("%d",&sa[i]);
for(int i=; i<n; ++i)
dp[i]=i;
for(int i=; i<n; ++i)
for( int j=i+; j<n; ++j)
if(dist[ sa[i] ][ sa[j] ]<=j-i)
dp[j]=min(dp[j], dp[i]+j-i-);
printf("%d\n",dp[n-]);
}
return ;
}

uva1424的更多相关文章

  1. UVa1424–Salesmen(DP)

    题目大意 给定一个包含n(n<=100)个点的无向连通图和一个长度为L的序列A(L<=200),你的任务是修改尽量少的数,使得序列中的任意两个相邻的数或者相同,或者对应图中两个相邻结点 题 ...

随机推荐

  1. hasattr() 、getattr() 、setattr()

    hasattr(object, name) :用于判断一个对象中是否有指定的属性或方法,如果存在返回 True,否则返回 False getattr(object, name, [default]) ...

  2. C语言分支结构之if else语句

    前面我们看到的代码都是顺序执行的,也就是先执行第一条语句,然后是第二条.第三条……一直到最后一条语句,这称为顺序结构. 但是对于很多情况,顺序结构的代码是远远不够的,比如一个程序限制了只能成年人使用, ...

  3. Android 监听按钮的点击事件

    onClick事件1.Button和ImageButton都拥有一个onClick事件 通过自身的.setOnClickListener(OnClickListener)方法添加点击事件2.所有的控件 ...

  4. C/C++对Lu系统内置动态对象进行运算符重载

    欢迎访问Lu程序设计 C/C++对Lu系统内置动态对象进行运算符重载 1 说明 要演示本文的例子,你必须下载Lu32脚本系统.本文的例子需要lu32.dll.lu32.lib.C格式的头文件lu32. ...

  5. java高级---->Thread之Condition的使用

    Condition 将 Object 监视器方法(wait.notify 和 notifyAll)分解成截然不同的对象,以便通过将这些对象与任意 Lock 实现组合使用,为每个对象提供多个等待 set ...

  6. [SCOI2008] 着色方案[高维dp]

    321. [SCOI2008] 着色方案 ★★★   输入文件:color.in   输出文件:color.out   简单对比时间限制:1 s   内存限制:64 MB 题目背景: 有n个木块排成一 ...

  7. 问答项目---账号密码异步校验后进行PHP校验

    在做登陆的时候,通过异步校验后还需要通过PHP来校验账号和密码的正确性. PHP校验账号密码: public function login(){ if(!IS_POST){echo "页面不 ...

  8. linux中增加swap分区文件的步骤方法

     一.swap交换分区 Swap分区在系统的物理内存不够用的时候,把硬盘空间中的一部分空间释放出来,以供当前运行的程序使用.那些被释放的空间可能来自一些很长时间没有什么操作的程序,这些被释放的空间被临 ...

  9. move_uploaded_file() 函数

    定义和用法 move_uploaded_file() 函数将上传的文件移动到新位置. 若成功,则返回 true,否则返回 false. 语法 move_uploaded_file(file,newlo ...

  10. oracle goldengate安装

    1.ftp工具上传ogg112101_fbo_ggs_Linux_x64_ora11g_64bit.zip分别到source和target服务器 [oracle@localhost mnt]$ ll ...