POJ 1192 (tree-shaped DP)

Source: Internet
Author: User

Optimal connected subset
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 2589 Accepted: 1382

Description

As we all know, we can use a Cartesian coordinate system to the plane of any point P with an ordered number of pairs (x, y) to uniquely represent, if x, y are integers, we will point P is called the Hour, otherwise the point P is called the non-whole hour. We write the set of all the whole hour on the plane as W.
Define 12 Hour P1 (x1, y1), P2 (x2, y2), if |x1-x2| + |y1-y2| = 1, is called P1, P2 adjacent, recorded as P1~P2, otherwise called P1, P2 is not adjacent.
Define 2 set S is a finite subset of W, i.e. S = {P1, P2,..., Pn} (n >= 1), where PI (1 <= i <= N) belongs to W, we refer to S as the hourly set.
Definition 3 Set S is an hourly set, if point R, T belongs to S, and there is a finite point sequence Q1, Q2,?, Qk satisfies:
1.Qi belongs to S (1 <= i <= K);
2.Q1 = R, Qk = T;
3.qi~qi + 1 (1 <= i <= k-1), namely Qi and Qi + 1 adjacent;
4. For any 1 <= i < J <= K have qi≠qj;
We say that point R is connected with point T on the hour set S, the point sequence Q1, Q2,..., qk is called the point set S in the junction R with Points T of a road.
Definition 4 If the hour set V satisfies: For any two points in V, and there is only one link to the two point of the road, V is called a single hour set.
Define 5 for each point on the plane, we can give it an integer, as the right of that point, so we put an hour to concentrate all the points of the sum of the weights called the whole hour set.
We want to find the optimal connected subset B of a V for a given single-hour set V, to satisfy:
1.B is a subset of V
2. For any two hour in B, connect in B;
3.B is the one that satisfies the conditions (1) and (2) of all the hourly concentration rights and the largest.

Input

The 1th line is an integer n (2 <= n <= 1000) that represents the number of points in the single point set V;
In the following N rows, line I (1 <= i <= N) has three integers, Xi, Yi, and CI in turn represent the horizontal axis, ordinate and right of the first point. The same line is separated by a space between the two adjacent numbers. -10^6 <= Xi, Yi <= 10^6;-100 <= Ci <= 100.

Output

Only an integer representing the right and the optimal connected set.

Sample Input

50 0-20 1 11 0 10-1 1-1 0 1

Sample Output

2

Source

Test instructions: To find a tree with the strongest connection in a strong connected trees

Analysis: dp[i][1] If the node is selected, then its child nodes in the selection and uncheck the weights dp[i][1] = dp[i][1] + max (dp[i_son][1],dp[i_son][0]) I_son is all the subtree of I
DP[I][0] If the node is not selected, then its child nodes have only one choice to ensure strong connectivity dp[i][0] = max (Dp[i][0],max (dp[i_son][1],dp[i_son][0))

Side only opened N WA,, must remember is 2*n, there is math.h Fabs will report CE, or use stdlib.h ABS bar

///Dp[i][1] If the node is selected, its child nodes are selected and not selected .///Dp[i][0] If the node is not selected, then its child nodes have only one choice to ensure strong connectivity .#include <iostream>#include<stdio.h>#include<string.h>#include<stdlib.h>#include<math.h>#defineN 1005using namespacestd;intdp[n][2];///Dp[i][0] Represents a subtree that does not select I nodes with I as the root node to gain maximum weight,///Dp[i][1] represents the choice I node to I as the root node of the subtree can obtain the maximum weightintHead[n];structedge{intU,v,next;} edge[2*n];///!!!! Open twice timesvoidAddedge (intUintVint&k) {edge[k].u= U,EDGE[K].V =v; Edge[k].next= head[u],head[u]=k++;}intVis[n];voidDfsintu) {Vis[u]=1;  for(intK=head[u]; k!=-1; k=Edge[k].next) {        intv =edge[k].v; if(!Vis[v])            {DFS (v); dp[u][1]+= Max (dp[v][0],dp[v][1]); dp[u][0] = max (dp[u][0],max (dp[v][1],dp[v][0])); }    }}intMain () {intN;  while(SCANF ("%d", &n)! =EOF) {memset (head,-1,sizeof(head)); Memset (DP,0,sizeof(DP)); memset (Vis,0,sizeof(VIS)); intx[n],y[n],w,tot=0;  for(intI=1; i<=n; i++) {scanf ("%d%d%d",&x[i],&y[i],&W); dp[i][1]=W;  for(intj=1; j<i; J + +)            {                if((ABS (X[i]-x[j]) +abs (y[i]-y[j]) = =1) {Addedge (I,j,tot);                Addedge (J,i,tot); }}} DFS (1)////Choose one of the root printf ("%d\n", Max (dp[1][0],dp[1][1])); }}

POJ 1192 (tree-shaped DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.