Salesmen

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

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
 #include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std; vector<int>edg[];
int dp[][],path[]; int main()
{
int T;
int i,j,k;
int n,m,l;
int x,y;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m); for(j=;j<=n;j++)
{
dp[][j]=;
}
for(i=;i<=n;i++)
{
edg[i].clear();
edg[i].push_back(i);
} for(i=;i<=m;i++)
{
scanf("%d %d",&x,&y);
edg[x].push_back(y);
edg[y].push_back(x);
}
scanf("%d",&l);
for(i=;i<=l;i++)
{
scanf("%d",&path[i]);
} dp[][path[]]=;
for(i=;i<=l;i++)
{
for(j=;j<=n;j++)
{
int v=edg[j][];
dp[i][j]=dp[i-][v]+;
for(k=;k<edg[j].size();k++)
{
v=edg[j][k];
dp[i][j]=min(dp[i][j],dp[i-][v]+);
}
}
dp[i][path[i]]--;
}
int ans=;
for(i=;i<=n;i++)
{
//printf("%d\n",dp[2][i]);
if(dp[l][i]<ans)
ans=dp[l][i];
}
printf("%d\n",ans);
}
return ;
}

UVA 1424 二 Salesmen的更多相关文章

  1. 递推DP UVA 1424 Salesmen

    题目传送门 /* 题意:给定包含n个点的无向图和一个长度为L的序列,修改尽量少的点使得相邻的数字相同或连通 DP:状态转移方程:dp[i][j] = min (dp[i][j], dp[i-1][k] ...

  2. UVA题解二

    UVA题解二 UVA 110 题目描述:输出一个Pascal程序,该程序能读入不多于\(8\)个数,并输出从小到大排好序后的数.注意:该程序只能用读入语句,输出语句,if语句. solution 模仿 ...

  3. Problem W UVA 662 二十三 Fast Food

    Fast Food Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status P ...

  4. UVA 607 二十二 Scheduling Lectures

    Scheduling Lectures Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submi ...

  5. UVA 442 二十 Matrix Chain Multiplication

    Matrix Chain Multiplication Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %l ...

  6. UVA 590 二十一 Always on the run

     Always on the run Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

  7. uva 11178二维几何(点与直线、点积叉积)

    Problem D Morley’s Theorem Input: Standard Input Output: Standard Output Morley’s theorem states tha ...

  8. UVA 11019 二维匹配 AC自动机

    这个题目要求在一个大矩阵里面匹配一个小矩阵,是AC自动机的灵活应用 思路是逐行按普通AC自动机匹配,用过counts[i][j]记录一下T字符矩阵以i行j列为开头的与P等大的矩阵区域 有多少行已经匹配 ...

  9. UVA 10465 Homer Simpson(全然背包: 二维目标条件)

    UVA 10465 Homer Simpson(全然背包: 二维目标条件) http://uva.onlinejudge.org/index.php? option=com_onlinejudge&a ...

随机推荐

  1. 夺命雷公狗---微信开发55----微信js-sdk接口开发(2)接口功能介绍之签名算法

    我们JS-SDK里面其实有不少的接口 startRecord---录音 stopRecord---停止录音 playVoice---播放 pauseVoice---暂停播放 uploadImage-- ...

  2. 通过restore database时重命名数据库rename database

    backup database testdb to disk='c:\testdb_ful.bak' with compression backup log testdb to disk='c:\te ...

  3. 【fedora】制作安装u盘

    找一台安装好linux系统的PC,将下载的LiveCD ISO文件复制到硬盘(这样速度快),查看U盘挂载的位置(用磁盘工具),在终端中使用dd命令: $ sudo dd if=<Live ISO ...

  4. 轻量linux-Crunch bang

    主页地址:http://crunchbang.org crunch bang11昵称 wheezy crunchbang 11 基于 debian7

  5. yii2复选框

    Yii2复选框的具体使用方法如下,以商品中的品牌为例在页面显示 第一种方法:使用ActiveForm::checkBoxlist()(这种方法可以把后台获取到的数据都生成复选框),具体使用如下: &l ...

  6. 《OpenGL着色语言》理解点记录三

    “帧缓冲区”中的“帧”的含义?   “帧”是连续图像中的一幅,3D可视化程序最终都是转化为一幅幅的图像输出在显示器上,这一幅幅的图像叫做叫“帧”.   解释“glBlendFunc(GL_SRC_AL ...

  7. 【海岛帝国系列赛】No.3 海岛帝国:运输资源

    海岛帝国:运输资源 [试题描述] YSF考虑到“药师傅”帝国现在资源极度不平均,于是,商讨启用南水北调工程.YZM为首席工程师.现在,YSF由于工作紧张,准备军用物资和民用物资.但他要时时关注运输工程 ...

  8. NOIP201105铺地毯

    NOIP201105铺地毯 [问题描述]为了准备一个独特的颁奖典礼,组织者在会场的一片矩形区域(可看做是平面直角坐标系的第一象限)铺上一些矩形地毯.一共有n 张地毯,编号从1 到n.现在将这些地毯按照 ...

  9. linux设备驱动归纳总结(四):4.单处理器下的竞态和并发【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-67005.html linux设备驱动归纳总结(四):4.单处理器下的竞态和并发 xxxxxxxxxx ...

  10. 【python cookbook】【数据结构与算法】14.对不原生支持比较操作的对象排序

    问题:想在同一个类的实例之间做排序,但是它们并不原生支持比较操作. 解决方案:使用内建的sorted()函数可接受一个用来传递可调用对象的参数key,sorted利用该可调用对象返回的待排序对象中的某 ...