传送门

令前缀和为s[i],则⼀一个要求等价于 s[r] - s[l - 1] >= x。

题中还有别的要求,包括 s[i] - s[i - 1] <= 1 和 s[i] - s[i- 1] >= 0。

建一个超级原点s,把所有结点连到s(令s = n + 1)

差分约束系统规定的只是元素的相对关系

按照题意,相对关系不变时最后的答案尽可能小

因此最终答案应该是:dis[ n ] - min_dis

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std; inline int read()
{
int sum = ,p = ;
char ch = getchar();
while(ch < '' || ch > '')
{
if(ch == '-')
p = -;
ch = getchar();
}
while(ch >= '' && ch <= '')
{
(sum *= ) += ch - '';
ch = getchar();
}
return sum * p;
} const int maxn = ,maxm = ;
int n,m,s,cnt;
int dis[maxn],head[maxn];
bool vis[maxn];
struct edge
{
int nxt,to,wei;
} e[maxn * ]; void add(int x,int y,int z)
{
e[++cnt].nxt = head[x];
e[cnt].to = y;
e[cnt].wei = z;
head[x] = cnt;
} void spfa(int x)
{
queue<int> q;
q.push(x);
for(int i = ; i <= n + ; i ++)
dis[i] = 1e9;
dis[s] = ;
vis[s] = true;
while(!q.empty())
{
int o = q.front();
q.pop();
vis[o] = false;
for(int i = head[o]; i; i = e[i].nxt)
{
int v = e[i].to;
if(dis[v] > dis[o] + e[i].wei)
{
dis[v] = dis[o] + e[i].wei;
if(!vis[v])
{
q.push(v);
vis[v] = true;
}
}
}
}
} int main()
{
n = read(),m = read();
s = n + ;
memset(head,-,sizeof(head));
for(int i = ; i <= n; i++)
add(s,i,);
int a,b,c;
for(int i = ; i <= m; i++)
{
a = read(),b = read(),c = read();
add(b,a - ,-c);
}
for(int i = ; i <= n; i++)
{
add(i - ,i,);
add(i,i - ,);
}
spfa(s);
int ans = 1e9;
for(int i=; i<=n; i++)
ans = min(ans, dis[i]);
printf("%d",dis[n] - ans);
return ;
}

种树-洛谷P1250(差分约束)的更多相关文章

  1. 洛谷P1250种树(贪心)

    题目描述 一条街的一边有几座房子.因为环保原因居民想要在路边种些树.路边的地区被分割成块,并被编号成1..N.每个部分为一个单位尺寸大小并最多可种一棵树.每个居民想在门前种些树并指定了三个号码B,E, ...

  2. 洛谷P1484 种树&洛谷P3620 [APIO/CTSC 2007]数据备份 题解(堆+贪心)

    洛谷P1484 种树&洛谷P3620 [APIO/CTSC 2007]数据备份 题解(堆+贪心) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/132 ...

  3. 题解——洛谷P1250 种树(差分约束)

    一道看一眼就知道差分约束的题目 但是最短路spfa的时候注意松弛条件是 if(dis[u]+w[i]<dis[v[i]]) dis[v[i]]=dis[u]+w[i]; 不能写成 if(dis[ ...

  4. 【最短路·差分约束】洛谷P1250

    题目描述 一条街的一边有几座房子.因为环保原因居民想要在路边种些树.路边的地区被分割成块,并被编号成1..N.每个部分为一个单位尺寸大小并最多可种一棵树.每个居民想在门前种些树并指定了三个号码B,E, ...

  5. HDU 1384 Intervals &洛谷[P1250]种树

    差分约束 差分约束的裸题,关键在于如何建图 我们可以把题目中给出的区间端点作为图上的点,此处应注意,由于区间中被标记的点的个数满足区间加法,这里与前缀和类似,对于区间[L..R]来说,我们加入一条从L ...

  6. 洛谷P3655 差分数组 树状数组

    题目链接:https://www.luogu.org/problemnew/show/P3655 不一定对,仅供参考,不喜勿喷,不喜勿喷. 先copy洛谷P3368 [模板]树状数组 2 题解里面一位 ...

  7. Java实现洛谷P1250 种树 (暴力)

    P1250 种树 输入输出样例 输入 9 4 1 4 2 4 6 2 8 9 2 3 5 2 输出 5 PS: 我种最少的树,意味着我的树要最多的被利用,意味着,我的树要尽可能的靠中间种, 也就是我把 ...

  8. 洛谷 P1250 种树 题解

    差分约束系统,维护前缀和,根据式子d[ b ] < = d[ e + 1 ] - t,可以看出要连e和b - 1,但占用了超级源点0,所以要把区间向后移,这样就可以用超级源点0来保持图的连通性( ...

  9. 洛谷P3275 [SCOI2011]糖果(差分约束,最长路,Tarjan,拓扑排序)

    洛谷题目传送门 差分约束模板题,等于双向连0边,小于等于单向连0边,小于单向连1边,我太蒻了,总喜欢正边权跑最长路...... 看遍了讨论版,我是真的不敢再入复杂度有点超级伪的SPFA的坑了 为了保证 ...

随机推荐

  1. jenkins 集成环境搭建

    http://www.cnblogs.com/jenniferhuang/p/3355252.html

  2. 微信公众号h5页面alert去掉域名

    h5页面内嵌到微信公众号提示信息alert的时候会显示域名,去掉域名显示重写alert方法: window.alert = function(name){ var iframe = document. ...

  3. Mike and Foam(位运算)

    English reading: bartender == barmaid:酒吧女招待 milliliter:毫升:千分之一毫升 foam:泡沫 a glass of beer with a good ...

  4. C++ vector的用法(转)

    原文链接:https://blog.csdn.net/qinyuehong/article/details/92837359

  5. git创建远程分支并推送

    1.查看所有分支(-a=>'查看全部的分支') git branch -a 2.创建本地test分支 git branch test 2.2切换test分支 git checkout test ...

  6. elasticSearch 起步(CentOS7 success)

    安装和启动 这里使用docker进行安装的 docker pull elasticsearch 然后使用 docker run -e ES_JAVA_OPTS="-Xms256m -Xmx2 ...

  7. 解决windows配置visual studio code调试golang环境问题

    写这篇随笔是为了Mark下在这个过程中配到的几个问题 1.具体过程可参考https://www.cnblogs.com/JerryNo1/p/5412864.html,Jerry博主写的非常详细了 1 ...

  8. php & c# DES

    php <?php class DES { var $key; var $iv; //偏移量 function DES($key = '11001100', $iv=0 ) { //key长度8 ...

  9. Redis Distributed lock

    using StackExchange.Redis; using System; using System.Collections.Generic; using System.Linq; using ...

  10. Equalize

    You are given two binary strings aa and bb of the same length. You can perform the following two ope ...