[NOIP2012] 提高组 洛谷P1084 疫情控制

题目描述

H 国有 n 个城市,这 n 个城市用 n-1 条双向道路相互连通构成一棵树,1 号城市是首都,

也是树中的根节点。

H 国的首都爆发了一种危害性极高的传染病。当局为了控制疫情,不让疫情扩散到边境

城市(叶子节点所表示的城市),决定动用军队在一些城市建立检查点,使得从首都到边境

城市的每一条路径上都至少有一个检查点,边境城市也可以建立检查点。但特别要注意的是,

首都是不能建立检查点的。

现在,在 H 国的一些城市中已经驻扎有军队,且一个城市可以驻扎多个军队。一支军队可以在有道路连接的城市间移动,并在除首都以外的任意一个城市建立检查点,且只能在

一个城市建立检查点。一支军队经过一条道路从一个城市移动到另一个城市所需要的时间等

于道路的长度(单位:小时)。

请问最少需要多少个小时才能控制疫情。注意:不同的军队可以同时移动。

输入输出格式

输入格式:

第一行一个整数 n,表示城市个数。

接下来的 n-1 行,每行 3 个整数,u、v、w,每两个整数之间用一个空格隔开,表示从

城市 u 到城市 v 有一条长为 w 的道路。数据保证输入的是一棵树,且根节点编号为 1。

接下来一行一个整数 m,表示军队个数。

接下来一行 m 个整数,每两个整数之间用一个空格隔开,分别表示这 m 个军队所驻扎

的城市的编号。

输出格式:

共一行,包含一个整数,表示控制疫情所需要的最少时间。如果无法控制疫情则输出-1。

输入输出样例

输入样例#1:
4
1 2 1
1 3 2
3 4 3
2
2 2
输出样例#1:
3

说明

【输入输出样例说明】

第一支军队在 2 号点设立检查点,第二支军队从 2 号点移动到 3 号点设立检查点,所需

时间为 3 个小时。

【数据范围】

保证军队不会驻扎在首都。

对于 20%的数据,2≤ n≤ 10;

对于 40%的数据,2 ≤n≤50,0<w <10^5;

对于 60%的数据,2 ≤ n≤1000,0<w <10^6;

对于 80%的数据,2 ≤ n≤10,000;

对于 100%的数据,2≤m≤n≤50,000,0<w <10^9。

NOIP 2012 提高组 第二天 第三题

二分答案,看能否全部覆盖。

在限定的时间内,所有军队全速向首都行进。无法到达首都的军队要驻扎在尽可能近的位置(以尽可能控制更多边缘城市),能到达首都的军队,要根据其剩余时间选择支援哪个城市(只要走到首都的第一层子结点就能控制其下的所有边缘城市),或者选择干脆不到达首都,直接驻扎在其来路的离首都最近城市。

↑以上操作可以用LCA便捷实现。

将所有可用军队的剩余时间从小到大排序,将所有需要支援结点的距离从小到大排序,贪心选择军队去支援即可。

↑但要注意,如果一支军队的剩余时间特别少,而它来路到首都的距离特别长,那么应该让它干脆不到达首都。具体实现看代码:

(我也不知道我为什么写了这么长)

 /*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define fou(x,i,j) for(int x=i;x<=j;++x)
#define fo(x,i,j) for(int x=i;x>=j;--x)
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
LL readL(){
LL x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct edge{
int v,nxt;
LL dis;
}e[mxn<<];
int hd[mxn],mct=;
void add_edge(int u,int v,LL dis){
e[++mct].v=v;e[mct].nxt=hd[u];e[mct].dis=dis;hd[u]=mct;
return;
}
//
int n,m;
int pos[mxn];
//
int dep[mxn];
LL dis[mxn];
int fa[mxn][];
void DFS(int u,int ff){
dep[u]=dep[ff]+;
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v;
if(v==ff)continue;
fa[v][]=u;
dis[v]=dis[u]+e[i].dis;
DFS(v,u);
}
return;
}
void LCA_init(){
fou(i,,)
fou(j,,n)
fa[j][i]=fa[fa[j][i-]][i-];
return;
}
struct cme{
int come;
LL dis;
}ari[mxn];//上溯时间
int cnt;
int cmp1(const cme a,const cme b){return a.dis<b.dis;}
struct tpro{
int city,dis;
}c[mxn];
int cmp2(const tpro a,const tpro b){return a.dis<b.dis;}
int cct=;
//
int nps[mxn];
bool pro[mxn];//受保护的城市
void find(int u,int ff){
if(!e[hd[u]].nxt)return;
bool flag=;
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v;
if(v==ff)continue;
// printf("u %d to %d %d\n",u,v,pro[v]);
if(pro[v]){continue;}
find(v,u);
if(!pro[v])flag=;
}
pro[u]=flag;
return;
} bool solve(LL lim){
// printf("\n\ntest lim:%d\n",lim);
memset(pro,,sizeof pro);
memset(ari,,sizeof ari);
memcpy(nps,pos,sizeof pos);
cnt=;cct=;
for(int i=;i<=m;i++){
if(dis[nps[i]]>lim){//时间不够回到首都
LL lft=dis[nps[i]]-lim;
// printf("%d left:%d\n",i,lft);
fo(j,,)
if(dis[fa[nps[i]][j]]>=lft)
nps[i]=fa[nps[i]][j];
pro[nps[i]]=;
// printf("num %d protected %d\n",i,nps[i]);
}
else{//时间够回到首都
ari[++cnt].dis=lim-dis[nps[i]];
fo(j,,){
// printf("%d %d %d\n",fa[nps[i]][j],dep[fa[nps[i]][j]],nps[i]);
if(dep[fa[nps[i]][j]]>)nps[i]=fa[nps[i]][j];
}
ari[cnt].come=nps[i];
// printf("%d comes from :%d\n",i,nps[i]);
}
}
find(,);
// printf("fin1\n");
if(pro[])return true;
// printf("nxt:\n");
for(int i=hd[];i;i=e[i].nxt){
int v=e[i].v;
if(!pro[v]) c[++cct].city=v,c[cct].dis=e[i].dis;
}
sort(ari+,ari+cnt+,cmp1);
sort(c+,c+cct+,cmp2);
/* printf("tpro:\n");
for(int i=1;i<=cct;i++){
printf("%d ",c[i].city);
}
printf("\n");*/
int j=;
for(int i=;i<=cnt;i++){
if(!pro[ari[i].come]){
pro[ari[i].come]=;
continue;
}
while(pro[c[j].city] && j<=cct)j++;
if(c[j].dis<=ari[i].dis)pro[c[j].city]=,++j;
}
for(int i=;i<=cct;i++){
if(!pro[c[i].city])return false;
}
return true;
}
LL ans=1e11;
int main(){
n=read();
int i,j,u,v,w;
LL smm=;
for(i=;i<n;++i){
u=read();v=read();w=readL();
add_edge(u,v,w);
add_edge(v,u,w);
smm+=w;
}
DFS(,);
LCA_init();
m=read();
for(i=;i<=m;++i)pos[i]=read();
LL l=,r=smm;
while(l<=r){
LL mid=(l+r)>>;
if(solve(mid)){
ans=mid;
r=mid-;
}
else l=mid+;
}
if(ans==1e11){printf("-1\n");}
else printf("%lld\n",ans);
return ;
}
上一篇:关于function和task的说明


下一篇:Ehcache(2.9.x) - API Developer Guide, Cache Usage Patterns