tle了。不知道错哪TT
样例
//输入
2
4 3
1 2 3
1 2 1
2 3 2
2 4 1
2 3 5
2 4 1
4 1 1
4 3
1 2 3
1 2 2
2 3 1
2 4 1
2 3 2
2 4 1
4 1 1
//输出
5
3
蒟蒻の代码
#include<bits/stdc++.h>
using namespace std;
const int N = 2010, inf = 1e9;
int T, n, k, cnt, cur[N], lst[N], dep[N], x, y, z, ans, s, t, tot;
bool f[N];
struct node
{
int to, nxt, w;
} e[N * 4];
void ade(int x, int y, int c)
{
e[++ cnt].nxt = lst[x];
e[cnt].to = y;
e[cnt].w = c;
lst[x] = cnt;
}
bool bfs()
{
queue<int> q;
memset(dep, 0, sizeof(dep));
q.push(s); dep[s] = 1;
while(! q.empty())
{
int u = q.front(); q.pop();
for(int i = lst[u]; i; i = e[i].nxt)
{
int y = e[i].to;
if(e[i].w && ! dep[y])
{
dep[y] = dep[u + 1];
if(t == y) return 1;
q.push(y);
}
}
}
return 0;
}
int dfs(int x, int mx)
{
if(x == t || ! mx) return mx;
int ret = 0;
for(int & i = cur[x]; i; i = e[i].nxt)
{
int y = e[i].to;
if(e[i].w && dep[y] == dep[x] + 1)
{
int f = dfs(y, min(e[i].w, mx - ret));
if(! f) dep[y] = -1;
e[i].w -= f;
e[i ^ 1].w += f;
ret += f;
if(ret == mx) break;
}
}
return ret;
}
int dinic()
{
int ss = 0;
while(bfs())
{
memcpy(cur, lst, sizeof(lst));
ss += dfs(s, inf);
}
return ss;
}
struct edge
{
int u, v, w, d;
}b[N], a[N];
int main()
{
cin >> T;
while(T --)
{
memset(f, 0, sizeof(f));
cnt = 0; ans = 0; tot = 0;
cin >> n >> k;
for(int i = 1, p; i <= k; i ++)
{
cin >> p;
f[p] = 1;
}
for(int i = 1; i < n; i ++)
{
cin >> a[i].u >> a[i].v >> a[i].w;
if(f[a[i].u] && f[a[i].v])
{
ans += a[i].w;
ade(s, ++ tot, a[i].w);
ade(tot, s, 0);
a[i].d = tot;
}
}
for(int i = 1; i < n; i ++)
{
cin >> b[i].u >> b[i].v >> b[i].w;
if(f[b[i].u] && f[b[i].v])
{
ans += b[i].w;
ade(++ tot, t, b[i].w);
ade(t, tot, 0);
b[i].d = tot;
}
}
s = tot + 1, t = tot + 2;
for(int i = 1; i < n; i ++)
for(int j = 1; j < n; j ++)
if(a[i].u == b[j].u || a[i].v == b[j].u || a[i].u == b[j].v || a[i].v == b[j].v)
ade(a[i].d, b[j].d, inf), ade(b[j].d, a[i].d, 0);
cout << ans - dinic() << "\n";
}
return 0;
}