P3440 [POI2006]SZK-Schools
应该是很显然的费用流模型吧...
$S$ 向所有学校连边,流量为 $1$,费用为 $0$(表示每个学校要选一个编号)
学校向范围内的数字连边,流量为 $1$,费用为 $c|m-m'|$(表示学校选择编号的花费)
注意学校向原来的数字连边,流量 $1$,费用 $0$(表示学校可以不改变编号)
所有数字向 $T$ 连边,流量为 $1$(每个数字只能给一个学校)
那么一个流就代表一个学校的选择
然后最小费用最大流就是答案
记得判一下无解就好了
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=2e5+,M=2e6+,INF=1e9+;
int fir[N],from[M<<],to[M<<],val[M<<],cst[M<<],cntt=;
inline void add(int a,int b,int c,int d)
{
from[++cntt]=fir[a]; fir[a]=cntt;
to[cntt]=b; val[cntt]=c; cst[cntt]=d;
from[++cntt]=fir[b]; fir[b]=cntt;
to[cntt]=a; val[cntt]=; cst[cntt]=-d;
}
int dis[N],mif[N],pre[N],S,T;
queue <int> q;
bool inq[N];
bool BFS()
{
for(int i=S;i<=T;i++) dis[i]=INF;
q.push(S); inq[S]=; dis[S]=; mif[S]=INF;
while(!q.empty())
{
int x=q.front(); q.pop(); inq[x]=;
for(int i=fir[x];i;i=from[i])
{
int &v=to[i]; if( !val[i] || dis[v]<=dis[x]+cst[i] ) continue;
dis[v]=dis[x]+cst[i]; pre[v]=i;
mif[v]=min(mif[x],val[i]);
if(!inq[v]) q.push(v),inq[v]=;
}
}
return dis[T]<INF;
}
int ans;
inline void upd()
{
for(int now=T,i=pre[T]; now!=S; now=to[i^],i=pre[now])
val[i]-=mif[T],val[i^]+=mif[T];
ans+=mif[T]*dis[T];
} int n;
int main()
{
int m,a,b,k;
n=read(); S=,T=n+n+;
for(int i=;i<=n;i++)
{
add(S,i,,);
m=read(),a=read(),b=read(),k=read();
add(i,n+m,,);
for(int j=a;j<=b;j++)
add(i,n+j,,abs(m-j)*k);
}
for(int i=;i<=n;i++) add(n+i,T,,);
while(BFS()) upd();
for(int i=fir[S];i;i=from[i])
if(val[i]) { printf("NIE"); return ; }
printf("%d",ans);
return ;
}
P3440 [POI2006]SZK-Schools的更多相关文章
- P3440 [POI2006]SZK-Schools(费用流)
P3440 [POI2006]SZK-Schools 每所学校$i$开一个点,$link(S,i,1,0)$ 每个编号$j$开一个点,$link(i,T,1,0)$ 蓝后学校向编号连边,$link(i ...
- POJ1236Network of Schools[强连通分量|缩点]
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16571 Accepted: 65 ...
- POJ 1236 Network of Schools(Tarjan缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16806 Accepted: 66 ...
- Network of Schools --POJ1236 Tarjan
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Description A number of schools are conne ...
- BZOJ 1121: [POI2008]激光发射器SZK
1121: [POI2008]激光发射器SZK Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 792 Solved: 653[Submit][Sta ...
- 【BZOJ-1121】激光发射器SZK 物理 + 数学 + 乱搞
1121: [POI2008]激光发射器SZK Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 682 Solved: 565[Submit][Sta ...
- POJ 1236 Network of Schools(强连通分量/Tarjan缩点)
传送门 Description A number of schools are connected to a computer network. Agreements have been develo ...
- POJ1236Network of Schools(强连通分量 + 缩点)
题目链接Network of Schools 参考斌神博客 强连通分量缩点求入度为0的个数和出度为0的分量个数 题目大意:N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后 ...
- [强连通分量] POJ 1236 Network of Schools
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16803 Accepted: 66 ...
随机推荐
- c# 调用外包程序 等待处理完成结果
string root = @"J:\yaoqianshu"; string pythonPath = "解压缩拷贝启动动画测试(新).py"; string ...
- [Android] 对ImageView设置属性scaleType为FIT_START,如何去掉多余空白
当对ImageView设置了属性scaleType为FIT_START时,可以通过调用ImageView的setAdjustViewBounds(true). 即: imageView.setScal ...
- chrome会话cookie显示过期时间为1969-12-31T23:59:59.000Z
cookie不设置过期时间的话,为浏览器会话cookie,关闭浏览器自动删除cookie 但是在chrome浏览器下,cookie过期时间显示为“1969-12-31T23:59:59.000Z” 在 ...
- Ural 1519 Formula 1 (DP)
题意:给定一个 n * m 的矩阵,问你能花出多少条回路. #pragma comment(linker, "/STACK:1024000000,1024000000") #inc ...
- jQuery框架-3.jQuery自定义封装插件和第三方插件
一.jQuery的封装扩展 1.jQuery中extend方法使用 (挂在到jQuery和jQuery.fn两对象身上的使用) 1.1.官方文档定义: jQuery.extend Merge th ...
- MySQL性能调优与架构设计——第10章 MySQL数据库Schema设计的性能优化
第10章 MySQL Server性能优化 前言: 本章主要通过针对MySQL Server(mysqld)相关实现机制的分析,得到一些相应的优化建议.主要涉及MySQL的安装以及相关参数设置的优化, ...
- ZOJ3770Ranking System 2017-04-14 12:42 52人阅读 评论(0) 收藏
Ranking System Time Limit: 2 Seconds Memory Limit: 65536 KB Few weeks ago, a famous software co ...
- Codeforces 766D Mahmoud and a Dictionary 2017-02-21 14:03 107人阅读 评论(0) 收藏
D. Mahmoud and a Dictionary time limit per test 4 seconds memory limit per test 256 megabytes input ...
- HDU1412:{A} + {B}
Problem Description 给你两个集合,要求{A} + {B}. 注:同一个集合中不会有两个相同的元素. Input 每组输入数据分为三行,第一行有两个数字n,m(0<n,m& ...
- Web 应用简单测试方案
测试:一定要分阶段测试,先确定入队列成功,再测试队列的执行是否成功. 功能点: 1. 翻页2. 加精3. 置顶4. 帖子浏览量(PV)5. 发帖6. 回复7. 评论 8. crontab 脚本 @20 ...