codeforces 863F Almost Permutation

Recently Ivan noticed an array a while debugging his code. Now Ivan can't remember this array, but the bug he was trying to fix didn't go away, so Ivan thinks that the data from this array might help him to reproduce the bug.

Ivan clearly remembers that there were n elements in the array, and each element was not less than 1 and not greater than n. Also he remembers q facts about the array. There are two types of facts that Ivan remembers:

1 li ri vi — for each x such that li ≤ x ≤ ri ax ≥ vi;
2 li ri vi — for each x such that li ≤ x ≤ ri ax ≤ vi.
Also Ivan thinks that this array was a permutation, but he is not so sure about it. He wants to restore some array that corresponds to the q facts that he remembers and is very similar to permutation. Formally, Ivan has denoted the cost of array as follows:

, where cnt(i) is the number of occurences of i in the array.

Help Ivan to determine minimum possible cost of the array that corresponds to the facts!

Input
The first line contains two integer numbers n and q (1 ≤ n ≤ 50, 0 ≤ q ≤ 100).

Then q lines follow, each representing a fact about the array. i-th line contains the numbers ti, li, ri and vi for i-th fact (1 ≤ ti ≤ 2, 1 ≤ li ≤ ri ≤ n, 1 ≤ vi ≤ n, ti denotes the type of the fact).

Output
If the facts are controversial and there is no array that corresponds to them, print -1. Otherwise, print minimum possible cost of the array.

Examples
input
3 0
output
3
input
3 1
1 1 3 2
output
5
input
3 2
1 1 3 2
2 1 3 2
output
9
input
3 2
1 1 3 2
2 1 3 1
output

-1

题意:条件1: 给定 L,R,V 对所以 L<=x<=R 的ax 有 ax >= V

            条件2: 给定 L,R,V 对所以 L<=x<=R 的ax 有 ax <= V

         求满足条件的数组的最小价值。 对于 数组a的价值 为:  SUM( cnt(x) ) ,cnt(x)为x在数组a中出现的次数。

分析:费用与流量平方成正比的最小费用流 。算法竞赛入经典 训练指南 p366.

#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#include<queue>
const int maxn = 10000;
const int inf = 1e9;
typedef long long ll;
using namespace std;
struct Edge
{
    int fr,to,cap,flow,cost;
};
struct MCMF
{
    int n,m;
    vector<Edge>edges;
    vector<int>G[maxn+5];
    int inq[maxn+5],d[maxn+5],p[maxn+5],a[maxn+5];

    void Init(int n)
    {
        for(int i=0; i<=maxn; i++) G[i].clear();
        edges.clear();
    }

    void Addedge(int fr,int to,int cap,int cost)
    {
        edges.push_back((Edge)
        {
            fr,to,cap,0,cost
        });
        edges.push_back((Edge)
        {
            to,fr,0,0,-cost
        });
        m = edges.size();
        G[fr].push_back(m-2);
        G[to].push_back(m-1);
    }

    bool Bell(int s,int t,int &flow,int &cost)
    {
        for(int i=0; i<=maxn; i++) d[i] = inf;
        memset(inq,0,sizeof(inq));
        d[s] = 0, inq[s] = 1,p[s] = 0, a[s] = inf;
        queue<int>Q;
        Q.push(s);
        while(!Q.empty())
        {
            int u = Q.front();
            Q.pop();
            inq[u] = 0;
            for(int i=0,l=G[u].size(); i<l; i++)
            {
                Edge &e = edges[G[u][i]];
                if(e.cap>e.flow&&d[e.to]>d[u]+e.cost)
                {
                    d[e.to] = d[u] + e.cost;
                    p[e.to] = G[u][i];
                    a[e.to] = min(a[u],e.cap-e.flow);
                    if(!inq[e.to])
                    {
                        Q.push(e.to);
                        inq[e.to] = 1;
                    }
                }
            }
        }
        if(d[t]==inf) return false;
        flow+=a[t];
        cost+=d[t]*a[t];
        int u = t;
        while(u!=s)
        {
            edges[p[u]].flow+=a[t];
            edges[p[u]^1].flow-=a[t];
            u = edges[p[u]].fr;
        }
        return true;
    }

    int Mincost(int s,int t,int N)
    {
        int flow = 0, cost = 0;
        while(Bell(s,t,flow,cost));
        if(flow<N) cost = -1;
        return cost;
    }
} my;
int n,m,S,T;
int l[105],r[105];
int main()
{
    while(~scanf("%d %d",&n,&m))
    {
        for(int i=1; i<=n; i++) l[i] = 1, r[i] = n;
        for(int i=1; i<=m; i++)
        {
            int t,x,y,v;
            scanf("%d %d %d %d",&t,&x,&y,&v);
            if(t==1) for(int j=x; j<=y; j++) l[j] = max(l[j],v);// 求出 ai 的范围
            if(t==2) for(int j=x; j<=y; j++) r[j] = min(r[j],v);
        }
        S = 0, T = n*2+1;
        my.Init(n*2+1);
        for(int i=1; i<=n; i++)
        {
            for(int j=l[i]; j<=r[i]; j++)
            {
                my.Addedge(j,i+n,1,0);
            }
            my.Addedge(i+n,T,1,0);
        }
        for(int i=1; i<=n; i++)// 对于每一个 i 都要与S连n条 cost = 1,3,5,7,9...2*n-1。
        {
            for(int j=1; j<=n; j++)
            {
                int cost = j*2 - 1;
                my.Addedge(S,i,1,cost);
            }
        }
        int cost = my.Mincost(S,T,n);
        printf("%d\n",cost);
    }
    return 0;
}


         

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值