题目: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. JavaScript入门之函数返回值

    函数返回值 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF ...

  2. WPF InitializeComponent() 方法介绍

    namespace WindowsApplication1 { /// <summary> /// Interaction logic for Window1.xaml /// </ ...

  3. Spring_day03--Spring配置c3p0连接池和dao使用jdbcTemplate

    Spring配置c3p0连接池和dao使用jdbcTemplate 1 spring配置c3p0连接池 第一步 导入jar包 第二步 创建spring配置文件,配置连接池 原始方式 (1)把代码在配置 ...

  4. open() 函数以 w+ 模式打开文件

    这种模式打开文件时,会先清空文件,然后才执行读写操作,当我们要执行读操作时,需要通过 seek() 方法将读取指针移到前面,才能读取内容 [root@localhost ~]$ cat 1.txt # ...

  5. 说说M451例程讲解之串口

    /**************************************************************************//** * @file main.c * @ve ...

  6. JSP内置对象——application,page,pageContext,config,Exception

    application对象application对象实现了用户数据的共享,可存放全局变量.application开始于服务器的启动,终止于服务器的关闭.在用户的前后链接或不同用户之间的连接中,可以对a ...

  7. js获取格式化后的当前时间

    代码如下: function getFormatDate() { var day=new Date(); var Year=0; var Month=0; var Day=0; var Hour = ...

  8. Spring源码从开始到放弃(一)

    参考<Spring技术内幕>分析. github上面有spring的源码(https://github.com/spring-projects/spring-framework) spri ...

  9. 模拟window桌面实现

    正在开发中的游戏有个全屏功能--可以在window桌面背景上运行,就像一些视频播放器在桌面背景上播放一样的,花了个上午整了个Demo放出来留个纪念. 实现功能:显示图标,双击图标执行相应的程序,右击图 ...

  10. Codeforces Round #186 (Div. 2).D

    纠结的一道dp. 状态转移方程还是比较好想的,优化比较纠结 D. Ilya and Roads time limit per test 3 seconds memory limit per test ...