LA 4256

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 连通

 1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <iostream>
5
6 using namespace std;
7
8 const int MAX_N = 205;
9 const int edge = 5000;
10 int N,M;
11 int a[MAX_N];
12 int dp1[MAX_N],dp2[MAX_N];
13 bool f[MAX_N][MAX_N];
14 int n;
15
16 void solve() {
17 int *now = dp2,*last = dp1;
18 for(int i = 1; i <= n; ++i) {
19 fill(now + 1,now + N + 1,n + 1);
20 for(int j = 1; j <= N; ++j) {
21 int v = a[i] != j;
22 for(int k = 1; k <= N; ++k) {
23 if(!f[j][k]) continue;
24 if(last[k] != n + 1)
25 now[j] = min(now[j],last[k] + v);
26 }
27
28 }
29 swap(now,last);
30 }
31
32 int ans = n + 1;
33 //for(int i = 1; i <= N; ++i) printf("%d",last[i]);
34 //printf("\n");
35 for(int i = 1; i <= N; ++i) ans = min(ans,last[i]);
36 printf("%d\n",ans);
37 }
38
39 int main()
40 {
41 // freopen("sw.in","r",stdin);
42 int t;
43 scanf("%d",&t);
44 while(t--) {
45 scanf("%d%d",&N,&M);
46
47 memset(dp1,0,sizeof(dp1));
48 memset(dp2,0,sizeof(dp2));
49 memset(f,0,sizeof(f));
50 for(int i = 1; i <= N; ++i) f[i][i] = 1;
51
52 for(int i = 0; i < M; ++i) {
53 int u,v;
54 scanf("%d%d",&u,&v);
55 f[u][v] = f[v][u] = 1;
56
57 }
58 scanf("%d",&n);
59 for(int i = 1; i <= n; ++i) {
60 scanf("%d",&a[i]);
61 }
62
63 solve();
64
65 }
66
67 return 0;
68 }


LA 4256,布布扣,bubuko.com

时间: 2024-12-15 08:56:44

LA 4256的相关文章

LA 4256 Salesmen 线性dp

// LA 4256 Salesmen 线性dp // // 像LCS和LIS问题类似,因为每次修改一个值,都是根据 // 前一个值决定的,那么最后一个结尾的数字肯定要作为 // 状态,而长度作为状态是一目了然的 // // d[i][j]表示长度为i,最后以j结尾的数组修改的最小次数 // // 则状态转移方程为 // // d[i][j] = min(d[i][j],d[i-1][k]+(j,k是否相同或者相邻?0:1)); // // 个人感觉还是比较明显的,最后的答案就是min(d[L]

LA 4256 DP Salesmen

d(i, j)表示使前i个数满足要求,而且第i个数值为j的最小改动次数. d(i, j) = min{ d(i-1, k) | k == j | G[j][k] } 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 const int maxn = 200 + 10; 7 8 int n, m, k; 9 int a[maxn]; 10 i

HDU 4256 The Famous Clock

The Famous Clock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1399    Accepted Submission(s): 940 Problem Description Mr. B, Mr. G and Mr. M are now in Warsaw, Poland, for the 2012's ACM-ICPC

hdu 5745 la vie en rose

这道题的官方题解是dp,但是可以暴力出来.改天再研究怎么dp. 暴力的时候,如果计算sum的时候,调用strlen函数会超时,可见这个函数并不是十分的好.以后能不用尽量不用. La Vie en rose Time Limit: 14000/7000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 861    Accepted Submission(s): 461 Problem

让MAC OS也能使用LL LA L等LS的别名

linux下默认ll是ls -l的别名.OS X下默认不支持.习惯了linux下使用ll,我们同样也可以将习惯搬到os x下的shell中. 再当前用户家目录下新建.bash_profile文件.根据你的习惯,添加下面格式内容即可. 1 2 3 alias ll='ls -l' alias la='ls -a' alias l='ls -la' 然后执行:source .bash_profile你还可以添加你喜欢的其他别名.

LA 3942 Remember the Word (Trie)

Remember the Word 题目:链接 题意:给出一个有S个不同单词组成的字典和一个长字符串.把这个字符串分解成若干个单词的连接(单词可以重复使用),有多少种方法? 思路:令d[i]表示从字符i开始的字符串(后缀s[i..L])的分解数,这d[i] = sum{d(i+len(x)) | 单词x是其前缀}.然后将所有单词建成一个Trie树,就可以将搜索单词的复杂度降低. 代码: #include<map> #include<set> #include<queue>

LA 2678 Subsequence

有一个正整数序列,求最短的子序列使得其和大于等于S,并输出最短的长度. 用数组b[i]存放序列的前i项和,所以b[i]是递增的. 遍历终点j,然后在区间[0, j)里二分查找满足b[j]-b[i]≥S的最大的i,时间复杂度为O(nlongn). 这里二分查找用到库函数lower_bound() 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #inclu

LA 4127 - The Sky is the Limit (离散化 扫描线 几何模板)

题目链接 非原创 原创地址:http://blog.csdn.net/jingqi814/article/details/26117241 题意:输入n座山的信息(山的横坐标,高度,山底宽度),计算他们的轮廓线, 即露出来的表面边长,有些山是重叠的不计.空白地带不计,每座山都是等腰三角形. 分析:大白书P414页. 求小山的总长度,用一些虚线将其离散化,分成一段一段的,特征点:山脚,山顶,交点.这样就能保 证相邻两个扫描点之间再无交点.然后一最上面的点就是分割点,维护上一个点lastp即可. 1

LA 2031

Mr. White, a fat man, now is crazy about a game named ``Dance, Dance, Revolution". But his dance skill is so poor that he could not dance a dance, even if he dances arduously every time. Does ``DDR" just mean him a perfect method to squander his