XVII Open Cup named after E.V. Pankratiev Grand Prix of Moscow Workshops, Sunday, April 23, 2017 Problem K. Piecemaking
题目:Problem K. Piecemaking
Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 512 mebibytes
The civil war in Berland continues for five years already. The United Nation decided to end the bloodshed.
Berland consists of n cities, connected by n - 1 bidirectional roads, forming a tree. The Purple Army
occupies several cities which are listed in the set A, and the Cian Army occupies cities listed in the set
B. The UN Assembly took an unexpected decision to end the war: they decided to destroy some roads
in Berland, so that no city from the set A is connected (directly or undirectly) with a city from the set
B. This way the enemies wouldn’t be able to reach each other, and the fighting would stop. Destroying
a road of length x kilometers requires x dollars.
Find the minimum sum of money neccessary for peacemaking in Berland.
Input
The first line of the input contains a positive integer n (2 ≤ n ≤ 200 000) — the number of cities in
Berland.
Next n - 1 lines contain information about roads in the form ui, vi, wi (1 ≤ ui; vi ≤ n, 1 ≤ wi ≤ 109) —
indices of cities connected by the i-th road, and the length of this road in kilometers.
The next line contains a positive integer m (1 ≤ m ≤ n) — the number of cities occupied by the Purple
Army, and m distinct integers a1; a2; : : : ; am (1 ≤ ai ≤ n) — indices of these cities.
The next line contains a positive integer k (1 ≤ k ≤ n) and k distinct integers b1; b2; : : : ; bk (1 ≤ bi ≤ n) —
the cities, occupied by the Cian army, in the similar format.
It is guaranteed that no city is occupied by both armies.
Output
Print the minimum possible number of dollars required to make it impossible to reach any city in set B
from any city in set A.
Example
standard input | standard output |
6 1 2 5 2 4 4 2 5 1 1 3 2 3 6 7 1 4 2 5 6 |
3 |
思路:
比较简单的树dp:dp[i][s]:表示这个点属于空集合,还是A集合,还是B集合?
转移过程略麻烦。
#include <bits/stdc++.h> using namespace std; #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e6+;
const int mod=1e9+; vector<PII>mp[K];
int n,m,col[K];
LL dp[K][]; void dfs(int u,int f)
{
for(auto v:mp[u])if(v.first!=f) dfs(v.first,u);
for(auto v:mp[u])if(v.first!=f)
{
if(col[u]==)
{
if(col[v.first]==)
{
dp[u][]+=min(dp[v.first][],min(dp[v.first][],dp[v.first][])+v.second);
dp[u][]+=min(min(dp[v.first][],dp[v.first][]),dp[v.first][]+v.second);
dp[u][]+=min(min(dp[v.first][],dp[v.first][]),dp[v.first][]+v.second);
}
else if(col[v.first]==)
{
dp[u][]+=dp[v.first][]+v.second;
dp[u][]+=dp[v.first][];
dp[u][]+=dp[v.first][]+v.second;
}
else
{
dp[u][]+=dp[v.first][]+v.second;
dp[u][]+=dp[v.first][]+v.second;
dp[u][]+=dp[v.first][];
}
}
else if(col[u]==)
{
if(col[v.first]==)
dp[u][]+=min(min(dp[v.first][],dp[v.first][]),dp[v.first][]+v.second);
else if(col[v.first]==)
dp[u][]+=dp[v.first][];
else
dp[u][]+=dp[v.first][]+v.second;
}
else
{
if(col[v.first]==)
dp[u][]+=min(min(dp[v.first][],dp[v.first][]),dp[v.first][]+v.second);
else if(col[v.first]==)
dp[u][]+=dp[v.first][]+v.second;
else
dp[u][]+=dp[v.first][];
}
}
if(col[u]==) dp[u][]=dp[u][]=2e14;
else if(col[u]==) dp[u][]=dp[u][]=2e14;
//printf("%d==%lld %lld %lld\n",u,dp[u][0],dp[u][1],dp[u][2]);
}
int main(void)
{
scanf("%d",&n);
for(int i=,x,y,z;i<n;i++)
scanf("%d%d%d",&x,&y,&z),mp[x].PB(MP(y,z)),mp[y].PB(MP(x,z));
scanf("%d",&m);
for(int i=,x;i<=m;i++) scanf("%d",&x),col[x]=;
scanf("%d",&m);
for(int i=,x;i<=m;i++) scanf("%d",&x),col[x]=;
dfs(,);
printf("%lld\n",min(min(dp[][],dp[][]),dp[][]));
return ;
}
XVII Open Cup named after E.V. Pankratiev Grand Prix of Moscow Workshops, Sunday, April 23, 2017 Problem K. Piecemaking的更多相关文章
- XVII Open Cup named after E.V. Pankratiev Grand Prix of Moscow Workshops, Sunday, April 23, 2017 Problem D. Great Again
题目: Problem D. Great AgainInput file: standard inputOutput file: standard outputTime limit: 2 second ...
- 【分块】【暴力】XVII Open Cup named after E.V. Pankratiev Grand Prix of Moscow Workshops, Sunday, April 23, 2017 Problem I. Rage Minimum Query
1000w的数组,一开始都是2^31-1,然后经过5*10^7次随机位置的随机修改,问你每次的全局最小值. 有效的随机修改的期望次数很少,只有当修改到的位置恰好是当前最小值的位置时才需要扫一下更新最小 ...
- XVII Open Cup named after E.V. Pankratiev. Grand Prix of America (NAIPC-2017)
A. Pieces of Parentheses 将括号串排序,先处理会使左括号数增加的串,这里面先处理减少的值少的串:再处理会使左括号数减少的串,这里面先处理差值较大的串.确定顺序之后就可以DP了. ...
- XVIII Open Cup named after E.V. Pankratiev. Grand Prix of SPb
A. Base $i - 1$ Notation 两个性质: $2=1100$ $122=0$ 利用这两条性质实现高精度加法即可. 时间复杂度$O(n)$. #include<stdio.h&g ...
- XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Siberia
1. GUI 按题意判断即可. #include<stdio.h> #include<iostream> #include<string.h> #include&l ...
- XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Peterhof
A. City Wall 找规律. #include<stdio.h> #include<iostream> #include<string.h> #include ...
- XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Khamovniki
A. Ability Draft 记忆化搜索. #include<stdio.h> #include<iostream> #include<string.h> #i ...
- XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Korea
A. Donut 扫描线+线段树. #include<cstdio> #include<algorithm> using namespace std; typedef long ...
- XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Saratov
A. Three Arrays 枚举每个$a_i$,双指针出$b$和$c$的范围,对于$b$中每个预先双指针出$c$的范围,那么对于每个$b$,在对应$c$的区间加$1$,在$a$处区间求和即可. 树 ...
随机推荐
- 移动ChemDraw结构有什么方法
ChemDraw软件是一款比较常见的化学绘图软件,化学专业的领域的人常常会用到它.本教程主要是针对新手用户,让其了解一些ChemDraw的一些基本操作,以便其能尽快上手早日用到工作中.下面我们就来给大 ...
- matlab判断图像是彩色图还是灰度图
matlab怎样看图像是彩色还是灰度_莹莹_新浪博客 http://blog.sina.com.cn/s/blog_76088a1f0101diq0.html 解决一: isrgb(A) 如果A是RG ...
- 使用openstack部署云计算服务环境
环境: 系统 硬盘 IP hostname redhat 7 sda 20G 192.168.0.70 openstack.com 64位 sdb 20G 配置网卡 [root@opens ...
- poj_2823 线段树
题目大意 给定一行数,共N个.有一个长度为K的窗口从左向右滑动,窗口中始终有K个数字,窗口每次滑动一个数字.求各个时刻窗口中的最大值和最小值. 题目分析 直接搜索,复杂度为O(n^2).本题可以看做是 ...
- LAMP集群项目
vi /etc/sysconfig/network 一.安装硬件环境(安装虚拟机) 1.安装VMware步骤 1.修改网卡配置 vi /etc/sysconfig/network-scripts/if ...
- 160509、Java过滤器与SpringMVC拦截器之间的关系与区别
今天学习和认识了一下,过滤器和SpringMVC的拦截器的区别,学到了不少的东西,以前一直以为拦截器就是过滤器实现的,现在想想还真是一种错误啊,而且看的比较粗浅,没有一个全局而又细致的认识,由于已至深 ...
- Debian安装Chrome
本文完全原创,转载请说明出处,希望对大家有用. 本篇博客是个人总结,一方面以便日后查看,另一方面希望能为其他人提供一些便利. 正文 新安装的Debian需要安装个chromeFQ,但从google网站 ...
- About LabView
Recently I am running an experiment. Because the lab has only NI devices, I have to learn to use the ...
- 重装系统后Myeclipse遇到的项目配置问题--一个菜鸟的经历!
电脑不知道为什么流量突然变大了. 一查svchost.exe后台下载老多系统.某某安全卫士根本么用,运维说用某企鹅管家. 结果一个鸟样.. 之前是系统是32位的win7 4G内存用不完.又打算升级内 ...
- 并发编程 - io模型 - 总结
1.提交任务得方式: 同步:提交完任务,等结果,执行下一个任务 异步:提交完,接着执行,异步 + 回调 异步不等结果,提交完任务,任务执行完后,会自动触发回调函数2.同步不等于阻塞: 阻塞:遇到io, ...