• UVa 4256 Salesmen dp


    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}

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

    dist(AB) = $\displaystyle \sum^{{n}}_{{i=1}}$d (aibi)

    where

    d (ab) = $\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}$

    Given a graph representing the working area of a salesman and a working path (possible not a correct path), A, of a salesman, write a program to compute a correct working path, B , of the same length where the distancedist(AB) is minimized.

    Input 

    The program is to read the input from standard input. The input consists of T test cases. The number of test cases (T) is given in the first line of the input. The first line of each test case contains two integers n1n2 (3$ \le$n1$ \le$100, 2$ \le$n2$ \le$4, 950) where n1 is the number of vertices of the graph representing the working map of a salesman and n2 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 . In the following n2 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 (2$ \le$n$ \le$200) 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
    ---------------------------------------------

    -_-|| 用了10秒钟想出公式,用了1小时调程序。。。

    f[i][j]表示第i个数为j时的最小花费

    f[i][j]=min( f[i-1][k] ) ( k,j间有路径或相等且j等于a[i] ) 

    f[i][j]=min( f[i-1][k]+1 )  ( k,j间有路径或相等 )

    -------------------------------------------

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    
    const int maxn=555;
    const int OO=1e9;
    
    int T;
    int n1,n2;
    bool path[maxn][maxn];
    int f[maxn][maxn];
    int a[maxn];
    int n;
    
    int main()
    {
        scanf("%d",&T);
        while (T--)
        {
            memset(path,0,sizeof(path));
            memset(f,-1,sizeof(f));
            scanf("%d%d",&n1,&n2);
            for (int i=1;i<=n2;i++)
            {
                int x,y;
                scanf("%d%d",&x,&y);
                path[x][y]=1;
                path[y][x]=1;
            }
            for (int i=1;i<=n1;i++) path[i][i]=1;
            scanf("%d",&n);
            for (int i=1;i<=n;i++) scanf("%d",&a[i]);
            for (int i=0;i<=n1;i++)
            {
                f[0][i]=0;
            }
            for (int i=1;i<=n;i++)
            {
                for (int j=1;j<=n1;j++)
                {
                    for (int k=1;k<=n1;k++)
                    {
                        if (a[i]==j)
                        {
                            if (path[k][j] && f[i-1][k]!=-1 && (f[i-1][k]<f[i][j]||f[i][j]==-1))
                                f[i][j]=f[i-1][k];
                        }
                        else
                        {
                            if (path[k][j] && f[i-1][k]!=-1 && (f[i-1][k]+1<f[i][j]||f[i][j]==-1))
                                f[i][j]=f[i-1][k]+1;
                        }
                    }
                }
            }
            //for (int i=1;i<=n;i++)for (int j=1;j<=n1;j++)cerr<<f[i][j]<<" ";cerr<<endl;
            int ans=OO;
            for (int i=1;i<=n1;i++)
                if (f[n][i]<ans) ans=f[n][i];
            printf("%d\n",ans);
        }
        return 0;
    }
    








  • 相关阅读:
    利用Vue这些修饰符帮我节省20%的开发时间
    ELK API
    ssh编译安装
    谷歌浏览器皮肤
    整理了100篇Linux技术精华
    使用Prometheus+Grafana监控MySQL实践
    mysqldiff
    kafka命令
    calico 文件丢失问题
    Prometheus 告警分配到指定接收组
  • 原文地址:https://www.cnblogs.com/cyendra/p/3226342.html
Copyright © 2020-2023  润新知