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 distancedist(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 设dp[i][j]是当前序列第i个数选择j的最小dis所以 dp[i][j] = min(dp[i][j],dp[i - 1][k] + (j != a[i])) k j 连通
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <iostream> using namespace std; const int MAX_N = ;
const int edge = ;
int N,M;
int a[MAX_N];
int dp1[MAX_N],dp2[MAX_N];
bool f[MAX_N][MAX_N];
int n; void solve() {
int *now = dp2,*last = dp1;
for(int i = ; i <= n; ++i) {
fill(now + ,now + N + ,n + );
for(int j = ; j <= N; ++j) {
int v = a[i] != j;
for(int k = ; k <= N; ++k) {
if(!f[j][k]) continue;
if(last[k] != n + )
now[j] = min(now[j],last[k] + v);
} }
swap(now,last);
} int ans = n + ;
//for(int i = 1; i <= N; ++i) printf("%d",last[i]);
//printf("\n");
for(int i = ; i <= N; ++i) ans = min(ans,last[i]);
printf("%d\n",ans);
} int main()
{
// freopen("sw.in","r",stdin);
int t;
scanf("%d",&t);
while(t--) {
scanf("%d%d",&N,&M); memset(dp1,,sizeof(dp1));
memset(dp2,,sizeof(dp2));
memset(f,,sizeof(f));
for(int i = ; i <= N; ++i) f[i][i] = ; for(int i = ; i < M; ++i) {
int u,v;
scanf("%d%d",&u,&v);
f[u][v] = f[v][u] = ; }
scanf("%d",&n);
for(int i = ; i <= n; ++i) {
scanf("%d",&a[i]);
} solve(); } return ;
}
 

LA 4256的更多相关文章

  1. LA 4256 商人

    题目链接:https://vjudge.net/contest/160916#problem/B 题意:给一个无向图,和一个序列:要求,在这个序列中,两两相连的两个数相同,或者,在无向图中相邻:(n& ...

  2. LA 4256 DP Salesmen

    d(i, j)表示使前i个数满足要求,而且第i个数值为j的最小改动次数. d(i, j) = min{ d(i-1, k) | k == j | G[j][k] } #include <cstd ...

  3. leggere la nostra recensione del primo e del secondo

    La terra di mezzo in trail running sembra essere distorto leggermente massima di recente, e gli aggi ...

  4. Le lié à la légèreté semblait être et donc plus simple

    Il est toutefois vraiment à partir www.runmasterfr.com/free-40-flyknit-2015-hommes-c-1_58_59.html de ...

  5. Mac Pro 使用 ll、la、l等ls的别名命令

    在 Linux 下习惯使用 ll.la.l 等ls别名的童鞋到 mac os 可就郁闷了~~ 其实只要在用户目录下建立一个脚本“.bash_profile”, vim .bash_profile 并输 ...

  6. Linux中的动态库和静态库(.a/.la/.so/.o)

    Linux中的动态库和静态库(.a/.la/.so/.o) Linux中的动态库和静态库(.a/.la/.so/.o) C/C++程序编译的过程 .o文件(目标文件) 创建atoi.o 使用atoi. ...

  7. Mac OS使用ll、la、l等ls的别名命令

    在linux下习惯使用ll.la.l等ls别名的童鞋到mac os可就郁闷了-- 其实只要在用户目录下建立一个脚本“.bash_profile”,并输入以下内容即可: alias ll='ls -al ...

  8. .Uva&LA部分题目代码

    1.LA 5694 Adding New Machine 关键词:数据结构,线段树,扫描线(FIFO) #include <algorithm> #include <cstdio&g ...

  9. 获取在线人数 CNZZ 和 51.la

    string Cookies = string.Empty; /// <summary> /// 获取在线人数 (51.la统计器) /// </summary> /// &l ...

随机推荐

  1. Python 以正确的宽度在盒子中居中打印一个字符

    注意://为整除的意思 代码: # -*- coding:UTF-8 -*- sentence = input("Sentence:") screen_width = 80 tex ...

  2. go语言包与包引用

    go语言中包(package)与java中的包(package)非常类似,都是组织代码的方式,而且都和磁盘上的目录结构存在对应关系. go语言中,包名一般为go代码所在的目录名,但是与java不同的是 ...

  3. 网络爬虫by pluskid

    网络爬虫(Web Crawler, Spider)就是一个在网络上乱爬的机器人.当然它通常并不是一个实体的机器人,因为网络本身也是虚拟的东西,所以这个“机器人”其实也就是一段程序,并且它也不是乱爬,而 ...

  4. UISegmentedControl swift

    // // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...

  5. VS2010出现灾难性错误的解决办法

    VS2010出现灾难性错误的解决办法   之前本人利用VS2010 在编写一个基于对话框的程序的时候,要在对话框类C-.DLG中添加函数,右键点击类向导,此时界面上弹出一个消息框,告知出现灾难性事故, ...

  6. Github for Windows使用图文教程

    原文:http://www.cr173.com/html/15618_1.html Git已经变得非常流行,连Codeplex现在也已经主推Git.Github上更是充斥着各种高质量的开源项目,比如r ...

  7. shell基本语法备忘

    1.第一行要写明shell种类 #!/bin/bash   2.打印输出 #!/bin/bashecho "Hello World !~"   3.变量定义 变量=前后不能有空格, ...

  8. cocos2dx 2.0+ 版本,IOS6.0+设置横屏

    使用cocos2dx 自带的xcode模板,是不能正常的设置为横屏的. 一共修改了三个地方: 在项目属性中:Deployment Info中,勾选上 Landscape left,以及 Landsca ...

  9. 如何写一个漂亮的Liferay Theme 6.2

    只要你看到的.想做出来的页面,都可以通过liferay theme来实现,至于具体实现凡方式,那就见仁见智了. 下面,我将介绍如何快速地建一个简单漂亮的liferay theme. 工具:lifera ...

  10. C#制作高仿360安全卫士窗体<一>

    开始写这一系列博客之前先要向大家说声抱歉,放肆雷特建立很久却很少有更新.当然博客人气也不旺,大部分都是看的人多评论收藏的人少.一直想要改变这种状态,正好赶上了最近工作上做了一个高仿360安全卫士窗体. ...