跳到正文

题解

Solution SP300

RuSun约 10 分钟
原文
文章信息与版本

文章信息

发布
2021年4月28日
收录
2025年12月12日
更新
2026年11月18日
篇幅
约 10 分钟
获赞
16
题目
SP300

版本

  1. v1当前

广告

我的博客

正文

已有的三篇题解做法很不优美, 并且及其相似。 事实上, 这三篇题解在建图上的方法一模一样, 都是先将所有边都存起来, 每一次再重新建图, 这是一种及其繁杂混乱的方式, 可能细节上容易出错。

而且, 没有必要将起点和终点的拆点的边赋值为不可割, 只需要从起点的出点开始, 终点的入点结束就可以了, 而这样反人类的麻烦的步骤(见下面的代码)竟然在三篇题解都出现了, 并且码风及其相似, 所以我怀疑他们在抄袭题解, 对自己和他人不负责任。

C++
for(i=0;i<n;i++){
	if(i==s||i==t)add(i,i+n,inf);
	else add(i,i+n,1);
}
for(i=0;i<m;i++)
	add(a[i]+n,b[i],inf),add(b[i]+n,a[i],inf);

我做这个题的时候, 由于未知原因一直过不了样例, 我于是向题解求助, 然而这样的题解没有给我任何帮助, 反而让我思路越来越乱。

所以满怀责任心的我决定写下这篇题解, 为后来人铺路。


题目的要求是将让任意两个电器不连通, 使得删除的电器最少。 所以, 我们联想到了最小割, 枚举两点就可以了, 但是这个题也有不少要处理的细节。

处理字符串

这可能并不是问题。 扫描一遍就可以了。

C++
PII div(string s)
{
	int x = 0, y = 0;
	bool flag = false;
	s.erase(0, 1);
	s.erase(s.size() - 1, 1);
	for (int i = 0; i < s.size(); i++)
		if (s[i] == ',')
			flag = true;
		else
		{
			if (!flag)
				x = x * 10 + s[i] - '0';
			else
				y = y * 10 + s[i] - '0';
		}
	return make_pair(x, y);
}

建边

对于每一组数据, 直接建图就可以了, 没有必要每次枚举的点。

对于拆点, 为大家介绍两种方式, 第一种是入点和出点分别为 i 和 i + n , 但是这种情况是有限制的, 只有已知所有点的个数才可以这么做。

C++
cin >> n >> m;
init();
for (int i = 0; i < n; i++)
{
	add(i, i + n, 1);
	add(i + n, i, 0);
}
for (int i = 1, a, b; i <= m; i++)
{
	string s;
	cin >> s;
	a = div(s).first;
	b = div(s).second;
	add(a + n, b, inf);
	add(b, a + n, 0);
	add(b + n, a, inf);
	add(a, b + n, 0);
}

第二种方法是入点和出点分别为 i << 1 和 i << 1 | 1, 这样就可以不用管点的个数。

C++
cin >> n >> m;
init();
for (int i = 0; i < n; i++)
{
	add(i << 1, i << 1 | 1, 1);
	add(i << 1 | 1, i << 1, 0);
}
for (int i = 1, a, b; i <= m; i++)
{
	string s;
	cin >> s;
	a = div(s).first;
	b = div(s).second;
	add(a << 1 | 1, b << 1, inf);
	add(b << 1, a << 1 | 1, 0);
	add(b << 1 | 1, a << 1, inf);
	add(a << 1, b << 1 | 1, 0);
}

他们是等价的。

枚举

那么有些同学就不禁要问了, 如果没有将边存起来, 而 dinic 将图变成了残流网络, 下一次的图就不正确了。 事实上, 我们要记住网络流的一个性质, 一条边和反向边的流量之和是不变的, 所以, 每一次枚举之后, 遍历所有边, 将反向边的流量加回去就可以了, 而反向边的流量自然就是 0 了。

