UVALIVE 4256 Salesmen
Salesmen
This problem will be judged on UVALive. Original ID: 4256
64-bit integer IO format: %lld Java class name: Main
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,

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 paths A = a1a2...an <tex2html_verbatim_mark>and B = b1b2...bn<tex2html_verbatim_mark>of the same length n <tex2html_verbatim_mark>as

where


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(A, B) <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>(3n1
100, 2
n2
4, 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>(2
n
200) <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
Source
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cmath>
- #include <algorithm>
- #include <climits>
- #include <vector>
- #include <queue>
- #include <cstdlib>
- #include <string>
- #include <set>
- #include <stack>
- #define LL long long
- #define pii pair<int,int>
- #define INF 0x3f3f3f3f
- using namespace std;
- const int maxn = ;
- bool g[maxn][maxn];
- int dp[][maxn];
- int main() {
- int t,n,m,p,u,v;
- scanf("%d",&t);
- while(t--) {
- memset(g,false,sizeof(g));
- scanf("%d %d",&n,&m);
- for(int i = ; i < m; ++i) {
- scanf("%d %d",&u,&v);
- g[u][v] = g[v][u] = true;
- }
- for(int i = ; i <= n; ++i) dp[][i] = ;
- scanf("%d %d",&p,&u);
- dp[][u] = ;
- for(int i = ; i < p; ++i) {
- scanf("%d",&u);
- for(int j = ; j <= n; ++j) {
- dp[i][j] = INF;
- int cost = u == j?:;
- for(int k = ; k <= n; ++k) {
- if(g[k][j] || k == j)
- dp[i][j] = min(dp[i][j],dp[i-][k]+cost);
- }
- }
- }
- int ans = INF;
- for(int i = ; i <= n; ++i)
- ans = min(ans,dp[p-][i]);
- cout<<ans<<endl;
- }
- return ;
- }
UVALIVE 4256 Salesmen的更多相关文章
- UVaLive 4256 Salesmen (简单DP)
题意:给一个无向连通图,和一个序列,修改尽量少的数,使得相邻两个数要么相等,要么相邻. 析:dp[i][j] 表示第 i 个数改成 j 时满足条件.然后就很容易了. 代码如下: #pragma com ...
- 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 ...
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- 思维 UVALive 3708 Graveyard
题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...
- UVALive 6145 Version Controlled IDE(可持久化treap、rope)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVA 1424 二 Salesmen
Salesmen Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Pr ...
- UVALive 6508 Permutation Graphs
Permutation Graphs Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
随机推荐
- DynaActionForm(动态ActionForm)的使用
在struts中利用DynaActionForm(动态ActionForm)可以节省代码的编写. 1.在struts-config.xml中配置DynaActionForm:加入这个Form中有三个属 ...
- 字节与字符_字节流与字符流_ASCII与Unicode_GB2312_GBK_GB18030_BIG-5
字节(Byte):通常将可表示经常使用英文字符8位二进制称为一字节. 一个英文字母(不分大写和小写)占一个字节的空间,一个中文汉字占两个字节的空间. 符号:英文标点2占一个字节,中文标点占两个字节. ...
- 通达OA 小飞鱼工作流在线培训教程(一)HTML基础介绍
应一些刚接触工作流设计朋友的要求,这里开设一个系列教程,对通达OA工作流设计相关的内容做个介绍.方便解决一些日常经常出现的问题,希望对刚刚接触这部分工作的朋友能够有些帮助. 工作流设计须要多方面的知识 ...
- linux删除多行
光标放到行dd:删除所在行 光标放到行Ndd: 删除所在行下的N行
- 0x12 队列
感觉和栈那一讲一样简单啊.. poj2259 这个乱搞硬模拟吧...没更新last WA了N次...(叫你懒得写拍) #include<cstdio> #include<iostre ...
- vue.js的学习之路
因为对jquery的ajax渲染很不满,所以我就来学vue.js了 1)vue.js是什么 官方解释为:Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型 ...
- Winform WPF 窗体显示位置
WinForm 窗体显示位置 窗体显示的位置首先由窗体的StartPosition决定,FormStartPosition这个枚举值由如下几种情况 // 摘要: // 窗体的位置由 System.Wi ...
- getElementsByClassName 方法兼容性封装方法二
var getElmsByClsName = function(className, results) { results = results || []; // 判断浏览器是否支持 getEleme ...
- Android TabLayout+ViewPager禁止滑动
1.重写ViewPager并重写覆盖ViewPager的onInterceptTouchEvent(MotionEvent arg0)方法和onTouchEvent(MotionEvent arg0) ...
- TortoiseSVN—Repo-browser
TortoiseSVN—Repo-browser,打开你要比较的两个版本所在的地址,选择一个版本做为比较的基础(单击右键—选择mark for comparison),再选择另外一个版本(单击右键—选 ...