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$处区间求和即可. 树 ...
随机推荐
- servlet和jsp中间的交互
jsp本质上也是一个servlet, 所有的jsp页面最终会编译成一个servlet 1. jsp访问servlet jsp访问servlet比较简单通过get, post的方式直接访问servlet ...
- 状态栏,ActionBar,工具栏高度调整
1.在属性中可以这样设置更改ActionBar的高度android:layout_marginTop="?android:attr/actionBarSize" Rect fram ...
- oracle数据库查询时间sql
select * from cc_picture_info where PICTURE_SOURCE = 3 AND UPLOAD_TIME > to_date('2017-03-29 16:5 ...
- Tomcat Server启动报错:Multiple Contexts have a path of "/east".
原因是 conf/server.xml 文件中多了一个<Context></Context>标签,路径有重复,把他删掉就好了.
- 基于Consul+Upsync+Nginx实现动态负载均衡
基于Consul+Upsync+Nginx实现动态负载均衡 1.Consul环境搭建 下载consul_0.7.5_linux_amd64.zip到/usr/local/src目录 cd /usr/l ...
- maven发布项目的snapshot到nexus
1.配置发布地址信息 <repositories> <repository> <id>nexus</id> <name>Local Repo ...
- 服务端使用Zookeeper注册服务地址,客户端从Zookeeper获取可用的服务地址。
一个轻量级分布式RPC框架--NettyRpc - 阿凡卢 - 博客园 http://www.cnblogs.com/luxiaoxun/p/5272384.html 这个RPC框架使用的一些技术所解 ...
- 会议室预订系统 td 宽度 php 浏览器 兼容性
w获取浏览器标识 <style> .w > td { <?php $wua=$_SERVER['HTTP_USER_AGENT']; if(strpos($wua, 'Chro ...
- multi-paradigm
w范式 https://developer.mozilla.org/en-US/docs/Web/JavaScript https://developer.mozilla.org/zh-CN/docs ...
- 如何实现手游app瘦身?
手游服务商来说,手游包体大一直是个很困扰的问题.一款手游产品而言,包体大小和更新方式对于有效用户的转化率往往起到非常关键的作用,话说手游安装包越小,用户转化率越高,那该如何实现app瘦身呢? 工具/原 ...