bzoj1520 [POI2006]Szk-Schools

(http://www.elijahqi.win/2018/01/02/bzoj1520-poi2006szk-schools/%20%E2%80%8E)
题目描述

There are
nn

n schools in Byteotia, and each has its number
mm

m,
1≤m≤n1\le m\le n

1≤m≤n, just as every school tends to. However, the previous king of Byteotia paid little attention to the school numbering, and generously allowed every new school to choose any number from the range
1⋯n1\cdots n

1⋯n. There is thus no guarantee that the numbers are unique, in fact it seems highly unlikely. Clearly some schools may have chosen the same number, and as a consequence some numbers from the range
1⋯n1\cdots n

1⋯n might be not used at all. A perfect numbering would be a permutation, i.e. a one that assigns each number from the specified range to exactly one school.

The newly crowned king of Byteotia wants to reform the numbering system, so that every number would be used exactly once. It is, however, not an easy task, since most schools are reluctant to change the numbers they have once chosen.

The king has sent his most trusted informants to all the schools to inquire which numbers each school would accept. Since every school would like to keep its current number (or at least a number close to it), each school director has specified an interval containing the school’s current number. These intervals are called the tolerance intervals. Furthermore, each school has stated the cost
cc

c of changing its number by
11

  1. Hence the total cost of changing a school’s number equals
    c×∣m−m′∣c\times|m-m’|

c×∣m−m′∣, where
mm

m denotes the old school number, and
m′m’

m′ the new one. Of course
m′m’

m′ must lie in the tolerance interval previously specified by the school. Now, all the information gathered, the king would like to know, if it’s possible to introduce a perfect numbering (while complying with every school’s tolerance interval), and if so, what is the minimal cost of such reenumaration. He has asked you - the royal computer scientist - to carefully study the information he has provided you with and give the answer in return.

TaskWrite a programme which:

reads from the standard input the current numbers of Byteotian schools, their tolerance intervals and the costs of changing their numbers by ,checks if it’s possible to perfectly reenumerate the schools complying with all previously described conditions, and if so, determines the minimal cost of such reenumeration,writes the result to the standard output.

有n间学校,每个学校都有自己的数字m, 1 <=m <= n.因为以前 的国王疏于管理,所以准许每间新学校从1- n任意的选数字.所以并不能保证 所有学校的数字都唯一.一个充美的标号应该每个学校都有一个唯一的数字, 即他们是一个1- n的排列.新的国王想改革数字系统,他想使学校标号成为一个 完美标号,但这并不是一个简单的工作,因为大多数的学较非常不情愿改变他们 曾经选择的标号.国王派出了他最信任的助手到学校去询问每个学校能接受哪些 标号.因为每个学校都想尽量保持自己原有标号(或者是一个尽量和其接近的标 号),所以每个学校都给出了一个它能接受的标号区间.这些区间称为可忍受区 间.此外,每个学校都给出了一个整数c表示将编号改变1的代价.所以一个学校的总的改变代价应该为c|m-m’|,这里的m是学校的老编号,m’为新编号. 当然W必须包含在可忍受区间之内.现在国王己经知道了所有的信息,他想知道 将卑梭编号改变为完美标号的最小代价.当不能满足所有条件时输出NIE
输入输出格式

输入格式:
The first line of the standard input contains one integer
nn

n (
1≤n≤2001\le n\le 200

1≤n≤200), denoting the number of schools in Byteotia. In the following
nn

n lines there are descriptions of the schools themselves. The line no.
i+1i+1

i+1 (
1≤i≤n1\le i\le n

1≤i≤n) contains four integers
mim_i

mi​,
aia_i

ai​,
bib_i

bi​ and
kik_i

ki​ (
1≤ai≤mi≤bi≤n1\le a_i\le m_i\le b_i\le n

1≤ai​≤mi​≤bi​≤n,
1≤ki≤1 0001\le k_i\le 1\ 000

1≤ki​≤1 000), separated by single spaces. These denote respectively: the current number of the
ii

i’th school, the left and right endpoint of the
ii

i’th school’s tolerance interval regarding the change of number (it is a closed interval, therefore the new
ii

i’th school’s number
mi′m’_i

mi′​ must satisfy the inequality
ai≤mi′≤bia_i\le m’_i\le b_i

ai​≤mi′​≤bi​) and the cost of changing the number of
ii

i’th school by
11

1.

输出格式:
If a reenumeration satisfying the above conditions is possible, the programme should write one integer
kk

k denoting the minimum possible cost of performing the operation. If it’s not possible, it should write the word NIE, which means no in Polish.
输入输出样例

输入样例#1: 复制

5
1 1 2 3
1 1 5 1
3 2 5 5
4 1 5 10
3 3 3 1

输出样例#1: 复制

9

费用流裸题吧

学校看成1~N的点 新的编号看成1~n的点 然后给每个学校都去向新的编号区间连边即可

费用就是 c*(标号之差) 跑一下费用流


#include<cmath>
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 440
#define inf 0x3f3f3f3f
using namespace std;
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0;char ch=gc();
    while(ch<'0'||ch>'9') ch=gc();
    while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gc();}
    return x;
}
struct node{
    int y,z,next,c;
}data[N*N*3];
int n,num=1,T,h[N],f[N],pre[N],path[N];bool flag[N];
inline void insert1(int x,int y,int z,int c){
    data[++num].y=y;data[num].z=z;data[num].next=h[x];data[num].c=c;h[x]=num;
    data[++num].y=x;data[num].z=0;data[num].next=h[y];data[num].c=-c;h[y]=num;
}
inline bool spfa(){
    memset(flag,0,sizeof(flag));memset(f,0x3f,sizeof(f));queue<int>q;memset(pre,-1,sizeof(pre));q.push(0);flag[0]=1;f[0]=0;
    while(!q.empty()){
        int x=q.front();q.pop();flag[x]=0;
        for (int i=h[x];i;i=data[i].next){
            int y=data[i].y,z=data[i].z,c=data[i].c;
            if (f[x]+c<f[y]&&z){
                f[y]=f[x]+c;pre[y]=x;path[y]=i;
                if(!flag[y]) flag[y]=1,q.push(y);
            } 
        }
    }if (pre[T]==-1) return 0;else return 1;
}
int main(){
    freopen("3440.in","r",stdin);
    n=read();T=2*n+1;
    for (int i=1;i<=n;++i){
        insert1(0,i,1,0);int m=read(),l=read(),r=read(),c=read();
        for (int j=l;j<=r;++j) insert1(i,n+j,1,c*(abs(m-j)));insert1(i+n,T,1,0);
    }int ans=0,ans1=0;
    while(spfa()){
        int minn=inf,now=T;
        while(now) minn=min(minn,data[path[now]].z),now=pre[now];ans+=minn;now=T;
        while(now){ans1+=data[path[now]].c*minn;data[path[now]].z-=minn;data[path[now]^1].z+=minn;now=pre[now];} 
    }if (ans!=n)printf("NIE");else printf("%d",ans1);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值