2286: [Sdoi2011消耗战
来源:互联网 发布:关于品质数据的口号 编辑:程序博客网 时间:2024/05/30 04:25
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
10
1 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
1 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
12
32
22
32
22
HINT
对于100%的数据,2<=n<=250000,m>=1,sigma(ki)<=500000,1<=ki<=n-1
Source
Stage2 day2
#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 YZYfreopen("yzy.txt","r",stdin);#endifcin >> 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;}
0 0
- 2286: [Sdoi2011消耗战
- 2286: [Sdoi2011消耗战
- BZOJ 2286: [Sdoi2011]消耗战
- bzoj 2286: [Sdoi2011]消耗战
- bzoj 2286: [Sdoi2011]消耗战
- 2286: [Sdoi2011]消耗战
- 2286: [Sdoi2011]消耗战
- BZOJ 2286: [Sdoi2011]消耗战
- bzoj 2286 [Sdoi2011]消耗战 虚树
- BZOJ 2286 [Sdoi2011]消耗战 虚树
- 【BZOJ】2286: [Sdoi2011消耗战【虚树DP】
- 【 bzoj 2286 】 : [Sdoi2011]消耗战 - 树形DP
- 2286: [Sdoi2011消耗战|树形DP|虚树
- bzoj 2286: [Sdoi2011消耗战 虚树+树形dp
- bzoj 2286 SDOI2011 消耗战 虚树dp
- 【BZOJ 2286】[Sdoi2011消耗战 虚树+dp
- bzoj 2286: [Sdoi2011]消耗战 虚树 DP
- 【BZOJ】2286 [Sdoi2011]消耗战 树形DP+虚树
- for循环嵌套函数obj[i]undefined
- day1-线性表
- Spring配置详解
- java 实验一Java开发环境(无脑实验系列)
- 浏览器的本地存储方法
- 2286: [Sdoi2011消耗战
- 用广播加回调函数可以实现数据的立即加载
- Untiy3D的粒子系统介绍
- SQL Compare 10 数据库结构比对工具 下载 及 破解工具 操作手册
- tomcat 7出现warning信息WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context}
- ActionBar 用法
- 子父类中,成员特点体现
- cqoi2016
- JVM垃圾收集器总结