题目: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的更多相关文章

  1. 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 ...

  2. 【分块】【暴力】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次随机位置的随机修改,问你每次的全局最小值. 有效的随机修改的期望次数很少,只有当修改到的位置恰好是当前最小值的位置时才需要扫一下更新最小 ...

  3. XVII Open Cup named after E.V. Pankratiev. Grand Prix of America (NAIPC-2017)

    A. Pieces of Parentheses 将括号串排序,先处理会使左括号数增加的串,这里面先处理减少的值少的串:再处理会使左括号数减少的串,这里面先处理差值较大的串.确定顺序之后就可以DP了. ...

  4. 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 ...

  5. XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Siberia

    1. GUI 按题意判断即可. #include<stdio.h> #include<iostream> #include<string.h> #include&l ...

  6. XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Peterhof

    A. City Wall 找规律. #include<stdio.h> #include<iostream> #include<string.h> #include ...

  7. XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Khamovniki

    A. Ability Draft 记忆化搜索. #include<stdio.h> #include<iostream> #include<string.h> #i ...

  8. XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Korea

    A. Donut 扫描线+线段树. #include<cstdio> #include<algorithm> using namespace std; typedef long ...

  9. 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$处区间求和即可. 树 ...

随机推荐

  1. 查看linux系统外网ip命令

    终端中输入 curl ipinfo.io 或者 curl ifconfig.me 即可通过IP地址检测网站提供的api获得取本机的外网IP,或者以 JSON 格式返回全部结果.

  2. 在dropDownList中实现既能输入一个新值又能实现下拉选的代码

    aspx: <div id="selDiv" style=" z-index:100; visibility:visible; clip:rect(0px 110p ...

  3. jQuery功能函数详解

    jQuery通过$.browser对象获取浏览器信息. 属性 说明msie 如果是ie为true,否则为falsemozilla 如果是mozilla相关的浏览器为true,否则为falsesafar ...

  4. KVC/KVO之KVO

    本章将分为三个部分: KVO是什么 KVO有什么用 KVO例子 KVO是什么 KVO,即NSKeyValueObserving,一个非正式的Protocol,提供一种机制来间接观察其他对象属性的变化. ...

  5. python虚拟环境的使用---virtualenv and virtualenvwrapper

    虚拟环境 virtualenv 常用操作 step1: 安装 : pip install virtualenv step2:新建 : virtualenv venv cd venv activate ...

  6. 自定义控件_水平滑动的View 自定义属性

    保持饥饿,保持愚蠢,我们对待事情本来应该就是这样的 接下来我要写一个水平滑动的自写义,实现效果 水平滑动我们有很多种实现方法,recyceryView,HorizontalScrollView都可以, ...

  7. 深搜———ZOJ 1004:anagrams by stack

    细节问题各种虐!! 其实就是简单的一个深搜 看成二叉树来理解:每个节点有两个枝:入栈和出栈. 剪枝操作:只有当栈顶元素和当前位置的目标字符相同时才出栈,否则就不出栈 dfs写三个参数:depth搜索深 ...

  8. Linuxyum源切换阿里云软件源

    备份本机软件源 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 下载新的CentOS-Bas ...

  9. 解决VMware Workstation虚拟机不能联网的解决办法

    在windows服务中查看,以下几个服务是否正常开启,没有就开启

  10. kubernetes基础知识:限制POD和容器运行的CPU、内存

    限制运行内存 https://kubernetes.io/docs/tasks/configure-pod-container/assign-memory-resource/ 先看一个pod的yaml ...