C++
for (int i = 0; i < n; i++)
	for (int j = 0; j < i; j++)
	{
		st = i << 1 | 1;
		ed = j << 1;
		ans = min(ans, dinic());
		for (int k = 0; k <= idx; k += 2)
		{
			wt[k] += wt[k ^ 1];
			wt[k ^ 1] = 0;
		}
	}

小优化

在 dinic 函数中增加一个小优化。

因为我们要求最小值, 所以, 如果这一次流比当前最小值还大了, 就直接停止了。

C++
int dinic()
{
	int res = 0, flow;
	while (bfs())
		while (flow = exploit(st, inf))
		{
			if (flow == inf)
				return inf;
			else
				res += flow;
			if (res >= ans)
				return res;
		}
	return res;
}

完整代码

C++
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <climits>
#define inf INT_MAX
using namespace std;
typedef pair <int, int> PII;
const int N = 110, M = 6e3 +10;
int n, m, st, ed, ans, d[N], cur[N];
int idx, hd[N], nxt[M], edg[M], wt[M];
bool bfs()
{
	for (int i = 0; i < n << 1; i++)
		d[i] = -1;
	d[st] = 0;
	cur[st] = hd[st];
	queue <int> q;
	q.push(st);
	while (!q.empty())
	{
		int t = q.front();
		q.pop();
		for (int i = hd[t]; ~i; i = nxt[i])
			if (d[edg[i]] == -1 && wt[i])
			{
				cur[edg[i]] = hd[edg[i]];
				d[edg[i]] = d[t] + 1;
				if (edg[i] == ed)
					return true;
				q.push(edg[i]);
			}
	}
	return false;
}
int exploit(int x, int limit)
{
	if (x == ed)
		return limit;
	int res = 0;
	for (int i = cur[x]; ~i && res < limit; i = nxt[i])
	{
		cur[x] = i;
		if (d[edg[i]] == d[x] + 1 && wt[i])
		{
			int t = exploit(edg[i], min(wt[i], limit - res));
			if (!t)
				d[edg[i]] = -1;
			wt[i] -= t;
			wt[i ^ 1] += t;
			res += t;
		}
	}
	return res;
}
int dinic()
{
	int res = 0, flow;
	while (bfs())
		while (flow = exploit(st, inf))
		{
			if (flow == inf)
				return inf;
			else
				res += flow;
			if (res >= ans)
				return res;
		}
	return res;
}
void add(int a, int b, int c)
{
	nxt[++idx] = hd[a];
	hd[a] = idx;
	edg[idx] = b;
	wt[idx] = c;
}
void init()
{
	ans = n;
	idx = -1;
	for (int i = 0; i < n << 1; i++)
		hd[i] = -1;
}
PII div(string s)
{
	int x = 0, y = 0;
	bool flag = false;
	s.erase(0, 1);
	s.erase(s.size() - 1, 1);
	for (int i = 0; i < s.size(); i++)
		if (s[i] == ',')
			flag = true;
		else
		{
			if (!flag)
				x = x * 10 + s[i] - '0';
			else
				y = y * 10 + s[i] - '0';
		}
	return make_pair(x, y);
}
int main()
{
	int T;
	cin >> T;
	while (T--)
	{
		cin >> n >> m;
		init();
		for (int i = 0; i < n; i++)
		{
			add(i << 1, i << 1 | 1, 1);
			add(i << 1 | 1, i << 1, 0);
		}
		for (int i = 1, a, b; i <= m; i++)
		{
			string s;
			cin >> s;
			a = div(s).first;
			b = div(s).second;
			add(a << 1 | 1, b << 1, inf);
			add(b << 1, a << 1 | 1, 0);
			add(b << 1 | 1, a << 1, inf);
			add(a << 1, b << 1 | 1, 0);
		}
		for (int i = 0; i < n; i++)
			for (int j = 0; j < i; j++)
			{
				st = i <<1 | 1; 
				ed = j << 1;
				ans = min(ans, dinic());
				for (int k = 0; k <= idx; k += 2)
				{
					wt[k] += wt[k ^ 1];
					wt[k ^ 1] = 0;
				}
			}
		cout << ans << endl;
	}
	return 0;
}
原文
存档评论1 条抓取于