2021四川省省赛F. Direction Setting 拆边+费用流
Posted kaka0010
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2021四川省省赛F. Direction Setting 拆边+费用流相关的知识,希望对你有一定的参考价值。
原题链接:https://codeforces.ml/gym/103117/problem/F
题意
给你n个图m条边,你可以任意选择边的方向,使得
∑
i
n
m
a
x
(
0
,
d
i
−
a
i
)
最
小
[
d
i
是
点
的
入
度
]
\\sum_{i}^{n}max(0,di-ai)最小[di是点的入度]
i∑nmax(0,di−ai)最小[di是点的入度]
分析
300的数据范围基本可以确定解法就是网络流,然后考虑怎么建边。当时做的时候把点拆成两个点,然后怎么选择边就不知道该怎么处理了。其实最直接有效的方法就是拆边,因为题目有二选一的性质,因此用边作为虚点,然后分别向两个顶点去连边,这样可以达成二选一的操作(思维上还是差点 )。
下面还剩下ai没有用到,从式子中分析,对于每个点来说ai相当于免费的流量,超过ai的部分需额外加费用,那么我们直接从顶点拉两条线出来到汇点,一条是ai的流向0费用,一条是无限流量1费用。
其实思维难度上还远未到金牌题的难度,做到一半就放弃了还是不应该的。
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const ll INF = 1e9;
const int N = 600 + 10;
const int M = 3e6 + 10;
const int MOD = 1e9 + 7;
int st, ed;
struct node {
int maxflow, mincost, cnt;
int vis[N], dis[N], pre[N], last[N], h[N], flow[N];
struct edge {
int to, next, cap, cos;
} e[M << 1];
void add(int u, int v, int cap, int cos) {
e[cnt].to = v;
e[cnt].cap = cap;
e[cnt].cos = cos;
e[cnt].next = h[u];
h[u] = cnt++;
e[cnt].to = u;
e[cnt].cap = 0;
e[cnt].cos = -cos;
e[cnt].next = h[v];
h[v] = cnt++;
}
void init() {
memset(h, -1, sizeof h);
cnt = 0;
mincost = maxflow = 0;
}
bool spfa() {
queue<int> q;
for (int i = 0; i <= ed; i++) dis[i] = INF, vis[i] = 0;
vis[st] = 1, dis[st] = 0, flow[st] = INF;
q.push(st);
while (q.size()) {
int u = q.front();
q.pop(); vis[u] = 0;
for (int i = h[u]; ~i; i = e[i].next) {
int v = e[i].to;
if (e[i].cap && dis[v] > dis[u] + e[i].cos) {
dis[v] = e[i].cos + dis[u];
flow[v] = min(flow[u], e[i].cap);
pre[v] = u;
last[v] = i;
if (!vis[v]) {
vis[v] = 1;
q.push(v);
}
}
}
}
if (dis[ed] != INF) return true;
else return false;
}
void MCMF() {
while (spfa()) {
int now = ed;
maxflow += flow[ed];
mincost += flow[ed] * dis[ed];
while (st != now) {
e[last[now]].cap -= flow[ed];
e[last[now] ^ 1].cap += flow[ed];
now = pre[now];
}
}
}
}mcmf;
int a[N], aa[N], bb[N];
void solve() {
int T; cin >> T; while (T--) {
int n, m; cin >> n >> m;
mcmf.init();
st = n + m + 1; ed = n + m + 2;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= m; i++) {
cin >> aa[i] >> bb[i];
mcmf.add(st, i + n, 1, 0);
mcmf.add(i+n, aa[i], 1, 0);
mcmf.add(i+n, bb[i], 1, 0);
}
for (int i = 1; i <= n; i++) {
mcmf.add(i, ed, a[i], 0);
mcmf.add(i, ed, INF, 1);
}
mcmf.MCMF();
cout << mcmf.mincost << endl;
for (int i = n+1; i <= n+m; i++) {
for (int j = mcmf.h[i]; ~j; j = mcmf.e[j].next) {
int v = mcmf.e[j].to;
if (v > n + m) continue;
if (!mcmf.e[j].cap) {
if (v == bb[i-n]) cout << 0;
else cout << 1;
break;
}
}
}
cout << endl;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifdef ACM_LOCAL
freopen("input", "r", stdin);
freopen("output", "w", stdout);
signed test_index_for_debug = 1;
char acm_local_for_debug = 0;
do {
if (acm_local_for_debug == '$') exit(0);
if (test_index_for_debug > 20)
throw runtime_error("Check the stdin!!!");
auto start_clock_for_debug = clock();
solve();
auto end_clock_for_debug = clock();
cout << "Test " << test_index_for_debug << " successful" << endl;
cerr << "Test " << test_index_for_debug++ << " Run Time: "
<< double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
cout << "--------------------------------------------------" << endl;
} while (cin >> acm_local_for_debug && cin.putback(acm_local_for_debug));
#else
solve();
#endif
return 0;
}
以上是关于2021四川省省赛F. Direction Setting 拆边+费用流的主要内容,如果未能解决你的问题,请参考以下文章
2020年第十一届蓝桥杯 - 省赛 - Python大学组 - F.成绩统计
2018年第九届蓝桥杯 - 省赛 - C/C++大学B组 - F.递增三元组