UVALive 4256 Salesmen (树DP,4级)
来源:互联网 发布:阿里云邮箱要钱吗 编辑:程序博客网 时间:2024/04/30 16:24
Description
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 becorrect 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 24 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 thedistance between two paths A =a1a2...an and B = b1b2...bn of the same lengthn as
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 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 integersn1 , n2(3n1100, 2n24, 950) where n1 is the number of vertices of the graph representing the working map of a salesman andn2 is the number of edges in the graph. The input graph is a connected graph. Each vertex of the graph is numbered from 1 ton1 . 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 integern (2n200) 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
问最少改几个数使得相邻要么相等,要么是树上节点。
思路:建树,添加自身环节点,这就省去了数组中数字相等的判断,直接判断是不是在树上就好了,然后一个DP也
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#define FOR(i,a,b) for(int i=a;i<=b;++i)#define clr(f,z) memset(f,z,sizeof(f))using namespace std;const int mm=222;int dp[mm][mm];bool g[mm][mm];int f[mm],n,m;int DP(int t,int fa){ if(t==0)return 0; if(dp[t][fa]!=-1)return dp[t][fa]; int&ret=dp[t][fa];ret=mm; FOR(i,1,n) { if(g[i][fa]) { ret=min(ret,DP(t-1,i)+(i==f[t]?0:1)); } } return ret;}int main(){ int cas,a,b; while(~scanf("%d",&cas)) { while(cas--) { scanf("%d%d",&n,&m); clr(g,0);clr(dp,-1); FOR(i,1,m) { scanf("%d%d",&a,&b); g[a][b]=1;g[b][a]=1; } FOR(i,1,n) { g[i][0]=1;g[0][i]=1;//初始化 g[i][i]=1;//equal } scanf("%d",&m); FOR(i,1,m)scanf("%d",&f[i]); printf("%d\n",DP(m,0)); } } return 0;}
- UVALive 4256 Salesmen (树DP,4级)
- UVALive - 4256 Salesmen DP+暴力
- UVALive 4256 Salesmen 【简单dp】
- UVALive - 4256 || LA 4256 Salesmen 商人(DP)
- UVaLive LA 4256 | UVa 1424 - Salesmen(简单DP)
- UVALive 4256 Salesmen
- UVALive 4256 Salesmen
- UVALive - 4256 Salesmen
- UVa 4256 - Salesmen dp
- LA 4256 Salesmen 线性dp
- UVA 1424 - Salesmen(dp)
- UVA 1424-Salesmen(DP)
- UVA-1424-Salesmen(DP)
- UVA 1424 - Salesmen (dp)
- uva 1424 - Salesmen(dp)
- UVA 1424 Salesmen | dp
- Salesmen - UVa 1424 dp
- UVA 1424 - Salesmen(DP)
- future Callable(线程)
- 深入研究线程池
- 之前遇到的一个问题
- win7 通过 wifi 调试开发 android 程序。
- ltib(1)初识
- UVALive 4256 Salesmen (树DP,4级)
- 天梯题目解答--通过初赛 3数论-4进制转换
- 基本数据结构只二叉树
- 概率dp求期望-hdu-4586-Play the Dice
- where are three concepts of the oop
- HDU 2686 双进程DP
- 类的加载和反射
- pjsip 编译配置项
- Flex 1114 The public attribute can only be used inside a package.