洛谷 P3410 拍照(最大流 + 建图)

这道题问的是一群人要和另一群人合影,每个客人都有必须在场的人全部在场才能在场,每个客人给的有收入,但是邀请也需要支出,问最大收入?

我觉得可以总结为一类问题,就是有先决条件的网络流问题。看到费用和支出我本来以为是费用流问题,但是想着想着发现这道题似乎和费用流没什么关系。费用流的决策并不符合这里的题意。本来是想客人和主人连边,起点到客人,汇点到主人之间连上容量为客人需要主人陪的数量的边,跑一下dinic再遍历前向星检测就行了。然而开幕雷击发生了,这种做法只有60,怎么改也没能更高。

然后想了半天就感觉这道题连边应该不是0或者1的关系,可以把入边容量给上收入的大小,出边给上花费,然后跑一边,用总收入减去最大花费,因为最大流肯定给的是能办的最多的,符合dinic的定义。

然后就愉快地AC了。

 

 

#include <bits/stdc++.h>
using namespace std;
#define limit (3000000 + 5)//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-4
#define FASTIO  ios::sync_with_stdio(false);cin.tie(0);
#define ff(a) printf("%d\n",a );
#define pi(a,b) pair<a,b>
#define rep(i, a, b) for(ll i = a; i <= b ; ++i)
#define per(i, a, b) for(ll i = b ; i >= a ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\acm_01\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\acm_01\\dabiao.txt", "wt", stdout)
typedef long long ll;
typedef unsigned long long ull;
inline ll read(){
    ll sign = 1, x = 0;char s = getchar();
    while(s > '9' || s < '0' ){if(s == '-')sign = -1;s = getchar();}
    while(s >= '0' && s <= '9'){x = (x << 3) + (x << 1) + s - '0';s = getchar();}
    return x * sign;
}//快读
inline void write(ll x){
    if(x < 0) putchar('-'),x = -x;
    if(x / 10) write(x / 10);
    putchar(x % 10 + '0');
}
int n,m,vs,ve,p;
int layer[limit],head[limit], cnt;
struct node{
    int to ,next;
    ll flow, w;
}edge[limit];
ll max_flow;
void add_one(int u , int v, ll flow = 0){
    edge[cnt].to = v;
    edge[cnt].next = head[u];
    edge[cnt].flow = flow;
    edge[cnt].w = 0;
    head[u] = cnt++;
}
inline void add(int u, int v, ll flow){
    add_one(u,v,flow);
    add_one(v, u,0);
}
inline void init(bool flag = true){
    if(flag){
        memset(head, -1, sizeof(head));
        cnt = 0;
    }else{
        memset(layer, -1, sizeof(layer));
    }
}
inline bool bfs(){
    init(false);
    queue<int>q;
    layer[vs] = 0;//从第0层开始
    q.push(vs);
    while (q.size()){
        int u = q.front();
        q.pop();
        traverse(u){
            int v = edge[i].to,flow = edge[i].flow;
            if(layer[v] == -1 && flow > 0){
                layer[v] = layer[u] + 1;//迭代加深
                q.push(v);
            }
        }
    }
    return ~layer[ve];
}
ll dfs(int u, ll flow){
    if(u == ve)return flow;
    ll rev_flow = 0,min_flow;
    traverse(u){
        int v =edge[i].to;
        ll t_flow = edge[i].flow;
        if(layer[v] == layer[u] + 1 && t_flow > 0){
            min_flow = dfs(v, min(flow, t_flow));
            flow -= min_flow;
            edge[i].flow -= min_flow;
            rev_flow += min_flow;
            edge[i^1].flow += min_flow;
            if(!flow)break;
        }
    }
    if(!rev_flow)layer[u] = -1;
    return rev_flow;
}
void dinic(){
    while (bfs()){
        max_flow += dfs(vs,inf);
    }
}
int val[limit],cost[limit],visited[limit];
list<int>v[105];
int main() {
#ifdef LOCAL
    FOPEN;
#endif
    m = read(), n = read();
    init();
    vs = 80001, ve = vs + 1;
    rep(i ,1,m){
        val[i] = read();
        int num;
        int cnt1 = 0;
        while (num = read()){
            ++cnt1;
            add(i, num + m , 1);//连边,从i号客人到num号
            v[i].push_back(num);
        }
        add(vs, i, v[i].size());
    }
    rep(i, 1,n){
        cost[i] = read();
        add(m + i, ve,INF);
    }
    dinic();
    int profit,price;
    profit = price = 0;
    traverse(vs){
        int vv = edge[i].to, flow = edge[i].flow;
        if(!flow){
            for(auto it : v[vv]){
                if(!visited[it]){
                    profit += val[it];
                    price += cost[it];
                    visited[it] = 1;
                }
            }
        }
    }
    ff(profit - price);
    return 0;
}
60分 Code
#include <bits/stdc++.h>
using namespace std;
#define limit (3000000 + 5)//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-4
#define FASTIO  ios::sync_with_stdio(false);cin.tie(0);
#define ff(a) printf("%d\n",a );
#define pi(a,b) pair<a,b>
#define rep(i, a, b) for(ll i = a; i <= b ; ++i)
#define per(i, a, b) for(ll i = b ; i >= a ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\acm_01\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\acm_01\\dabiao.txt", "wt", stdout)
typedef long long ll;
typedef unsigned long long ull;
inline ll read(){
    ll sign = 1, x = 0;char s = getchar();
    while(s > '9' || s < '0' ){if(s == '-')sign = -1;s = getchar();}
    while(s >= '0' && s <= '9'){x = (x << 3) + (x << 1) + s - '0';s = getchar();}
    return x * sign;
}//快读
inline void write(ll x){
    if(x < 0) putchar('-'),x = -x;
    if(x / 10) write(x / 10);
    putchar(x % 10 + '0');
}
int n,m,vs,ve,p;
int layer[limit],head[limit], cnt;
struct node{
    int to ,next;
    ll flow, w;
}edge[limit];
ll max_flow;
void add_one(int u , int v, ll flow = 0){
    edge[cnt].to = v;
    edge[cnt].next = head[u];
    edge[cnt].flow = flow;
    edge[cnt].w = 0;
    head[u] = cnt++;
}
inline void add(int u, int v, ll flow){
    add_one(u,v,flow);
    add_one(v, u,0);
}
inline void init(bool flag = true){
    if(flag){
        memset(head, -1, sizeof(head));
        cnt = 0;
    }else{
        memset(layer, -1, sizeof(layer));
    }
}
inline bool bfs(){
    init(false);
    queue<int>q;
    layer[vs] = 0;//从第0层开始
    q.push(vs);
    while (q.size()){
        int u = q.front();
        q.pop();
        traverse(u){
            int v = edge[i].to,flow = edge[i].flow;
            if(layer[v] == -1 && flow > 0){
                layer[v] = layer[u] + 1;//迭代加深
                q.push(v);
            }
        }
    }
    return ~layer[ve];
}
ll dfs(int u, ll flow){
    if(u == ve)return flow;
    ll rev_flow = 0,min_flow;
    traverse(u){
        int v =edge[i].to;
        ll t_flow = edge[i].flow;
        if(layer[v] == layer[u] + 1 && t_flow > 0){
            min_flow = dfs(v, min(flow, t_flow));
            flow -= min_flow;
            edge[i].flow -= min_flow;
            rev_flow += min_flow;
            edge[i^1].flow += min_flow;
            if(!flow)break;
        }
    }
    if(!rev_flow)layer[u] = -1;
    return rev_flow;
}
void dinic(){
    while (bfs()){
        max_flow += dfs(vs,inf);
    }
}
int val[limit],cost[limit],visited[limit];
int main() {
#ifdef LOCAL
    FOPEN;
#endif
    m = read(), n = read();
    init();
    vs = 80001, ve = vs + 1;
    rep(i ,1,m){
        val[i] = read();
        int num;
        while (num = read()){
            add(i, num + m , INF);//连边,从i号客人到num号
        }
        add(vs, i, val[i]);
    }
    rep(i, 1,n){
        cost[i] = read();
        add(m + i, ve,cost[i]);
    }
    dinic();
    int profit,price;
    profit = price = 0;
    rep(i ,1,m){
        profit += val[i];
    }
    write(profit - max_flow);
    return 0;
}
AC Code

 

posted @ 2020-08-20 15:38  tiany7  阅读(131)  评论(0编辑  收藏  举报