UVa 11167 Monkeys in the Emei Mountain (最大流)
题意:雪雪是一只猴子。它在每天的 2:00 —— 9:00之间非常渴,所以在这个期间它必须喝掉2个单位的水。它可以多次喝水,只要它喝水的总量是2.它从不多喝,在一小时内他只能喝一个单位的水。所以它喝水的时间段可能是2:00 ——4:00,或者3:00——5:00,或者7:00——9:00.甚至喝两次,第一次2:00——3:00,第二次8:00——9:00.但是它不能在1:00——3:00喝水,因为在1:00时它不渴,也不能在8:00——10:00喝水,因为9:00必须结束。一共有n(n <= 100)只这样的猴子。我们用一个(v,a,b)(0 <= v,a,b <= 50000,a < b,v <= b - a)来描述一个在时间a~b之间口渴,并且必须在这个期间喝够v个单位水的猴子。所有猴子喝水的速度都是1小时喝1个单位的水。现在的问题是只有一个地方可以让m只猴子同时喝水,需要作出一个能满足所有猴子的喝水需要的安排,问能否给出安排。多解时输出任意一组解即可。
析:很明显的把每只猴子都看作一个结点,然后把每个时间也是看成一个结点,但是,,,太多了吧,肯定会爆炸的,所以,先把所有的区间进行离散化,然后对每个区间建立结点,在输出解的时候,要注意,把所有的区间进行合并,只要对每个区间都加一个标志,循环标记,每次都加一个段,因为最大流最大的情况就是把整个区间都充满,不会超过。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-3;
const int maxn = 500 + 10;
const int maxm = 1e7 + 10;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} struct Edge{
int from, to, cap, flow;
};
struct Dinic{
int n, m, s, t;
vector<Edge> edges;
vector<int> G[maxn];
int d[maxn];
bool vis[maxn];
int cur[maxn]; void init(int n){
this-> n = n;
for(int i = 0; i < n; ++i) G[i].cl;
edges.cl;
} void addEdge(int from, int to, int cap){
edges.pb((Edge){from, to, cap, 0});
edges.pb((Edge){to, from, 0, 0});
m = edges.sz;
G[from].pb(m - 2);
G[to].pb(m - 1);
} bool bfs(){
ms(vis, 0); vis[s] = 1; d[s] = 0;
queue<int> q; q.push(s); while(!q.empty()){
int u = q.front(); q.pop();
for(int i = 0; i < G[u].sz; ++i){
Edge &e = edges[G[u][i]];
if(!vis[e.to] && e.cap > e.flow){
vis[e.to] = 1;
d[e.to] = d[u] + 1;
q.push(e.to);
}
}
}
return vis[t];
} int dfs(int u, int a){
if(u == t || a == 0) return a;
int flow = 0, f;
for(int &i = cur[u]; i < G[u].sz; ++i){
Edge &e = edges[G[u][i]];
if(d[e.to] == d[u] + 1 && (f = dfs(e.to, min(a, e.cap-e.flow))) > 0){
e.flow += f;
edges[G[u][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()){ ms(cur, 0); flow += dfs(s, INF); }
return flow;
}
}; Dinic dinic;
vector<int> val;
int v[maxn], a[maxn], b[maxn];
int cnt[maxn]; int getpos(int x){ return lower_bound(val.begin(), val.end(), x) - val.begin(); } int main(){
int kase = 0;
while(scanf("%d", &n) == 1 && n){
scanf("%d", &m);
val.cl; val.pb(-1);
int sum = 0;
for(int i = 1; i <= n; ++i){
scanf("%d %d %d", v+i, a+i, b+i);
val.pb(a[i]); val.pb(b[i]);
sum += v[i];
}
sort(val.begin(), val.end());
val.resize(unique(val.begin(), val.end()) - val.begin());
int len = val.sz;
int s = 0, t = len + n + 3;
dinic.init(t + 5);
for(int i = 1; i <= n; ++i){
dinic.addEdge(s, len+i, v[i]);
int l = getpos(a[i]);
int r = getpos(b[i]);
for(int j = l; j < r; ++j) dinic.addEdge(len+i, j, val[j+1]-val[j]);
}
for(int j = 1; j + 1 < len; ++j) dinic.addEdge(j, t, (val[j+1]-val[j])*m);
if(sum != dinic.maxflow(s, t)) printf("Case %d: No\n", ++kase);
else{
printf("Case %d: Yes\n", ++kase);
for(int i = 0; i < len; ++i) cnt[i] = val[i];
for(int i = 1; i <= n; ++i){
vector<P> ans;
for(int j = 0; j < dinic.G[i+len].sz && v[i]; ++j){
Edge &e = dinic.edges[dinic.G[i+len][j]];
if(e.from != i + len) continue;
int x = dinic.edges[dinic.G[i+len][j]^1].flow;
if(x >= 0) continue;
x = -x; v[i] -= x;
if(val[e.to] + x < val[e.to+1]){
if(x + cnt[e.to] <= val[e.to+1]){
ans.push_back(P(cnt[e.to], cnt[e.to] + x));
cnt[e.to] += x;
if(cnt[e.to] == val[e.to+1]) cnt[e.to] = val[e.to];
}
else{
x -= val[e.to+1] - cnt[e.to];
ans.push_back(P(cnt[e.to], val[e.to+1]));
cnt[e.to] = val[e.to] + x;
ans.push_back(P(val[e.to], cnt[e.to]));
}
}
else ans.push_back(P(val[e.to], val[e.to+1]));
}
sort(ans.begin(), ans.end());
for(int j = 0; j + 1 < ans.sz; ){
if(ans[j].se == ans[j+1].fi) ans[j].se = ans[j+1].se, ans.erase(ans.begin()+j+1);
else ++j;
}
printf("%d", ans.sz);
for(int j = 0; j < ans.sz; ++j) printf(" (%d,%d)", ans[j].fi, ans[j].se);
printf("\n");
}
}
}
return 0;
}
UVa 11167 Monkeys in the Emei Mountain (最大流)的更多相关文章
- UVa11167 Monkeys in the Emei Mountain(最大流)
题目大概说有n只猴子,猴子们在某个时间段需要喝vi时间的水,各个单位时间段最多允许m只猴子同时喝水,问猴子们能否成功喝水并输出一个可行的方案,输出方案的时间段区间要从小到大排序并且合并连续的区间. 首 ...
- UVA-11167 Monkeys in the Emei Mountain(区间模型最大流+输出方案)
题目大意:有n只猴子,每只猴子都有一组参数(v,a,b),表示这只猴子在时间段[a,b]之间必须要喝v个单位水,并且每个时间单位只能和一个单位水,每次至少喝一个单位.但是只有一个水池,并且这个水池最多 ...
- UVa 753 - A Plug for UNIX(最大流)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- uva 10806 Dijkstra, Dijkstra. (最小费最大流)
uva 10806 Dijkstra, Dijkstra. 题目大意:你和你的伙伴想要越狱.你的伙伴先去探路,等你的伙伴到火车站后,他会打电话给你(电话是藏在蛋糕里带进来的),然后你就能够跑去火车站了 ...
- UVA 1658 海军上将(拆点法+最小费用限制流)
海军上将 紫书P375 这题我觉得有2个难点: 一是拆点,要有足够的想法才能把这题用网络流建模,并且知道如何拆点. 二是最小费用限制流,最小费用最大流我们都会,但如果限制流必须为一个值呢?比如这题限制 ...
- UVA 11082 Matrix Decompressing 矩阵解压(最大流,经典)
题意: 知道矩阵的前i行之和,和前j列之和(任意i和j都可以).求这个矩阵.每个格子中的元素必须在1~20之间.矩阵大小上限20*20. 思路: 这么也想不到用网络流解决,这个模型很不错.假设这个矩阵 ...
- UVa 1660 电视网络(点连通度+最小割最大流+Dinic)
https://vjudge.net/problem/UVA-1660 题意:给出一个无向图,求出点连通度.即最少删除多少个点,使得图不连通. 思路: 如果求线连通度的话,直接求个最大流就可以了.但这 ...
- UVA 753 A Plug for UNIX (最大流)
关键在建图,转换器连一条容量无限的边表示可以转化无数次,设备的插头连源点,插座连汇点. dinic手敲已熟练,输出格式又被坑,总结一下,输出空行多case的,一个换行是必要的,最后一个不加空行,有Te ...
- UVA - 820 Internet Bandwidth (因特网带宽)(最大流)
题意:给出所有计算机之间的路径和路径容量后,求出两个给定结点之间的流通总容量.(假设路径是双向的,且两方向流动的容量相同) 分析:裸最大流.标号从1开始,初始化的时候注意. #pragma comme ...
随机推荐
- spring事务没回滚
最近遇见一个问题,用spring管理实务,在service层处理数据,保存数据时出现异常,但没有回滚,检查了一下,发现是因为我用try catch将异常进行捕获了,没有抛出导致的:默认spring事务 ...
- autolayout不work
对于代码创建的UIView,将下面的选项关掉 [label3 setTranslatesAutoresizingMaskIntoConstraints:NO];
- 使用flash导出图集动画到unity
1.选中要导出的元件,元件所有动作要对齐,右键导出Sprite Sheet.. 2.设置如下 3.复制导出的png图片到unity,对图片进行网格裁剪,网格宽高在plist文件中:
- Session的常用场景
session :存储浏览器sessionID值保存在客户端,sessionID的key:data 数据存储在服务器上 会话管理,用户登录验证,权限访问控制,购物车,临时数据.
- proc
1. /proc 下文件的内容是动态创建的,当文件可写时可用作控制和配置目的. 2. 在某个进程读取 /proc 文件时,内核会分配一个内存页,驱动程序通过这个内存页将数据返回到用户空间 (read( ...
- 大型运输行业实战_day02_2_数据模型建立
1.模型分析 1.基本必备字段 id state type createTime updateTime 2.车票 : 车次 开始车站 到达车站 出发时间 票价 ...
- mybatis什么时候需要声明jdbcType?
经常会见到以下两种写法:1. #{bookId}2. #{bookId,jdbcType=INTEGER}一般情况下,两种写法都可以.它们都可以获取Dao层传递过来的参数.但是,当传入的参数为null ...
- 72. Edit Distance (String; DP)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- JAVA序列化和反序列化 对象<=>IO流 对象<=>字节数组
http://developer.51cto.com/art/201202/317181.htm http://blog.csdn.net/earbao/article/details/4691440 ...
- day10:vcp考试
Q181. An administrator is deploying ESXi 6.x hosts using Auto Deploy and wants the image profile to ...