poj 1192最优连通子集(简单树形dp)
题目链接:http://poj.org/problem?id=1192
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> using namespace std; const int maxn = 1005; const int INF = 0x3f3f3f; int dp[maxn]; vector<int> G[maxn]; int W[maxn]; int N; struct Point{ int x,y; }P[maxn]; int ans; void dfs(int u,int fa){ dp[u] = W[u]; for(int i=0;i<G[u].size();i++){ int v = G[u][i]; if(v == fa) continue; dfs(v,u); dp[u] += max(dp[v],0); } ans = max(ans,dp[u]); } int main() { // freopen("E:\\acm\\input.txt","r",stdin); scanf("%d",&N); for(int i=1;i<=N;i++){ scanf("%d %d",&P[i].x,&P[i].y); scanf("%d",&W[i]); } for(int i=1;i<=N;i++) for(int j=i+1;j<=N;j++) if(abs(P[i].x-P[j].x) + abs(P[i].y-P[j].y) == 1){ G[i].push_back(j); G[j].push_back(i); } ans = -INF; dfs(1,-1); printf("%d\n",ans); }