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 ...
随机推荐
- nvidia 驱动下载地址
http://www.nvidia.com/Download/index.aspx?lang=en-us
- 10个实用的UX设计作品推销小窍门
以下内容由Mockplus(摹客)团队翻译整理,仅供学习交流,Mockplus是更快更简单的原型设计工具. 众所周知,产品用户体验很重要,即使是Google也知道这一点.但是,当真正涉及到UX 设计销 ...
- pyspider示例代码二:解析JSON数据
本系列文章主要记录和讲解pyspider的示例代码,希望能抛砖引玉.pyspider示例代码官方网站是http://demo.pyspider.org/.上面的示例代码太多,无从下手.因此本人找出一下 ...
- 解决Error running 'index.jsp : Address localhost:1099 is already in use的方法
晚上在idea中 启动服务器一次后 正常运行,但是改了注解配置后 报错,报错为Error running 'index.jsp : Address localhost:1099 is alread ...
- mutex 实现 只允许一个进程
static class Program { [STAThread] static void Main() { bool createdNew=false; Mutex mutex = new Mut ...
- javadoc tags
Where Tags Can Be Used The following sections describe where the tags can be used. Note that these t ...
- (最短路 SPFA)Currency Exchange -- poj -- 1860
链接: http://poj.org/problem?id=1860 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 2326 ...
- An Introduction to Text Mining using Twitter Streaming
Text mining is the application of natural language processing techniques and analytical methods to t ...
- pro1
#include<iostream> using namespace std; int main(void) { int i,a[],sum; cin>>i; for(i=0; ...
- Javascript的事件模型和Promise实现
1. Javascript的运行时模型——事件循环 JS的运行时是个单线程的运行时,它不像其他编程语言,比如C++,Java,C#这些可以进行多线程操作的语言.当它执行一个函数时,它只会一条路走到黑, ...