2019 Petrozavodsk Winter Camp, Yandex Cup C. Diverse Singing 上下界网络流

时间:2024-10-30 17:08:00

2019 Petrozavodsk Winter Camp, Yandex Cup  C. Diverse Singing  上下界网络流

建图一共建四层 第一层为N个歌手 第二层为{pi,li} 第三层为{si,li} 第四层为M首歌

除了S和第一层与第三层与T之间的边为[1,INF] 其他边均为[0,1]

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef int JQK;
const JQK INF = ;
struct node {
int p, s, l;
bool operator <(const node &rhs) const {
if (p != rhs.p)
return p > rhs.p;
else {
if (s != rhs.s)
return s > rhs.s;
else
return l > rhs.l;
}
}
} nodecnt;
int n, m, k, s1, t1, s2, t2;
map<pair<int, int>, int> pl, sl;
map<int, pair<int, int> > plb, slb;
map<node, int> psl;
vector<int> anser;
namespace dinic {
const int MAXN = ;
const int MAXM = ;
const JQK INF = ;
int Head[MAXN], cur[MAXN], lev[MAXN], to[MAXM << ], nxt[MAXM << ], ed = ;
int S, T, SS, TT, MAXP;
JQK f[MAXM << ], totflow[MAXN];
inline void addedge(int u, int v, JQK cap) {
to[++ed] = v;
nxt[ed] = Head[u];
Head[u] = ed;
f[ed] = cap;
to[++ed] = u;
nxt[ed] = Head[v];
Head[v] = ed;
f[ed] = ;
return;
}
inline void add(int u, int v, JQK caplow, JQK captop) {
addedge(u, v, captop - caplow);
totflow[u] -= caplow, totflow[v] += caplow;
return ;
}
inline bool BFS() {
int u;
for (int i = ; i <= MAXP + ; i++) {
lev[i] = -;
}
//memset(lev, -1, sizeof(lev));
queue<int>q;
lev[S] = ;
q.push(S);
while (q.size()) {
u = q.front();
q.pop();
for (int i = Head[u]; i; i = nxt[i])
if (f[i] && lev[to[i]] == -) {
lev[to[i]] = lev[u] + ;
q.push(to[i]);
/*
if (to[i] == T)
{
return 1;
}
magic one way optimize
*/
}
}
for (int i = ; i <= MAXP + ; i++) {
cur[i] = Head[i];
}
//memcpy(cur, Head, sizeof Head);
return lev[T] != -;
}
inline JQK DFS(int u, JQK maxf) {
if (u == T || !maxf) {
return maxf;
}
JQK cnt = , tem;
for (int &i = cur[u]; i; i = nxt[i])
if (f[i] && lev[to[i]] == lev[u] + ) {
tem = DFS(to[i], min(maxf, f[i]));
maxf -= tem;
f[i] -= tem;
f[i ^ ] += tem;
cnt += tem;
if (!maxf) {
break;
}
}
if (!cnt) {
lev[u] = -;
}
return cnt;
}
JQK Dinic() {
JQK ans = ;
while (BFS()) {
ans += DFS(S, INF);
}
return ans;
}
void del(int x) {
for (int i = Head[x]; i; i = nxt[i])
f[i] = f[i ^ ] = ;
}
void init(int S1, int T1, int S2, int T2) {
for (int i = ; i <= MAXP + ; i++) {
totflow[i] = Head[i] = ;
}
ed = ;
S = S1;
T = T1;
SS = S2;
TT = T2;
return;
}
inline JQK pushdownflow(int x) {
JQK sum = ;
for (int i = ; i <= x; i++) {
if (totflow[i] > ) {
addedge(SS, i, totflow[i]);
sum += totflow[i];
} else if (totflow[i] < ) {
addedge(i, TT, -totflow[i]);
}
}
return sum;//sum==dinic 循环流有解
}
JQK work() { //有源汇最大流
JQK ans = ;
JQK sum = pushdownflow(TT - );
addedge(T, S, INF);
swap(S, SS), swap(T, TT);
if (sum != Dinic()) { //Dinic(SS,TT) 求出一个有源汇有上下界可行流.此时的流不一定最大.
cout << - << endl;
return ;
}
ans = f[ed]; //可行流流量
f[ed] = f[ed ^ ] = ;
swap(S, SS), swap(T, TT); //Dinic(S,T)
ans += Dinic();//新增广出的s-t流量
cout << ans << endl;
for (int i = n + m + ; i <= n + m + k; i++) {
for (int j = Head[i]; j; j = nxt[j]) {
int v = to[j];
if (v >= n + m + k + && v <= n + m + * k && f[j] == ) {
nodecnt.p = plb[i].first;
nodecnt.s = slb[v].first;
nodecnt.l = slb[v].second;
anser.push_back(psl[nodecnt]);
}
}
}
sort(anser.begin(), anser.end());
for (int v : anser) {
cout << v << " ";
}
return ans;
}
}
int main() {
int u, v;
JQK c;
scanf("%d %d %d", &n, &m, &k);
dinic::MAXP = n + m + * k + ;
s1 = n + m + * k + ;
t1 = s1 + ;
s2 = t1 + ;
t2 = s2 + ;
dinic::init(s1, t1, s2, t2); //S T SS TT
for (int i = ; i <= n; i++)
dinic::add(s1, i, , INF);
for (int i = n + ; i <= n + m; i++)
dinic::add(i, t1, , INF);
for (int i = ; i <= k; i++) {
scanf("%d %d %d", &u, &v, &c);
nodecnt.p = u, nodecnt.s = v, nodecnt.l = c;
if (!psl[nodecnt])
psl[nodecnt] = i;
if (!pl[make_pair(u, c)]) {
pl[make_pair(u, c)] = n + m + i;
plb[n + m + i] = make_pair(u, c);
dinic::add(u, n + m + i, , );
}
if (!sl[make_pair(v, c)]) {
sl[make_pair(v, c)] = n + m + k + i;
slb[n + m + k + i] = make_pair(v, c);
dinic::add(n + m + k + i, n + v, , );
}
dinic::add(pl[make_pair(u, c)], sl[make_pair(v, c)], , );
}
dinic::work();
return ;
}