2286: [Sdoi2011消耗战
2016-05-23 00:09
274 查看
2286: [Sdoi2011消耗战
Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1878 Solved: 671
[Submit][Status][Discuss]
Description
在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达。现在,我军已经侦查到敌军的总部在编号为1的岛屿,而且他们已经没有足够多的能源维系战斗,我军胜利在望。已知在其他k个岛屿上有丰富能源,为了防止敌军获取能源,我军的任务是炸毁一些桥梁,使得敌军不能到达任何能源丰富的岛屿。由于不同桥梁的材质和结构不同,所以炸毁不同的桥梁有不同的代价,我军希望在满足目标的同时使得总代价最小。侦查部门还发现,敌军有一台神秘机器。即使我军切断所有能源之后,他们也可以用那台机器。机器产生的效果不仅仅会修复所有我军炸毁的桥梁,而且会重新随机资源分布(但可以保证的是,资源不会分布到1号岛屿上)。不过侦查部门还发现了这台机器只能够使用m次,所以我们只需要把每次任务完成即可。
Input
第一行一个整数n,代表岛屿数量。接下来n-1行,每行三个整数u,v,w,代表u号岛屿和v号岛屿由一条代价为c的桥梁直接相连,保证1<=u,v<=n且1<=c<=100000。
第n+1行,一个整数m,代表敌方机器能使用的次数。
接下来m行,每行一个整数ki,代表第i次后,有ki个岛屿资源丰富,接下来k个整数h1,h2,…hk,表示资源丰富岛屿的编号。
Output
输出有m行,分别代表每次任务的最小代价。Sample Input
101 5 13
1 9 6
2 1 19
2 4 8
2 3 91
5 6 8
7 5 4
7 8 31
10 7 9
3
2 10 6
4 5 7 8 3
3 9 4 6
Sample Output
1232
22
HINT
对于100%的数据,2<=n<=250000,m>=1,sigma(ki)<=500000,1<=ki<=n-1Source
Stage2 day2[Submit][Status][Discuss]
虚树模板题
如何构建虚树嘛。。。传送门
感觉这位大哥写的很好
可以看到,本题中k很少,就算全部合起来也没几个。。。
事实上每次询问用到的点都没有n个,m次询问合起来也不过2n
那么构建虚树,倍增求边权,树形dp,复杂度O(nlogn)
#include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #include<cmath> #include<vector> #include<queue> using namespace std; typedef long long LL; const int maxn = 2E5 + 5E4 + 10; const LL INF = 1E18; struct E{ int to,w; }; int n,m,a[maxn],dfn[maxn],L[maxn],fa[maxn][20]; int s[maxn],clo,che,mi[maxn][20],bo[maxn]; bool pic[maxn]; vector <E> v[maxn],v2[maxn]; void dfs(int x) { for (int i = 1; i < 20; i++) { fa[x][i] = fa[fa[x][i-1]][i-1]; mi[x][i] = min(mi[x][i-1],mi[fa[x][i-1]][i-1]); } dfn[x] = ++clo; for (int i = 0; i < v[x].size(); i++) { int to = v[x][i].to; if (L[to]) continue; L[to] = L[x] + 1; fa[to][0] = x; mi[to][0] = v[x][i].w; dfs(to); } } int LCA(int p,int q) { if (L[p] < L[q]) swap(p,q); che = ~0U>>1; int Log; for (Log = 0; L[p] - (1<<Log) >= 1; Log++); --Log; for (int j = Log; j >= 0; j--) if (L[p] - (1<<j) >= L[q]) { che = min(che,mi[p][j]); p = fa[p][j]; } if (p == q) return p; for (int j = Log; j >= 0; j--) if (fa[p][j] != fa[q][j]) p = fa[p][j],q = fa[q][j]; return fa[p][0]; } LL solve(int x,LL f) { LL ret = 0; for (int i = 0; i < v2[x].size(); i++) { int to = v2[x][i].to; if (!pic[to]) ret += solve(to,v2[x][i].w); else ret += 1LL*v2[x][i].w; if (ret >= f) return f; } return min(ret,f); } bool cmp(const int &x,const int &y) { return dfn[x] < dfn[y]; } int main() { #ifdef YZY freopen("yzy.txt","r",stdin); #endif cin >> n; for (int i = 1; i < n; i++) { int x,y,w; scanf("%d%d%d",&x,&y,&w); v[x].push_back((E){y,w}); v[y].push_back((E){x,w}); } L[1] = 1; dfs(1); cin >> m; for (int j = 1; j <= m; j++) { int k; scanf("%d",&k); for (int i = 1; i <= k; i++) scanf("%d",&a[i]),pic[a[i]] = 1; sort(a + 1,a + k + 1,cmp); int cnt; s[cnt = 1] = 1; for (int i = 1; i <= k; i++) { int u,t = s[cnt]; int lca = LCA(t,a[i]); if (lca == t) {s[++cnt] = a[i]; continue;} for (;;) { t = s[cnt]; u = s[cnt-1]; int Lca = LCA(t,u); if (dfn[Lca] >= dfn[lca]) { if (bo[Lca] != j) v2[Lca].clear(),bo[Lca] = j; v2[Lca].push_back((E){t,che}); --cnt; if (dfn[Lca] == dfn[lca]) break; } else { LCA(t,lca); if (bo[lca] != j) v2[lca].clear(),bo[lca] = j; v2[lca].push_back((E){t,che}); s[cnt] = lca; break; } } s[++cnt] = a[i]; } while (cnt > 1) { int t = s[cnt],u = s[cnt-1]; LCA(u,t); --cnt; if (bo[u] != j) v2[u].clear(),bo[u] = j; v2[u].push_back((E){t,che}); } printf("%lld\n",solve(1,INF)); for (int i = 1; i <= k; i++) pic[a[i]] = 0; } return 0; }
相关文章推荐
- 工作队列
- 兄台息怒,关于arguments,您的想法和大神是一样一样的----闲聊JS中的apply和call
- android 简单的网格布局 GridLayout
- leetcode_c++:Combination Sum_2(040)
- Untiy3D的粒子系统介绍
- [Java] 作业6(字符串)
- Linux08--Shell程序设计03 shell script
- cqoi2016
- ActionBar 用法
- 字父类中,成员特点体现
- SQL注入测试平台 SQLol -2.SELECT注入测试
- Codeforces Round #349 (Div. 1) B. World Tour 最短路+暴力枚举
- 用广播加回调函数可以实现数据的立即加载
- hihoCoder 1298 欧拉函数
- Java Web项目报错The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- 终于把小说都看完了
- 浏览器的本地存储方法
- JSONP跨域原理和jQuery.getJSON用法
- 百度相册彻底关闭下线
- JSP实现用户登录、注册和退出功能