Description

给 \(N\) 个格子区间涂色,有两类限制条件

  • 区间 \([L,R]\) 内至少 \(K\) 个
  • 区间 \([L,R]\) 外至少 \(K\) 个

    求最少要涂多少个格子

Solution

显然有单调性,所以二分

当限定了总个数后,差分约束来判定可行性

直接写bellman-ford就可以

训练时候忘记建 \(d[i+1]-d[i] \ge 0\) 这条边

#include <bits/stdc++.h>
using namespace std; const int INF = 0x3f3f3f3f;
const int MAXN = 3005; int dist[MAXN];
struct Edge {
int u,v,cost;
Edge(int _u=0,int _v=0,int _cost=0):u(_u),v(_v),cost(_cost){};
}; vector <Edge> E; bool bellman_ford(int start,int n) {
for(int i=1;i<=n;i++) {
dist[i] = INF;
}
dist[start]=0;
for(int i=1;i<n;i++) {
bool flag=false;
for(int j=0;j<E.size();j++) {
int u=E[j].u;
int v=E[j].v;
int cost=E[j].cost;
if(dist[v]>dist[u]+cost) {
dist[v]=dist[u]+cost;
flag=true;
}
}
if(!flag) return true;
}
for(int j=0;j<E.size();j++) {
if(dist[E[j].v] > dist[E[j].u] + E[j].cost) return false;
}
return true;
} int T,n,m1,m2; struct Item {
int l,r,k;
} r1[3005],r2[3005]; int main() {
ios::sync_with_stdio(false);
cin>>T;
while(T--) {
cin>>n>>m1>>m2;
for(int i=1;i<=m1;i++) cin>>r1[i].l>>r1[i].r>>r1[i].k;
for(int i=1;i<=m2;i++) cin>>r2[i].l>>r2[i].r>>r2[i].k;
int L=0,R=n;
while(R>L) {
int mid=(L+R)/2;
//cout<<"checking "<<mid<<endl;
E.clear();
for(int i=1;i<=n;i++)
E.push_back(Edge(i,i+1,1)),
E.push_back(Edge(i+1,i,0));
for(int i=1;i<=m1;i++) {
E.push_back(Edge(r1[i].r+1,r1[i].l,-r1[i].k));
}
for(int i=1;i<=m2;i++) {
E.push_back(Edge(r2[i].l,r2[i].r+1,mid-r2[i].k));
}
E.push_back(Edge(1,n+1,mid));
E.push_back(Edge(n+1,1,-mid));
//for(int i=0;i<E.size();i++) cout<<E[i].u<<" "<<E[i].v<<" "<<E[i].cost<<endl;
if(bellman_ford(1,n+1)) R=mid;
else L=mid+1;
}
cout<<L<<endl;
}
}

[CCPC2019 哈尔滨] A. Artful Paintings - 差分约束,最短路的更多相关文章

  1. POJ 3159 Candies(差分约束+最短路)题解

    题意:给a b c要求,b拿的比a拿的多但是不超过c,问你所有人最多差多少 思路:在最短路专题应该能看出来是差分约束,条件是b - a <= c,也就是满足b <= a + c,和spfa ...

  2. POJ 3169 Layout(差分约束+最短路)题解

    题意:有一串数字1~n,按顺序排序,给两种要求,一是给定u,v保证pos[v] - pos[u] <= w:二是给定u,v保证pos[v] - pos[u] >= w.求pos[n] - ...

  3. Layout---poj3169(差分约束+最短路spfa)

    题目链接:http://poj.org/problem?id=3169 有n头牛站成一排 在他们之间有一些牛的关系比较好,所以彼此之间的距离不超过一定距离:也有一些关系不好的牛,希望彼此之间的距离大于 ...

  4. poj3159 差分约束 spfa

    //Accepted 2692 KB 1282 ms //差分约束 -->最短路 //TLE到死,加了输入挂,手写queue #include <cstdio> #include & ...

  5. Candies-POJ3159差分约束

    Time Limit: 1500MS Memory Limit: 131072K Description During the kindergarten days, flymouse was the ...

  6. ZOJ 2770火烧连营——差分约束

    偶尔做了一下差分约束. 题目大意:给出n个军营,每个军营最多有ci个士兵,且[ai,bi]之间至少有ki个士兵,问最少有多少士兵. ---------------------------------- ...

  7. POJ 2983 Is the Information Reliable? 差分约束

    裸差分约束. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #i ...

  8. 2014 Super Training #6 B Launching the Spacecraft --差分约束

    原题:ZOJ 3668 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3668 典型差分约束题. 将sum[0] ~ sum ...

  9. POJ 1364 King --差分约束第一题

    题意:求给定的一组不等式是否有解,不等式要么是:SUM(Xi) (a<=i<=b) > k (1) 要么是 SUM(Xi) (a<=i<=b) < k (2) 分析 ...

随机推荐

  1. Wix 快速开发安装包程序 (二)安装行为

    上一小节,主要介绍了构建最小级别的安装包,这个安装包所做的事情很简单,主要是打包好一些文件,然后放到用户机器的某个位置下面. 这个小节,主要是总结安装过程的各种行为如何使用Wix编写. 一.写注册表 ...

  2. 转:Laravel 项目开发规范

    文件介绍很好 值得细细看看 https://www.jianshu.com/p/e464a35e5ed2 https://learnku.com/docs/laravel-specification/ ...

  3. Spring学习笔记-高级装配-03

    主要内容: ●Spring profile ●条件化的bean声明 ●自动装配与歧义性 ● Spring表达式语言 本章介绍一些高级的装配技术,可实现更为高级的装配功能. 环境与profile 软件开 ...

  4. tensorflow张量排序

    本篇记录一下TensorFlow中张量的排序方法 tf.sort和tf.argsort # 声明tensor a是由1到5打乱顺序组成的 a = tf.random.shuffle(tf.range( ...

  5. P1642 规划 [01分数规划]

    裸题,考虑size完了跑一个树上背包,这题没了. // by Isaunoya #include <bits/stdc++.h> using namespace std; #define ...

  6. Joomla 3.0.0 --> 3.4.6-RCE复现

    Joomla是一款网站管理系统CMS,最近曝出其3.00-3.4.6的joomla版本存在一个RCE漏洞. 并且大佬已将exp放在了github上,下载地址:https://www.exploit-d ...

  7. P1339 热浪【最短路】

    #include <bits/stdc++.h> #define dbg(x) cout << #x << "=" << x < ...

  8. P1282 多米诺骨牌【dp】

    P1282 多米诺骨牌 提交 20.02k 通过 6.30k 时间限制 1.00s 内存限制 125.00MB 题目提供者洛谷 难度提高+/省选- 历史分数100 提交记录 查看题解 标签   查看算 ...

  9. String类型的日期怎么转化为Date类型

    在一个SQL中,如果同时使用rownum和order by,会有一个先后顺序的问题. 比如select id1,id2 from t_tablename where rownum<3 order ...

  10. Educational Codeforces Round 82 (Rated for Div. 2) A-E代码(暂无记录题解)

    A. Erasing Zeroes (模拟) #include<bits/stdc++.h> using namespace std; typedef long long ll; ; in ...