种树-洛谷P1250(差分约束)
2024-09-06 22:12:30
令前缀和为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 ;
}
最新文章
- struts---JSP界面验证码生成与验证
- Java重点之小白解析--浅谈HashMap与HashTable
- jQuery构造函数init参数分析(三)
- 内嵌DB
- C# 6新特性及示例代码
- [vsftp服务]——ftp虚拟用户、权限设置等的实验
- jquery操作cookie {分享}
- HTML5_用语义化标记重新定义博客
- 【转】#ifdef __cplusplus深度剖析
- [jQuery] $.grep使用
- plaidctf2015 uncorrupt png
- poj3280(区间dp)
- 百度官方wormHole后门检测记录(转)
- 【杂】指针,*,&;
- python笔记10-切片(从list或字符串中取几个元素)
- C++ 重载运算符和重载函数
- 如何查看目前正在使用的Windows10是哪个版本?
- OpenEXR-2.2.0在Win7 x64系统下的安装方法
- SQL删除重复数据只保留一条数据
- Elasticsearch技术解析与实战(二)文档的CRUD操作