UVALive - 6571 It Can Be Arranged 最大流
题目链接:
http://acm.hust.edu.cn/vjudge/problem/48415
It Can Be Arranged
Time Limit: 3000MS
#### 问题描述
> Every year, several universities arrange inter-university national programming contests. ACM ICPC
> Dhaka site regional competition is held every year in Dhaka and one or two teams are chosen for ACM
> ICPC World Finals.
> By observing these, MMR (Mission Maker Rahman) has made a plan to open a programming
> school. In that school, N courses are taught. Each course is taught every day (otherwise, programmers
> may forget DP while learning computational geometry!). You will be given the starting time Ai and
> finishing time Bi (inclusive) of each course i (1 ≤ i ≤ N). You will be also given the number of students
> registered for each course, Si (1 ≤ i ≤ N). You can safely assume no student has registered to two
> different courses. MMR wants to hire some rooms of a building, named Sentinel Tower, for running that
> school. Each room of Sentinel Tower has a capacity to hold as much as M students. The programmers
> (students) are very restless and a little bit filthy! As a result, when coursei
> is taken in a class room,
> after the class is finished, it takes cleanij time to clean the room to make it tidy for starting teaching
> coursej immediately just after coursei in the same room.
> Your job is to help MMR to decide the minimum number of rooms need to be hired to run the
> programming school.
#### 输入
> Input starts with an integer T (T ≤ 100) denoting the number of test cases. Each case starts with two
> integers N (1 ≤ N ≤ 100), number of courses and M (1 ≤ M ≤ 10000), capacity of a room. Next N
> lines will contain three integers Ai
> , Bi (0 ≤ Ai ≤ Bi ≤ 10000000) and Si (1 ≤ Si ≤ 10000), starting
> and finishing time of a course. Next N lines will contain the clean time matrix, where the i-th row will
> contain N integers cleanij (1 ≤ i ≤ N, 1 ≤ j ≤ N, 0 ≤ cleanij ≤ 10000000, cleanii = 0).
#### 输出
> For each case, print the test case number, starting from 1, and the answer, minimum number of rooms
> needed to be hired.
样例
sample input
3
1 5
1 60 12
0
4 1
1 100 10
50 130 3
150 200 15
80 170 7
0 2 3 4
5 0 7 8
9 10 0 12
13 14 15 0
2 1
1 10 1
12 20 1
0 2
5 0sample output
Case 1: 3
Case 2: 22
Case 3: 2
题意
现在需要上n个课程,每个课程:(s,t,p),描述开始时间,结束时间,和上课人数。
现在有若干个最多能容纳m个人的教室,问如何用最少的教室上完所有的课。
题解
如果教室的容量没有要求,那么这将是一个经典的DAG的最少路径覆盖问题。
加了教室容量限制之后,变成了一个带权的最少路径覆盖问题。
我们可以这样建图:首先拆点,把课程i拆成i,i+n,0到i连边,i+n到2*n+1连边,边权为i课程需要的教室数,然后如果课程i上完能够接着上j,那么就连一条边权为INF的边从i到j+n。 然后跑最大流,这样跑出了的值相当于能够共用的教室数,吧总数-最大流就是答案了。
代码
#include<map>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define M (l+(r-l)/2)
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
typedef long long LL;
const int maxn=444;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const int mod=1e9+7;
struct Edge {
int from,to,cap,flow;
Edge(int f,int t,int c,int fl):from(f),to(t),cap(c),flow(fl) {}
};
struct Dinic {
int n,m,s,t;
vector<Edge> egs;
vector<int> G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
void init(int n) {
this->n=n;
for(int i=0; i<=n; i++) G[i].clear();
egs.clear();
}
void addEdge(int from,int to,int cap) {
egs.push_back(Edge(from,to,cap,0));
egs.push_back(Edge(to,from,0,0));
m=egs.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool BFS() {
memset(vis,0,sizeof(vis));
queue<int> Q;
Q.push(s);
d[s]=0;
vis[s]=1;
while(!Q.empty()) {
int x=Q.front();
Q.pop();
for(int i=0; i<G[x].size(); i++) {
Edge& e=egs[G[x][i]];
if(!vis[e.to]&&e.cap>e.flow) {
vis[e.to]=1;
d[e.to]=d[x]+1;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x,int a) {
if(x==t||a==0) return a;
int flow=0,f;
for(int& i=cur[x]; i<G[x].size(); i++) {
Edge& e=egs[G[x][i]];
if(d[x]+1==d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>0) {
e.flow+=f;
egs[G[x][i]^1].flow-=f;
flow+=f;
a-=f;
if(a==0) break;
}
}
return flow;
}
int Maxflow(int s,int t) {
this->s=s;
this->t=t;
int flow=0;
while(BFS()) {
memset(cur,0,sizeof(cur));
flow+=DFS(s,INF);
}
return flow;
}
} dinic;
int n,m;
LL si[maxn],ti[maxn],C[maxn][maxn];
int wi[maxn];
void init() {
dinic.init(2*n+2);
}
int main() {
int tc,kase=0;
scanf("%d",&tc);
while(tc--) {
scanf("%d%d",&n,&m);
init();
for(int i=1; i<=n; i++) {
scanf("%lld%lld%d",&si[i],&ti[i],&wi[i]);
}
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) {
scanf("%lld",&C[i][j]);
}
}
int sum=0;
for(int i=1; i<=n; i++) {
int cap=wi[i]%m?wi[i]/m+1:wi[i]/m;
sum+=cap;
dinic.addEdge(0,i,cap);
dinic.addEdge(i+n,2*n+1,cap);
}
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) {
if(ti[i]+C[i][j]<si[j]) {
dinic.addEdge(i,j+n,INF);
}
}
}
int ans=dinic.Maxflow(0,2*n+1);
printf("Case %d: %d\n",++kase,sum-ans);
}
return 0;
}
UVALive - 6571 It Can Be Arranged 最大流的更多相关文章
- 【Uvalive 2531】 The K-League (最大流-类似公平分配问题)
[题意] 有n个队伍进行比赛,每场比赛,恰好有一支队伍取胜.一支队伍败.每个队伍需要打的比赛场数相同,给你每个队伍目前已经赢得场数和输得场数,再给你一个矩阵,第 i 行第 j 列 表示队伍 i 和队伍 ...
- POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Network / FZU 1161 (网络流,最大流)
POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Networ ...
- uvalive 3231 Fair Share 公平分配问题 二分+最大流 右边最多流量的结点流量尽量少。
/** 题目: uvalive 3231 Fair Share 公平分配问题 链接:https://vjudge.net/problem/UVALive-3231 题意:有m个任务,n个处理器,每个任 ...
- UVALive - 6266 Admiral 费用流
UVALive - 6266 Admiral 题意:找两条完全不相交不重复的路使得权值和最小. 思路:比赛的时候时间都卡在D题了,没有仔细的想这题,其实还是很简单的,将每个点拆开,连一条容量为1,费用 ...
- UVALive 6887 Book Club 最大流解最大匹配
题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...
- Uvalive 4865 Data Recovery 最大流
题意就是 给一个50 * 50的矩阵 然后给出每行每列元素的和 和一个初始矩阵 矩阵中有些是未知,有些是已知 然后我们求目标矩阵就是把能确定的元素的值求出来,实在不能确定的就置为-1 所有矩阵元素的值 ...
- UVALive 3645 Objective: Berlin(最大流 :时序模型)
题意:已知n(n <= 150)个城市和m(m <= 5000)个航班,每个航班有出发地.到达地.乘坐人数.起飞时间和降落时间(时间用时和分表示),求从一个指定城市出发,去往另一个指定城市 ...
- 【 UVALive - 2197】Paint the Roads(上下界费用流)
Description In a country there are n cities connected by m one way roads. You can paint any of these ...
- 【UVALive - 5131】Chips Challenge(上下界循环费用流)
Description A prominent microprocessor company has enlisted your help to lay out some interchangeabl ...
随机推荐
- jQuery插件Skippr实现焦点图
史上效果最好的焦点图幻灯片jQuery插件Skippr,轻量级插件.响应式布局插件,强大的参数自定义 配置,可自定义切换速度.切换方式.是否显示左右箭头.是否自动播放.自动播放间隔时间等配置 参数,调 ...
- jquery 基础汇总---导图篇
最近在慕课网学习了一些jquery的基础知识,为了方便记忆,整理出来的导图 jQuery基础总共分为4个部分,分别是样式篇.事件篇.动画篇.DOM篇. 样式篇,主要介绍jQuery的基础语法,选择器以 ...
- .NET程序员爱上网站[整理]
1.博客园(代码改变世界) http://www.cnblogs.com 2.开源中国社区(开源软件发现.使用和交流平台) http://www.oschina.net 3.CSDN(中国最大的IT社 ...
- sed命令实战
删除所有的空行,并在每行后面增加一个空行 sed '/^$/d;G' /etc/fstab 将每一行前导的“空白字符”(空格,制表符)删除 sed 's/^[\t ]*//' file 将文本中的 a ...
- c语言学习第四天数据类型1
int 代表整数,它在内存中占4个字节,二进制的表示方式是占用了三十二位,二进制中只包含0和1,那它的最大值就是全为1,但int是 有符号类型,所以最高位(左边的第一位)要拿出来做符号位,这样就只 ...
- php 安装xdebug扩展
php 扩展获取地址 http://pecl.php.net/package/ 编译安装的过程 wget http://pecl.php.net/get/xdebug-2.2.2.tgz tar -z ...
- PHPExcel读取excel的多个sheet存入数据库
//批量导入文章 excel读取 public function importdata ( $filename, $tmp_name ) { //设置超时时间 set_time_limit(0); $ ...
- [C#] Extension Method 扩展方法
当我们引用第三方的DLL.或者Visual Studio自己的库的时候,或许会发现这样的一个情况,如果这个类型有一个XX的方法就好了.这时候我们可以用到扩展方法,是我们的代码更加灵活和高效. 这里我举 ...
- grunt初体验
最近参与多人团队项目开发过程之中,使用到了grunt来构建项目,包括一些文件的压缩,合并等操作.亲自动手进行grunt任务的配置,学到了很多东西.现将自己的学习过程记录如下: 1.对于一个项目而言,使 ...
- 2016/09/21 Java关键字final
1.final类 final类不能被继承,没有子类,final类中的方法默认是final的. final类不能被继承,因此final类的成员方法没有机会被覆盖,默认都是final的. 2.final方 ...