zoukankan      html  css  js  c++  java
  • [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便捷实现。

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

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

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

      1 /*by SilverN*/
      2 #include<algorithm>
      3 #include<iostream>
      4 #include<cstring>
      5 #include<cstdio>
      6 #include<cmath>
      7 #define LL long long
      8 #define fou(x,i,j) for(int x=i;x<=j;++x)
      9 #define fo(x,i,j) for(int x=i;x>=j;--x)
     10 using namespace std;
     11 const int mxn=50005;
     12 int read(){
     13     int x=0,f=1;char ch=getchar();
     14     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
     15     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
     16     return x*f;
     17 }
     18 LL readL(){
     19     LL x=0,f=1;char ch=getchar();
     20     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
     21     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
     22     return x*f;
     23 }
     24 struct edge{
     25     int v,nxt;
     26     LL dis;
     27 }e[mxn<<1];
     28 int hd[mxn],mct=0;
     29 void add_edge(int u,int v,LL dis){
     30     e[++mct].v=v;e[mct].nxt=hd[u];e[mct].dis=dis;hd[u]=mct;
     31     return;
     32 }
     33 //
     34 int n,m;
     35 int pos[mxn];
     36 //
     37 int dep[mxn];
     38 LL dis[mxn];
     39 int fa[mxn][17];
     40 void DFS(int u,int ff){
     41     dep[u]=dep[ff]+1;
     42     for(int i=hd[u];i;i=e[i].nxt){
     43         int v=e[i].v;
     44         if(v==ff)continue;
     45         fa[v][0]=u;
     46         dis[v]=dis[u]+e[i].dis;
     47         DFS(v,u);
     48     }
     49     return;
     50 }
     51 void LCA_init(){
     52     fou(i,1,16)
     53         fou(j,1,n)
     54             fa[j][i]=fa[fa[j][i-1]][i-1];
     55     return;
     56 }
     57 struct cme{
     58     int come;
     59     LL dis;
     60 }ari[mxn];//上溯时间 
     61 int cnt;
     62 int cmp1(const cme a,const cme b){return a.dis<b.dis;}
     63 struct tpro{
     64     int city,dis;
     65 }c[mxn];
     66 int cmp2(const tpro a,const tpro b){return a.dis<b.dis;}
     67 int cct=0;
     68 //
     69 int nps[mxn];
     70 bool pro[mxn];//受保护的城市 
     71 void find(int u,int ff){
     72     if(!e[hd[u]].nxt)return;
     73     bool flag=1;
     74     for(int i=hd[u];i;i=e[i].nxt){
     75         int v=e[i].v;
     76         if(v==ff)continue;
     77 //        printf("u %d to %d   %d
    ",u,v,pro[v]);
     78         if(pro[v]){continue;}
     79         find(v,u);
     80         if(!pro[v])flag=0;
     81     }
     82     pro[u]=flag;
     83     return;
     84 }
     85 
     86 bool solve(LL lim){
     87 //    printf("
    
    test lim:%d
    ",lim);
     88     memset(pro,0,sizeof pro);
     89     memset(ari,0,sizeof ari);
     90     memcpy(nps,pos,sizeof pos);
     91     cnt=0;cct=0;
     92     for(int i=1;i<=m;i++){
     93         if(dis[nps[i]]>lim){//时间不够回到首都 
     94             LL lft=dis[nps[i]]-lim;
     95 //            printf("%d left:%d
    ",i,lft);
     96             fo(j,16,0)
     97                 if(dis[fa[nps[i]][j]]>=lft)
     98                  nps[i]=fa[nps[i]][j];
     99             pro[nps[i]]=1;
    100 //            printf("num %d protected %d
    ",i,nps[i]);
    101         }
    102         else{//时间够回到首都
    103             ari[++cnt].dis=lim-dis[nps[i]];
    104             fo(j,16,0){
    105 //                printf("%d %d %d
    ",fa[nps[i]][j],dep[fa[nps[i]][j]],nps[i]);
    106                 if(dep[fa[nps[i]][j]]>1)nps[i]=fa[nps[i]][j];
    107             }
    108             ari[cnt].come=nps[i];
    109 //            printf("%d comes from :%d
    ",i,nps[i]);
    110         }
    111     }
    112     find(1,0);
    113 //    printf("fin1
    ");
    114     if(pro[1])return true;
    115 //    printf("nxt:
    ");
    116     for(int i=hd[1];i;i=e[i].nxt){
    117         int v=e[i].v;
    118         if(!pro[v]) c[++cct].city=v,c[cct].dis=e[i].dis;
    119     }
    120     sort(ari+1,ari+cnt+1,cmp1);
    121     sort(c+1,c+cct+1,cmp2);
    122 /*    printf("tpro:
    ");
    123     for(int i=1;i<=cct;i++){
    124         printf("%d ",c[i].city);
    125     }
    126     printf("
    ");*/
    127     int j=1;
    128     for(int i=1;i<=cnt;i++){
    129         if(!pro[ari[i].come]){
    130             pro[ari[i].come]=1;
    131             continue;
    132         }
    133         while(pro[c[j].city] && j<=cct)j++;
    134         if(c[j].dis<=ari[i].dis)pro[c[j].city]=1,++j;
    135     }
    136     for(int i=1;i<=cct;i++){
    137         if(!pro[c[i].city])return false;
    138     }
    139     return true;
    140 }
    141 LL ans=1e11;
    142 int main(){
    143     n=read();
    144     int i,j,u,v,w;
    145     LL smm=0;
    146     for(i=1;i<n;++i){
    147         u=read();v=read();w=readL();
    148         add_edge(u,v,w);
    149         add_edge(v,u,w);
    150         smm+=w;
    151     }
    152     DFS(1,0);
    153     LCA_init();
    154     m=read();
    155     for(i=1;i<=m;++i)pos[i]=read();
    156     LL l=1,r=smm;
    157     while(l<=r){
    158         LL mid=(l+r)>>1;
    159         if(solve(mid)){
    160             ans=mid;
    161             r=mid-1;
    162         }
    163         else l=mid+1;
    164     }
    165     if(ans==1e11){printf("-1
    ");}
    166     else printf("%lld
    ",ans);
    167     return 0;
    168 }
  • 相关阅读:
    跟风!发一篇我常用VS开发技巧
    引用:程序员最常犯的五大非技术性错误
    Introduction to the Oracle Database 3
    Oracle Database 12c 12大新特性详解
    Streams全库复制
    Introduction to the Oracle Database 2
    Oracle FlashBack
    Oracle Database Features 2
    Oracle Database Features
    TNSName配置小结
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/6059413.html
Copyright © 2011-2022 走看看