zoukankan      html  css  js  c++  java
  • UVA 1424 二 Salesmen

    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,

    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(AB) = $displaystyle sum^{{n}}_{{i=1}}$d (aibi)

    <tex2html_verbatim_mark>

    where

    d (ab) = $displaystyle left{vphantom{ egin{array}{cc} 0 & mbox{if } a=b \  1 & mbox{otherwise} end{array} }
ight.$$displaystyle egin{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<stdio.h>
     2 #include<string.h>
     3 #include<math.h>
     4 #include<vector>
     5 #include<algorithm>
     6 using namespace std;
     7 
     8 vector<int>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<edg[j].size();k++)
    53                 {
    54                     v=edg[j][k];
    55                     dp[i][j]=min(dp[i][j],dp[i-1][v]+1);
    56                 }
    57             }
    58             dp[i][path[i]]--;
    59         }
    60         int ans=999;
    61         for(i=1;i<=n;i++)
    62         {
    63             //printf("%d
    ",dp[2][i]);
    64             if(dp[l][i]<ans)
    65                 ans=dp[l][i];
    66         }
    67         printf("%d
    ",ans);
    68     }
    69     return 0;
    70 }
    View Code
  • 相关阅读:
    bzoj3223: Tyvj 1729 文艺平衡树
    bzoj1014: [JSOI2008]火星人prefix
    bzoj3231: [Sdoi2008]递归数列
    bzoj2282: [Sdoi2011]消防
    bzoj3195: [Jxoi2012]奇怪的道路
    成员内部类 局部内部类 匿名内部类
    静态代码块 构造代码块
    父类子类转换
    clone()方法
    后缀表达式求值
  • 原文地址:https://www.cnblogs.com/cyd308/p/4771571.html
Copyright © 2011-2022 走看看