WC模拟(1.2) T2 变量

变量

题目背景:

1.2 WC模拟T2

分析:网络流,最大流最小割

 

其实这个题····一看就是网络流最小割······结果我考场上脑子里全部是拆点,然后开心的没有想出建图······尴尬脸。

考虑,显然,W是来搞笑的,最后乘上去就好了,对于每一个变量建一个点i,显然每个等式后面的三项是可以直接提出来一起考虑的,对于每一个答案w[i]统计其贡献cnt[i],对于cnt[i]的正负性,讨论后选择在à i,和à T中的一条边上加上2 * abs(cnt[i]),然后对于一个等式a * |w[x] - w[y]|,我们在x, y之间连2 * a的无向边,表示,如果选择两个不同的方向则一定也要割掉中间的一条边,否则选择相同两个。然后解决变量间的限制,对于x < y直接强制xTINF的边,SyINF的边就可以了,如果对于x <= y,等价于不能存在x = W, y = -W的情况,那么所以直接xyINF的有向边,对于x == y,直接xyINF的边,yxINF的边就好了。然后原图最小割加上一开始默认值的答案就可以了。

Source:

 

/*
	created by scarlyw
*/
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <set>
#include <queue>
#include <ctime>
#include <bitset>

inline char read() {
	static const int IN_LEN = 1024 * 1024;
	static char buf[IN_LEN], *s, *t;
	if (s == t) {
		t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
		if (s == t) return -1;
	}
	return *s++;
}

///*
template<class T>
inline void R(T &x) {
	static char c;
	static bool iosig;
	for (c = read(), iosig = false; !isdigit(c); c = read()) {
		if (c == -1) return ;
		if (c == '-') iosig = true;	
	}
	for (x = 0; isdigit(c); c = read()) 
		x = ((x << 2) + x << 1) + (c ^ '0');
	if (iosig) x = -x;
}
//*/

const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN], *oh = obuf;
inline void write_char(char c) {
	if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
	*oh++ = c;
}

template<class T>
inline void W(T x) {
	static int buf[30], cnt;
	if (x == 0) write_char('0');
	else {
		if (x < 0) write_char('-'), x = -x;
		for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
		while (cnt) write_char(buf[cnt--]);
	}
}

inline void flush() {
	fwrite(obuf, 1, oh - obuf, stdout);
}

/*
template<class T>
inline void R(T &x) {
	static char c;
	static bool iosig;
	for (c = getchar(), iosig = false; !isdigit(c); c = getchar())
		if (c == '-') iosig = true;	
	for (x = 0; isdigit(c); c = getchar()) 
		x = ((x << 2) + x << 1) + (c ^ '0');
	if (iosig) x = -x;
}
//*/

const int MAXN = 500 + 10;
const int INF = 1000000000;

struct node {
	int to, w, rev;
	node(int to = 0, int w = 0, int rev = 0)
		 : to(to), w(w), rev(rev) {}
} ;

int x, y, z, a, b, c, d, e, f, r, n, val, p, q, s, t, ans, data_case;
int temp[MAXN], dis[MAXN], cnt[MAXN];
std::vector<node> edge[MAXN];

inline void add_edge(int x, int y, int w) {
	edge[x].push_back(node(y, w, edge[y].size()));
	edge[y].push_back(node(x, 0, edge[x].size() - 1));
}

inline void build_graph() {
	R(n), R(val), R(p), R(q), s = 0, t = n + 1, ans = 0;
	for (int i = s; i <= t; ++i) edge[i].clear(), cnt[i] = 1;
	for (int i = 1; i <= p; ++i) {
		R(x), R(y), R(z), R(a), R(b), R(c), R(d), R(e), R(f);
		add_edge(x, y, 2 * a), add_edge(y, x, 2 * a);
		add_edge(y, z, 2 * b), add_edge(z, y, 2 * b);
		add_edge(z, x, 2 * c), add_edge(x, z, 2 * c);
		cnt[x] += (d - f), cnt[y] += (e - d), cnt[z] += (f - e);
	}
	for (int i = 1; i <= n; ++i)
		(cnt[i] > 0) ? (ans -= cnt[i], add_edge(i, t, 2 * cnt[i]))
			 : (ans += cnt[i], add_edge(s, i, -2 * cnt[i]));
	for (int i = 1; i <= q; ++i) {
		R(x), R(y), R(r);
		switch (r) {
			case 0: add_edge(x, y, INF);
					break ;
			case 1: add_edge(x, y, INF), add_edge(y, x, INF);
					break ;
			case 2: add_edge(s, y, INF), add_edge(x, t, INF);
		}
	}
	std::cerr << ans << '\n';
}

inline bool bfs(int s, int t) {
	memset(dis, -1, sizeof(int) * (n + 5));
	std::queue<int> q;
	dis[s] = 0, q.push(s);
	while (!q.empty()) {
		int cur = q.front();
		q.pop();
		for (int p = 0; p < edge[cur].size(); ++p) {
			node *e = &edge[cur][p];
			if (dis[e->to] == -1 && e->w > 0) {
				dis[e->to] = dis[cur] + 1;
				if (e->to == t) return true ;
				q.push(e->to);
			}
		}
	}
	return false;
}

inline int dfs(int cur, int low, int t) {
	if (cur == t) return low;
	int delta = 0;
	for (int &p = temp[cur]; p < edge[cur].size(); ++p) {
		node *e = &edge[cur][p];
		if (dis[e->to] == dis[cur] + 1 && e->w > 0) {
			int ret = dfs(e->to, std::min(low - delta, e->w), t);
			e->w -= ret, edge[e->to][e->rev].w += ret;
			if ((delta += ret) == low) break ;
		}
	}
	return delta;
}

inline int max_flow(int s, int t) {
	int ans = 0;
	while (bfs(s, t)) {
		int ret;
		while (memset(temp, 0, sizeof(int) * (n + 5)), 
			ret = dfs(s, INF, t)) 
				ans += ret;
	}
	return ans;
}

inline void solve() {
	build_graph();
	W((long long)(ans + max_flow(s, t)) * val), write_char('\n');
}

int main() {
	freopen("variable.in", "r", stdin);
	freopen("variable.out", "w", stdout);
	R(data_case);
	while (data_case--) std::cerr << data_case << '\n', solve();
	flush();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值