【UVA】1218 - Perfect Service(动态规划)
来源:互联网 发布:宁静马景涛 知乎 编辑:程序博客网 时间:2024/04/28 13:04
好纠结的一道题,如果不按书上的思路来根本想不出来,哎~还是思路不够啊。
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<vector>#include<stack>#include<queue>#include<map>#include<set>#include<list>#include<string>#include<sstream>#include<ctime>using namespace std;#define _PI acos(-1.0)#define INF 1 << 10#define esp 1e-6typedef long long LL;typedef unsigned long long ULL;typedef pair<int,int> pill;/*======================================================================================*/#define MAXD 10000 + 10int n;int dp[MAXD][4];int vis[MAXD];/*dp[i][0]代表结点i是服务器的时候*//*dp[i][1]代表结点i的父亲是服务器的时候*//*dp[i][2]代表结点i的儿子是服务器的时候*/vector<int>G[MAXD];void DP(int pos){ vis[pos] = 1; dp[pos][0] = 1; dp[pos][1] = 0; dp[pos][2] = 1 << 20; vector<int>arr; for(int i = 0 ; i < G[pos].size() ; i++){ int t = G[pos][i]; if(!vis[t]){ DP(t); arr.push_back(t); dp[pos][0] = dp[pos][0] + min(dp[t][1],dp[t][0]); dp[pos][1] += dp[t][2]; } } for(int i = 0 ; i < arr.size() ; i++){ int t = arr[i]; dp[pos][2] = min(dp[pos][2],dp[pos][1] - dp[t][2] + dp[t][0]); } return ;}int main(){ while(scanf("%d",&n)){ for(int i = 1 ; i <= n ; i++) G[i].clear(); for(int i = 1 ; i < n ; i++){ int x,y; scanf("%d%d",&x,&y); G[x].push_back(y); G[y].push_back(x); } int ans = 1 << 20; DP(1); memset(vis,0,sizeof(vis)); ans = min(dp[1][0],dp[1][2]); printf("%d\n",ans); int next; scanf("%d",&next); if(next < 0) break; } return 0;}
0 0
- 【UVA】1218 - Perfect Service(动态规划)
- 100道动态规划——37 UVA 1218 Perfect Service 树形DP 分析问题
- UVA 1218Perfect Service
- UVa 1218 Perfect Service
- Uva-1218 Perfect Service
- UVA-1218 Perfect Service
- UVA 1218 Perfect Service
- UVa 1218 - Perfect Service(树形DP)
- UVa 1218:Perfect Service(DP)
- UVA - 1218 Perfect Service (树形DP)
- UVA1218[Perfect Service] 树上动态规划模型
- Perfect Service - UVa 1218 dp
- UVa #1218 Perfect Service (例题9-14)
- UVa 1218 Perfect Service [DFS+DP]
- uva 1218 Perfect Service 树形dp
- uva 1218——Perfect Service
- UVA - 1218 Perfect Service(树形dp)
- UVA 1218 Perfect Service [树形dp]
- JSTL中取得int类型数据的长度
- IBinder对象在进程间传递的形式(二)
- Ubuntu下双显示器设定
- Unity3D中关于NGUI的学习笔记(更多资讯资料群:193521697邀请码:10026)
- quick-cocos2dx中一些代码行的含义
- 【UVA】1218 - Perfect Service(动态规划)
- Android中ImageView.ScaleType属性值
- ios--分栏控制器的基本概念
- Android Binder IPC分析
- selenium2library 离线安装需另外的包
- 怎样将unity资源窗口中的文件一下全部折叠/打开
- 百度诉360违反爬虫协议案宣判
- varnish代理服务器笔记
- 百度诉360违反爬虫协议案宣判