hdu - 3660 - Alice and Bob's Trip(树形dp)
来源:互联网 发布:linux wget 命令 编辑:程序博客网 时间:2024/05/12 06:43
题意:一棵有n个结点的树,编号从0(根结点)开始,Alice和Bob一起从0走到叶子结点,Alice走最短路,Bob走最长路,Bob先选择下一个结点,然后两个一起走到那个结点,接着Alice选择下一个结点……,总长度要在[L, R]内,问这种走法的最长路的长度(1 <= n <= 500000, 0 <= L, R <= 1000000000, 1 <= 相邻结点间距离 <= 1000)。
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3660
——>>怒刷树状dp。。。
设da[i]为Alice从结点i出发走到叶子的最短距离,则
状态转移方程为:da[x] = min(da[x], db[v[e]] + w[e]);
设db[i]为Bob从结点i出发走到叶子的最长距离,则
状态转移方程为:db[x] = max(db[x], da[v[e]] + w[e]);
加上IO优化,以C++提交~
#include <cstdio>#include <cstring>#include <algorithm>#include <cctype>using namespace std;const int maxn = 500000 + 10;const int INF = 0x3f3f3f3f;int n, L, R, head[maxn], nxt[maxn], u[maxn], v[maxn], w[maxn], ecnt, da[maxn], db[maxn];void init(){ ecnt = 0; memset(head, -1, sizeof(head));}void addEdge(int uu, int vv, int ww){ u[ecnt] = uu; v[ecnt] = vv; w[ecnt] = ww; nxt[ecnt] = head[uu]; head[uu] = ecnt; ecnt++;}int nextInt(){ char c = getchar(); while(!isdigit(c)) c = getchar(); int ret = 0; while(isdigit(c)){ ret = ret * 10 + c - '0'; c = getchar(); } return ret;}void read(){ int uu, vv, ww, i; for(i = 0; i < n-1; i++){ uu = nextInt(); vv = nextInt(); ww = nextInt(); addEdge(uu, vv, ww); }}void dp(int x, int cur){ da[x] = head[x] == -1 ? 0 : INF; db[x] = 0; for(int e = head[x]; e != -1; e = nxt[e]){ int len = cur + w[e]; if(len <= R) dp(v[e], len); if(len + db[v[e]] >= L && len + db[v[e]] <= R) da[x] = min(da[x], db[v[e]] + w[e]); if(len + da[v[e]] >= L && len + da[v[e]] <= R) db[x] = max(db[x], da[v[e]] + w[e]); }}void solve(){ dp(0, 0); if(db[0]) printf("%d\n", db[0]); else puts("Oh, my god!");}int main(){ while(scanf("%d%d%d", &n, &L, &R) == 3){ init(); read(); solve(); } return 0;}
- hdu - 3660 - Alice and Bob's Trip(树形dp)
- [HDU] 3660 Alice and Bob's Trip -- 树形DP?
- 树形dp-hdu-3660-Alice and Bob's Trip
- hdu 3660 Alice and Bob's Trip(树形DP)
- uva 1484 Alice and Bob's Trip (树形dp)
- UVALive 5088 Alice and Bob's Trip(树形DP)
- UVA 1484 - Alice and Bob's Trip(树形DP)
- uva 1484 - Alice and Bob's Trip(树形dp)
- UVA1484 Alice and Bob's Trip(树形dp)
- UVALive 5088 Alice and Bob's Trip(树形DP)
- Uva 1484 Alice and Bob's Trip(树形dp)
- uva1484 Alice and Bob's Trip (树形dp)
- uva-1484(hdu-3660) Alice and Bob's Trip 听说是树形dp
- HDU 3660 Alice and Bob's Trip
- hdu 3660 Alice and Bob's Trip
- HDU 3660 Alice and Bob's Trip
- hdu3660 Alice and Bob's Trip 树形dp
- UVA 1484 Alice and Bob's Trip (树形dp)
- 处理android.os.NetWorkOnMainThreadException异常的方法
- paip.navicat form mysql导入文本文件时CPU占用100%的解决
- 多线程编程经典文章集合
- 在下载文件名中有空格时,firefox会自动截断
- Java垃圾回收调优
- hdu - 3660 - Alice and Bob's Trip(树形dp)
- poj1220 (高精度任意进制转换)
- php如何判断中文文件存在
- cocos2d-x 使游戏真正自适配屏幕分辨率的方法
- 不可表示的数
- MFC处理jpg等格式图片 IPicture接口(一)
- MFC处理jpg等格式图片 Cpicture(二)
- linux并发控制(三)
- linux并发控制例子