博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA 1424 二 Salesmen
阅读量:6930 次
发布时间:2019-06-27

本文共 4587 字,大约阅读时间需要 15 分钟。

Salesmen

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

Submit     

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,

 

\epsfbox{p4256.eps}<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(
A
B) = 
$\displaystyle \sum^{​{n}}_{​{i=1}}$
d (
a
i
b
i)

<tex2html_verbatim_mark>

where

 

d (
a
b) = 
$\displaystyle \left\{\vphantom{ \begin{array}{cc} 0 & \mbox{if } a=b \\  1 & \mbox{otherwise} \end{array} }\right.$
$\displaystyle \begin{array}{cc} 0 & \mbox{if } a=b \\  1 & \mbox{otherwise} \end{array}$

<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>(3$ \le$n1$ \le$100, 2$ \le$n2$ \le$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$ \le$n$ \le$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
1 #include
2 #include
3 #include
4 #include
5 #include
6 using namespace std; 7 8 vector
edg[1005]; 9 int dp[1005][1005],path[2005];10 11 12 int main()13 {14 int T;15 int i,j,k;16 int n,m,l;17 int x,y;18 scanf("%d",&T);19 while(T--)20 {21 scanf("%d %d",&n,&m);22 23 for(j=1;j<=n;j++)24 {25 dp[1][j]=1;26 }27 for(i=1;i<=n;i++)28 {29 edg[i].clear();30 edg[i].push_back(i);31 }32 33 for(i=1;i<=m;i++)34 {35 scanf("%d %d",&x,&y);36 edg[x].push_back(y);37 edg[y].push_back(x);38 }39 scanf("%d",&l);40 for(i=1;i<=l;i++)41 {42 scanf("%d",&path[i]);43 }44 45 dp[1][path[1]]=0;46 for(i=2;i<=l;i++)47 {48 for(j=1;j<=n;j++)49 {50 int v=edg[j][0];51 dp[i][j]=dp[i-1][v]+1;52 for(k=1;k
View Code

 

转载于:https://www.cnblogs.com/cyd308/p/4771571.html

你可能感兴趣的文章
hdu 1496(hash经典)
查看>>
javascript转换日期字符串为Date对象
查看>>
javascript 兼容不同手机的 canvas
查看>>
一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少?
查看>>
方法javaJVM学习笔记-内存处理
查看>>
esxtop 指标%RDY,NUMA,Wide-VMs
查看>>
android官方资料
查看>>
贴片电感和贴片磁珠的区别
查看>>
ArcGIS与SuperMap的使用比较(1)
查看>>
vcenter SSO
查看>>
Android开发1——查找所需要出示权限的内容
查看>>
C#/Net代码精简优化技巧
查看>>
HDUOJ------(1272)小希的迷宫
查看>>
wget
查看>>
.net框架
查看>>
如何查看自己电脑系统的安装日期-Window上
查看>>
MongoDB与传统数据库的使用区别——批量插入与批量查询
查看>>
【Java】IO Stream详细解读
查看>>
Linux C 读取文件夹下所有文件(包括子文件夹)的文件名
查看>>
在oracle下如何创建database link全面总结
查看>>