[CF1288F]Red-Blue Graph

27 篇文章 0 订阅

题目

传送门 to CF

思路

最初有一个 贪心 + + + 二分图匹配 调整法的大体思路。但是继续往下推,就还是很难搞了……

其实我们可以注意到这是 线性规划。给一条边设置两个变量,给两个端点 red \text{red} red 边或者 blue \text{blue} blue 边。显然两个变量同时非零时不优,所以问题是等价转化。

线性规划可以考虑 网络流。譬如规定从 X \frak X X 部流向 Y \frak Y Y 部的流量是 red \text{red} red,那么 X \frak X X 部的 red \text{red} red 点实际上就是 “流出多于流入”。显然我们要加入 S , T S,T S,T 来使得每个点的流量守恒;那么 S → x S\rightarrow x Sx 的流量下界是 1 1 1,就可以保证 x x x 流出多于流入了。

做有上下界的最小费用可行流;显然最大流量是 O ( n + m ) \mathcal O(n+m) O(n+m) 级别的,所以总复杂度 O [ ( n + m ) 2 log ⁡ ( n + m ) ] \mathcal O[(n+m)^2\log(n+m)] O[(n+m)2log(n+m)]

代码

注意 Y \frak Y Y 部与 X \frak X X 部的连边方式是相反的;这个错了可以调试一晚上……

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cctype>
using namespace std;
# define rep(i,a,b) for(int i=(a); i<=(b); ++i)
# define drep(i,a,b) for(int i=(a); i>=(b); --i)
typedef long long llong;
inline int readint(){
	int a = 0, c = getchar(), f = 1;
	for(; !isdigit(c); c=getchar())
		if(c == '-') f = -f;
	for(; isdigit(c); c=getchar())
		a = (a<<3)+(a<<1)+(c^48);
	return a*f;
}

const int MAXN = 405;
struct Edge{
	int to, nxt, val, capa;
	Edge(int _t,int _n,int _v,int _c)
		:to(_t),nxt(_n),val(_v),capa(_c){}
	Edge() = default;
};
Edge e[MAXN*12];
int head[MAXN], cntEdge;
/// @param c capacity (instead of cost!)
void addEdge(int a,int b,int v,int c){
	// printf("%d %d %d,%d\n",a,b,v,c);
	e[cntEdge] = Edge(b,head[a],v,c);
	head[a] = cntEdge ++;
	e[cntEdge] = Edge(a,head[b],-v,0);
	head[b] = cntEdge ++;
}

int pq[MAXN<<1], dis[MAXN], pre[MAXN];
inline void pushUp(int o){
	pq[o] = (dis[pq[o<<1]] < dis[pq[o<<1|1]]) ? pq[o<<1] : pq[o<<1|1];
}
inline void reload(int o){ while(o >>= 1) pushUp(o); }
const int INF = 0x3fffffff;
int h[MAXN]; // height
void dijkstra(int x,const int n){
	rep(i,1,n) pq[i+n] = i; // index
	memset(pq+1,0,n<<2); // dis = INF
	fill(dis,dis+n+1,INF);
	for(dis[x]=0; x!=0; x=pq[1]){
		pq[x+n] = 0; reload(x+n);
		const int w = dis[x]+h[x];
		for(int i=head[x]; ~i; i=e[i].nxt){
			int &v = dis[e[i].to];
			if(e[i].capa && v > w+e[i].val-h[e[i].to]){
				v = w-h[e[i].to]+e[i].val;
				reload(e[i].to+n), pre[e[i].to] = i;
			}
		}
	}
	rep(i,1,n) if(dis[i] != INF) dis[i] = (h[i] += dis[i]);
}
/// @param sink the sink of the flow, whose index is maximum
void dinic(const int source,const int sink,int &flow,int &cost){
	while(dijkstra(source,sink), dis[sink] != INF){
		int tmp = INF; // flow of this time
		for(int i=sink; i!=source; i=e[pre[i]^1].to)
			tmp = min(tmp,e[pre[i]].capa);
		flow += tmp, cost += tmp*dis[sink];
		for(int i=sink; i!=source; i=e[pre[i]^1].to)
			e[pre[i]].capa -= tmp, e[pre[i]^1].capa += tmp;
	}
}

char col[MAXN];
int main(){
	// freopen("my.out","w",stdout);
	int n1 = readint(), n2 = readint(), m = readint();
	int red = readint(), blue = readint();
	const int source = n1+n2+1, sink = source+1;
	const int ss = sink+1, tt = ss+1;
	memset(head+1,-1,tt<<2);
	int cntr = 0, cntb = 0;
	scanf("%s",col+1); rep(i,1,n1)
		if(col[i] == 'R'){
			++ cntr, addEdge(ss,i,0,1);
			addEdge(source,i,0,INF); // no upper limit
		}
		else if(col[i] == 'B'){
			++ cntb, addEdge(i,tt,0,1);
			addEdge(i,sink,0,INF);
		}
		else addEdge(source,i,0,INF), addEdge(i,sink,0,INF);
	addEdge(source,tt,0,cntr), addEdge(ss,sink,0,cntb);
	const int need = cntr+cntb; cntr = cntb = 0;
	scanf("%s",col+1); rep(i,1,n2)
		if(col[i] == 'R'){
			++ cntr, addEdge(i+n1,tt,0,1);
			addEdge(i+n1,sink,0,INF); // OPPOSITE!
		}
		else if(col[i] == 'B'){
			++ cntb, addEdge(ss,i+n1,0,1);
			addEdge(source,i+n1,0,INF); // CAUTIOUS!
		}
		else addEdge(source,i+n1,0,INF), addEdge(i+n1,sink,0,INF);
	addEdge(source,tt,0,cntb), addEdge(ss,sink,0,cntr);
	const int zxy = cntEdge; // Respected Brilliant Queen
	for(int a,b,i=0; i!=m; ++i){
		a = readint(), b = readint();
		addEdge(a,b+n1,red,1), addEdge(b+n1,a,blue,1);
	}
	addEdge(sink,source,0,INF); // to rectify the flow
	int flow = 0, cost = 0; dinic(ss,tt,flow,cost);
	// printf("flow = %d, cost = %d\n",flow,cost);
	if(flow != need+cntr+cntb) return puts("-1"), 0;
	printf("%d\n",cost);
	for(int i=zxy; i-zxy!=(m<<2); i+=4)
		if(!e[i].capa) putchar('R');
		else if(!e[i+2].capa) putchar('B');
		else putchar('U');
	putchar('\n');
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值