题目描述

输入

输出

如果有可行解, 输出最小代价,否则输出NIE.

样例输入

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

样例输出

9


题解

费用流

设xi表示学生,yi表示编号,那么S->xi,容量为1,费用为0;xi->能够使得xi接受的yj,容量为1,费用为按照题目中计算出来的相应费用;yi->T,容量为1,费用为0。

然后跑最小费用最大流即可。

#include <cstdio>
#include <cstring>
#include <queue>
#define N 500
#define M 100000
using namespace std;
queue<int> q;
int head[N] , to[M] , val[M] , cost[M] , next[M] , cnt = 1 , s , t , dis[N] , from[N] , pre[N];
int abs(int x)
{
return x > 0 ? x : -x;
}
void add(int x , int y , int v , int c)
{
to[++cnt] = y , val[cnt] = v , cost[cnt] = c , next[cnt] = head[x] , head[x] = cnt;
to[++cnt] = x , val[cnt] = 0 , cost[cnt] = -c , next[cnt] = head[y] , head[y] = cnt;
}
bool spfa()
{
int x , i;
memset(from , -1 , sizeof(from));
memset(dis , 0x3f , sizeof(dis));
dis[s] = 0 , q.push(s);
while(!q.empty())
{
x = q.front() , q.pop();
for(i = head[x] ; i ; i = next[i])
if(val[i] && dis[to[i]] > dis[x] + cost[i])
dis[to[i]] = dis[x] + cost[i] , from[to[i]] = x , pre[to[i]] = i , q.push(to[i]);
}
return ~from[t];
}
int main()
{
int n , i , j , m , a , b , k , f = 0 , ans = 0;
scanf("%d" , &n) , s = 0 , t = 2 * n + 1;
for(i = 1 ; i <= n ; i ++ )
{
scanf("%d%d%d%d" , &m , &a , &b , &k) , add(s , i , 1 , 0) , add(i + n , t , 1 , 0);
for(j = a ; j <= b ; j ++ ) add(i , j + n , 1 , k * abs(j - m));
}
while(spfa())
{
k = 0x3f3f3f3f;
for(i = t ; i != s ; i = from[i]) k = min(k , val[pre[i]]);
f += k , ans += k * dis[t];
for(i = t ; i != s ; i = from[i]) val[pre[i]] -= k , val[pre[i] ^ 1] += k;
}
if(f < n) printf("NIE\n");
else printf("%d\n" , ans);
return 0;
}

最新文章

  1. Excel通过身份证获取出生年月,性别,年龄,生肖,星座,省份等信息总结归纳
  2. Python实例4
  3. JS - Constructor还可以这样用
  4. 【网络收集】获取JavaScript 的时间使用内置的Date函数完成
  5. node.js笔记——gulp
  6. .net mvc 发布部署到机器上
  7. JS 操作Dom节点之样式
  8. hadoop执行hbase插入表操作,出错:Stack trace: ExitCodeException exitCode=1:(xjl456852原创)
  9. 复习知识点:XML解析数据,JOSN解析数据,GET请求数据,POST请求数据
  10. Strange fuction
  11. Chapter 1 Securing Your Server and Network(12):保护链接服务器
  12. PAT 1124 Raffle for Weibo Followers
  13. 从session中获取当前用户的工具类
  14. oracle10g和oracle11g导入导出数据区别
  15. git入门(廖雪峰老师)
  16. Delphi学习技巧
  17. web前端性能调优(一)
  18. IDEA 修改 jdk 版本
  19. 阿里云Redis公网连接的解决办法
  20. Sensor fusion(传感器融合)

热门文章

  1. vue组件 $children,$refs,$parent的使用
  2. 注册Windows service及其相关
  3. servlet从服务器磁盘文件读出到浏览器显示,中文乱码问题,不要忘记在输入流和输出流都要设置编码格式,否则一个地方没设置不统一就会各种乱码
  4. linux系统监控工具glances
  5. 使用vscode开发vue cli 3项目,配置eslint以及prettier
  6. windows_Bat_Scripts查看系统IP-更改regedit-更新系统补丁
  7. tp3.2框架中使用volist输出混乱的一点发现
  8. 【PHP】判断变量是否为控
  9. 第11课 文章分类(组件化开发) Thinkphp5商城第四季
  10. 1 &gt; 2 and 3 &lt; 4 or 4 &gt; 5 and 2 &gt; 1 or 9 &lt; 8