3218: a + b Problem

14 篇文章 0 订阅
12 篇文章 0 订阅

3218: a + b Problem

Time Limit: 20 Sec   Memory Limit: 40 MB
Submit: 1144   Solved: 443
[ Submit][ Status][ Discuss]

Description

Input

Output

Sample Input

Sample Output

HINT

Source

[ Submit][ Status][ Discuss]

Ans = ∑bi(i为黑色) + ∑wi(i为白色) - ∑pi(i奇怪)
很容易想到要用网络流解,但是
不会建模呀。。。死都想不到

Ans = ∑(bi + wi) - ∑bi(i为白色) - ∑wi(i为黑色) - ∑pi(i奇怪)
想要让Ans尽可能大,那就是减掉的东西尽可能小啦
于是联系到最小割,,于是联系到网络流
对于每个点i,
从源点连一条容量为bi的边
从i向汇点连一条容量为wi的边
分别表示,若处于S割则该点染成黑色,若处于T割则该点染成白色
题中还有一个限制条件————奇怪的点
对于每个点i建立一个辅助点i',从i向i'连一条容量为pi的边
从i'向每一个影响它的j连容量为INF的边
这样就保证了i'和每一个j必属于同一个割集
若要染黑i同时将任意j染成白,则一定要支付p的代价

不过这样建图边是n^2级别的,此题的内存,,,污
事实上对每个点有影响的点存在与一个题目给的区间里面
把区间放在线段树上自然被切成log段
那么从辅助点i'出发后,不直接连那些j,而是连接这log个区间,内存开销也就解决了
但是对于每个点,这些区间里面的信息是不断更新的,
所以上个主席树
就是我们在原图里面多放了一棵主席树啦


一开始写的时候主席树没有向以前的信息连边。。。好蠢啊,,WA了半小时
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
 
const int maxn = 5E3 + 50;
const int maxm = 2E6 + 5E5;
const int INF = ~0U>>1;
const int T = 22;
 
struct E{
    int to,cap,flow;
    E(){}
    E(int to,int cap,int flow): to(to),cap(cap),flow(flow){}
}edgs[maxm];
 
int n,m,s,t,tot,cnt,Cnt,Cur = 1,ans,cur[maxn*T],L[maxn*T],l[maxn],p[maxn]
    ,Root[maxn],lc[maxn*T],rc[maxn*T],r[maxn],a[maxn],vis[maxn*T],A[maxn*T];
 
queue <int> Q;
vector <int> v[maxn*T];
 
void Add(int x,int y,int cap)
{
    v[x].push_back(cnt); 
    edgs[cnt++] = E(y,cap,0);
    v[y].push_back(cnt);
    edgs[cnt++] = E(x,0,0);
}
 
int Insert(int o,int l,int r,int pos,int Num)
{
    int ret = ++tot;
    if (l == r) {
        Add(ret,Num,INF);
        if (o) Add(ret,o,INF);
        return ret;
    }
    int mid = (l + r) >> 1;
    if (pos <= mid) {
        rc[ret] = rc[o];
        lc[ret] = Insert(lc[o],l,mid,pos,Num);
        Add(ret,lc[ret],INF);
        if (rc[ret]) Add(ret,rc[ret],INF);
        if (lc[o]) Add(ret,lc[o],INF);
    }
    else {
        lc[ret] = lc[o];
        rc[ret] = Insert(rc[o],mid+1,r,pos,Num);
        Add(ret,rc[ret],INF);
        if (lc[ret]) Add(ret,lc[ret],INF);
        if (rc[o]) Add(ret,rc[o],INF);
    }
    return ret;
}
 
void Build(int o,int l,int r,int bl,int br,int Now)
{
    if (!o) return;
    if (bl <= l && r <= br) {
        Add(Now,o,INF);
        return;
    }
    int mid = (l + r) >> 1;
    if (bl <= mid) Build(lc[o],l,mid,bl,br,Now);
    if (br > mid) Build(rc[o],mid + 1,r,bl,br,Now);
}
 
bool BFS()
{
    vis[s] = ++Cnt; L[s] = 1;
    Q.push(s);
    while (!Q.empty()) {
        int k = Q.front(); Q.pop();
        for (int i = 0; i < v[k].size(); i++) {
            E e = edgs[v[k][i]];
            if (e.cap == e.flow) continue;
            if (vis[e.to] == Cnt) continue;
            vis[e.to] = Cnt;
            L[e.to] = L[k] + 1;
            Q.push(e.to);
        }
    }
    return vis[t] == Cnt;
}
 
int Dicnic(int x,int a)
{
    if (x == t) return a;
    int flow = 0;
    for (int &i = cur[x]; i < v[x].size(); i++) {
        E &e = edgs[v[x][i]];
        if (e.cap == e.flow) continue;
        if (L[e.to] != L[x] + 1) continue;
        int f = Dicnic(e.to,min(a,e.cap - e.flow));
        if (!f) continue;
        e.flow += f;
        edgs[v[x][i]^1].flow -= f;
        a -= f;
        flow += f;
        if (!a) return flow;
    }
    if (!flow) L[x] = -1;
    return flow;
}
 
int getint()
{
	char ch = getchar();
	int ret = 0;
	while (ch < '0' || '9' < ch) ch = getchar();
	while ('0' <= ch && ch <= '9')
		ret = ret*10 + ch - '0',ch = getchar();
	return ret;
}
 
int main()
{
    #ifdef DMC
        freopen("DMC.txt","r",stdin);
    #endif
     
    n = getint();
    s = 0; t = n + 1;
    for (int i = 1; i <= n; i++) {
        int b,w;
        a[i] = getint();
        b = getint();
        w = getint();
        l[i] = getint();
        r[i] = getint();
        p[i] = getint();
        //scanf("%d%d%d%d%d%d",&a[i],&b,&w,&l[i],&r[i],&p[i]);
        A[++tot] = a[i];
        A[++tot] = l[i];
        A[++tot] = r[i];
        Add(s,i,b);
        Add(i,t,w);
        ans += b;
        ans += w;
    }
    sort(A + 1,A + tot + 1);
    for (int i = 2; i <= tot; i++)
        if (A[i] != A[i-1])
            A[++Cur] = A[i];
    tot = n + 1;
    for (int i = 1; i <= n; i++) {
        int pos = lower_bound(A + 1,A + Cur + 1,a[i]) - A;
        Root[i] = Insert(Root[i-1],1,Cur,pos,i);
        int New = ++tot;
        Add(i,New,p[i]);
        int posl = lower_bound(A + 1,A + Cur + 1,l[i]) - A;
        int posr = lower_bound(A + 1,A + Cur + 1,r[i]) - A;
        Build(Root[i-1],1,Cur,posl,posr,New);
    }
     
    int MaxFlow = 0;
    while (BFS()) {
        for (int i = 0; i <= tot; i++) cur[i] = 0;
        MaxFlow += Dicnic(s,INF);
    }
    cout << ans - MaxFlow;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值