NOIP 2012 Day2 T3 疫情控制

时间:2022-05-10 08:58:42
  1 #include<cstdio>
  2 #include<iostream>
  3 #include<string>
  4 #include<cstring>
  5 #include<queue>
  6 #include<stack>
  7 #include<vector>
  8 #include<cmath>
  9 #include<algorithm>
 10 #include<iomanip>
 11 #include<bitset>
 12 using namespace std;
 13 struct data
 14 {
 15     int l;
 16     long long w;
 17 };
 18     data k,need[50005];
 19 vector <data> city[1005];
 20 int n,m,a,b,ans,kk,cnt;
 21 int tt,ff;
 22 long long c,mid,l=1,r;
 23 long long lt[50005],send[50005];
 24 int zz[50005],army[50005],dis[50005][20],f[50005][20];
 25 int dep[50005],stay[50005],one[50005],g[50005][2],sec[50005];
 26 bool vis[50005],vis2[50005],p[50005];
 27 bool comp(data x,data y)
 28 {
 29     if (x.w>=y.w)  return true;
 30     return false;
 31 }
 32 bool comp2(int x,int y)
 33 {
 34     if (x>=y)  return true;
 35     return false;
 36 }
 37 void dfs(int x)
 38 {
 39     vis[x]=true;
 40     for (int i=0;i<city[x].size();i++)
 41         if (!vis[city[x][i].l])
 42         {
 43             dep[city[x][i].l]=dep[x]+1;
 44             dis[city[x][i].l][0]=city[x][i].w;
 45             f[city[x][i].l][0]=x;
 46             dfs(city[x][i].l);
 47         }
 48 }
 49 void init()
 50 {//here  51     for (int j=1;(1<<j)<=n;j++)
 52         for(int i=1;i<=n;i++)
 53         {
 54             f[i][j]=f[f[i][j-1]][j-1];
 55             dis[i][j]=dis[f[i][j-1]][j-1]+dis[i][j-1];
 56         }
 57 }
 58 void up(int x,int s)
 59 {
 60     for (int i=0;dis[x][i]<=lt[s];i++)
 61     {
 62         p[x]=false;
 63         x=f[x][i];
 64         p[x]=true;
 65         army[s]=x;
 66         lt[s]-=dis[x][i];
 67         if (lt[s]<lt[stay[x]])  stay[x]=s;
 68         if (dep[x]==2)  return ;
 69     }
 70     return ;
 71 }
 72 int dfs2(int x)
 73 {
 74     int s=0;
 75     vis2[x]=true;
 76     for (int i=0;i<city[x].size();i++)
 77         if (!vis2[city[x][i].l])
 78         {
 79             dfs2(city[x][i].l);
 80             if (p[city[x][i].l]==false)  break;
 81             else  s++;
 82         }
 83     if (s==city[x].size()){p[x]=true;return 2;}
 84     if (p[x]==true)  return 1;
 85     return 0;
 86 }
 87 int ss()
 88 {
 89     if (ff<tt) return 0;
 90     sort(need+1,need+1+tt,comp);
 91     sort(send+1,send+1+ff,comp2);
 92     for (int i=1;i<=tt;i++)
 93         if (need[i].w>send[i])  return 1;
 94     return 0;
 95 }
 96 int check()
 97 {
 98     tt=0,ff=0;
 99     vis2[1]=true;
100         for (int i=0;i<city[1].size();i++)
101         {
102             int t=dfs2(city[1][i].l);
103             one[city[1][i].l]=t;
104             if (t==0){
105                 need[++tt].l=city[1][i].l;
106                 need[tt].w=city[1][i].w;
107             }
108         }
109     for (int i=1;i<=m;i++)
110         if (dep[army[i]]==2)
111         {
112             if (one[army[i]]==1&&stay[army[i]]!=i)
113             {
114                 if (lt[i]-g[i][0]>=0)
115                 {
116                     lt[i]-=g[i][0];
117                     send[++ff]=lt[i];
118                 }
119             }
120             if (one[army[i]]==2&&lt[i]-g[i][0]>=0)
121             {
122                 lt[i]-=g[i][0];
123                     send[++ff]=lt[i];
124             }
125         }
126     return ss();
127 }
128 int main()
129 {
130     freopen ("blockade.in","r",stdin);
131     //freopen ("blockade.out","w",stdout);
132     scanf ("%d",&n);
133     for (int i=1;i<=n-1;i++)
134     {
135         scanf ("%d%d%lld",&a,&b,&c);
136         k.l=b;k.w=c;city[a].push_back(k);
137         k.l=a;k.w=c;city[b].push_back(k);
138         r+=c;
139         if (a==1||b==1)  kk++;
140     }
141     scanf ("%d",&m);
142     if (m<kk){printf ("-1");return 0;}
143     for (int i=1;i<=m;i++)
144     {
145         scanf ("%d",&a);
146         zz[++cnt]=a;
147         army[cnt]=a;
148         p[a]=true;
149     }
150     vis[1]=true;
151     for (int i=0;i<city[1].size();i++)
152     {
153         g[city[1][i].l][0]=city[1][i].w;
154         f[city[1][i].l][0]=city[1][i].l;
155         dep[city[1][i].l]=2;
156         dfs(city[1][i].l);
157     }
158     init();//here 159     lt[0]=1e14;
160     while (l<r)
161     {
162         mid=(l+r)>>1;
163         for (int i=1;i<=cnt;i++)
164         {
165             lt[zz[i]]=mid;
166             up(zz[i],i);
167         }
168         if (check())  l=mid+1;//and here 169         else r=mid;
170     }
171     printf ("%d",r);
172     return 0;
173 }