UVALive

来源:互联网 发布:xap格式软件下载 编辑:程序博客网 时间:2024/06/08 04:39

原题:

Traveling salesmen of . (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 thecorrect path could
be [1 2 2 4 5 5 5 7 4], [1 24 7 5 5 5 7 4], or [1 2 26 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 =
a 1a 2. . . a nand B = b 1 b 2 . . . b n of the same lengthn as
dist(A, B) =
n i
=1
d(ai, bi)
where
d(a, b) ={ 0 if1 otherwisea =b
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 distance dist(A, B) is minimized.
Input
The input consists ofT 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 integersn 1,n 2(3 n 1100,2 n 24,950) where n 1 is
the number of vertices of the graph representing the working map of a salesman andn 2is the number
of edges in the graph. The input graph is a connected graph. Each vertex of the graph is numbered
from 1 to n 1. In the followingn 2lines, 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 n 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
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
10


题意:

       给定一个无向图和一段序列,给出最小的操作数使序列中所有相邻的端点相等或为无向图中的相邻节点。


思路:

      设dp[i][j]为序列前i个点且第i个点为j时的最小操作数,则dp[i][j]=min{dp[i-1][k](dp[i-1][k]+1) | G[j][k]==1||j==k},由于数据量很小,三重循环暴力即可。


#include <iostream>#include <iomanip>#include <algorithm>#include <cstdio>#include <cstring>#include <queue>#include <deque>#include <string>#include <cmath>#include <vector>#include <utility>#include <set>#include <map>#include <sstream>#include <climits>//#pragma comment(linker, "/STACK:1024000000,1024000000")#define pi acos(-1.0)#define INF 2147483647using namespace std;typedef long long ll;typedef pair <int,int > PP;int G[105][105];int dp[205][105];int main(){    int T;    scanf("%d",&T);    while(T--)    {        memset(G,0,sizeof(G));        memset(dp,0,sizeof(dp));        for(int i=0; i<105; i++)            G[i][i]=1;        int n1,n2;        scanf("%d%d",&n1,&n2);        while(n2--)        {            int a,b;            scanf("%d%d",&a,&b);            G[a][b]=1;            G[b][a]=1;        }        int l,t;        scanf("%d%d",&l,&t);        for(int i=1; i<=n1; i++)            if(i!=t)                dp[0][i]=1;        for(int i=1; i<l; i++)        {            scanf("%d",&t);            for(int j=1; j<=n1; j++)            {                dp[i][j]=INF;                for(int k=1; k<=n1; k++)                {                    if(G[j][k])                    {                        if(j!=t)//当前位也需要改变时,操作数需加1                            dp[i][j]=min(dp[i][j],dp[i-1][k]+1);                        else                            dp[i][j]=min(dp[i][j],dp[i-1][k]);                    }                }            }        }        int res=INF;        for(int i=1; i<=n1; i++)        {            res=min(dp[l-1][i],res);            //cout<<i<<" "<<dp[l-1][i]<<endl;        }        printf("%d\n",res);    }    return 0;}


原创粉丝点击