(Blockade.cpp/c/pas)
"Problem description"
H state-owned n cities, these n cities with n-1 two-way road interconnected form a tree, city 1th is the capital,
is also the root node in the tree.
A highly hazardous infectious disease has erupted in the capital of H country. Authorities in order to control the outbreak, not to let the epidemic spread to the border
City (the city represented by the leaf node), decided to use the army to set up checkpoints in some cities, making it from the capital to the border
Every route in the city has at least one checkpoint, and border cities can also establish checkpoints. But it is particularly important to note that
The capital is unable to establish a checkpoint.
Now, there are troops in some cities of the H country, and a city can be stationed in several armies. An army can move between cities with road connections and establish checkpoints in any city other than the capital, and only in
A city establishes checkpoints. The time required for an army to move from one city to another in a single road.
In the length of the road (unit: Hours).
It will take at least a few hours to control the outbreak. Note: Different armies can move at the same time.
Input
The input file name is blockade.in.
The first line is an integer n, which indicates the number of cities.
The next n-1 line, 3 integers per line, u, V, W, each two integers separated by a space, representing the
City u to City v there is a road that is long W. The data guarantees that a tree is entered, and the root node number is 1.
The next line is an integer m, which indicates the number of troops.
The next line of M-integers, separated by a space between each of the two integers, indicates the presence of the M army.
The number of the city.
Output
The output file is blockade.out.
A total of one row, containing an integer that represents the minimum time required to control the outbreak. If you cannot control the outbreak, output-1.
"Input and Output sample"
Blockade.in
4
1 2 1
1 3 2
3 4 3
2
2 2
Blockade.out
3
"Input and Output sample description"
The first Army set up checkpoints at point 2nd, and the Second Army moved from Point 2nd to point 3rd to set up checkpoints, required
The time is 3 hours.
The
Data range
guarantees that the army will not be stationed in the capital.
for 20% of data, 2≤n≤10;
for 40% of data, 2≤n≤50,0
#include <cstdio> #include <cstring> #include <algorithm> using namespace std;
const int maxn=50005;
int n,num,m;
BOOL VIS[MAXN],MS[MAXN];
int HEAD[MAXN],ARMY[MAXN],ROOT[MAXN],SONS[MAXN],CON[MAXN],R[MAXN],W[MAXN];
int FA[20][MAXN],DIS[20][MAXN]; struct edge{int to,v,next;}
G[MAXN];
void Adde (int u,int v,int W) {g[++num].to=v;
G[num].v=w;
G[num].next=head[u];
Head[u]=num;
} void Dfs (int x) {vis[x]=true;
for (int i=head[x];i!=-1;i=g[i].next) {if (vis[g[i].to]) continue;
Fa[0][g[i].to]=x;
DIS[0][G[I].TO]=G[I].V;
sons[x]++;
DFS (G[I].TO); }} void Beizen () {for (int. i=1;i<20;i++) {for (int j=1;j<=n;j++) {Fa[i][j]=fa[i-1][fa[i-1][j]
];
DIS[I][J]=DIS[I-1][J]+DIS[I-1][FA[I-1][J]];
}}} bool Check (int de) {memset (ms,0,sizeof (ms));
Memset (r,0,sizeof (R));
Memset (W,0,sizeof (w));
int tot=0;
printf ("%d", 1);
for (int i=1;i<=m;i++) { if (De>root[i]) tot++,r[tot]=de-root[i];
else if (De==root[i]) {int f=army[i];
for (int j=19;j>=0;j--) if (fa[j][f]!=1) f=fa[j][f];
Ms[f]=true;
} else{//printf ("%d", root[i]);
int x=de,f=army[i]; while (x>0) {for (int j=19;j>=0;j--) if (dis[j][f]<=x) {X-=di
S[J][F];
F=FA[J][F];
printf ("%d", j);
} if (x-dis[0][f]<0) break;
}//printf ("%d", x);
printf ("%d%d\n", f,army[i]);
while (sons[fa[0][f]]==1&&fa[0][f]!=1) f=fa[0][f];
if (fa[0][f]==1) ms[f]=true;
printf ("%d", f);
}}//printf ("-1");
int tu=0;
for (int i=head[1];i!=-1;i=g[i].next) {if (ms[g[i].to]==false) tu++,w[tu]=g[i].v;
} if (Tu>tot) return false; Sort (r+1,r+tOT+1);
Sort (w+1,w+tu+1);
int j=1;
for (int. i=1;i<=tu;i++) {while (R[j]<w[i]&&j<=tot) j + +;
if (J>tot) return false;
} return true;
} int main () {memset (head,-1,sizeof (head));
memset (vis,0,sizeof (VIS));
Freopen ("Blockade.in", "R", stdin);
Freopen ("Blockade.out", "w", stdout);
scanf ("%d", &n);
num=0;
Fa[0][1]=1;
for (int i=1;i<=n-1;i++) {int u,v,w;
scanf ("%d%d%d", &u,&v,&w);
Adde (U,V,W);
Adde (V,U,W);
} int tot=0;
for (int i=head[1];i!=-1;i=g[i].next) tot++;
scanf ("%d", &m);
if (tot>m) {printf ("-1");
return 0;
} dfs (1);
Beizen ();
int maxr=-1;
for (int i=1;i<=m;i++) scanf ("%d", &army[i]);
for (int i=1;i<=m;i++) {root[i]=dis[19][army[i]];
Maxr=max (Maxr,root[i]);
}//printf ("%d", check (9));
int LEFT=1,RIGHT=6*MAXR; while (left<right) {int mid= (left+right)/2;
printf ("m=%d \ n", mid);
if (check (mid)) right=mid-1;
else left=mid+1;
} printf ("%d", right);
return 0; }