锰锌 网络流玄关求调,马蜂良好
  • 板块学术版
  • 楼主Yyccccc
  • 当前回复6
  • 已保存回复6
  • 发布时间2024/8/22 19:59
  • 上次更新2024/8/22 21:59:45
查看原帖
锰锌 网络流玄关求调,马蜂良好
528814
Yyccccc楼主2024/8/22 19:59

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;
}
2024/8/22 19:59
加载中...