标签:差分约束 ret void cpp empty weight mat queue 约束
当然是差分约束了!
两个条件:\(a_r - a_{l - 1} \geq c\) 和 \(0 \geq a_i - a_{i - 1} \leq 1\)
这里统一化为大于等于,成为最长路。
直接求出最长路即可。
PS:队列抽风了。。。有STL的队列才过了。我是不是得退役了。。。
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
const int maxn = 30005, maxm = 50005;
struct Edges
{
int next, to, weight;
} e[maxm << 4];
int head[maxn], tot;
int n, m;
bool vis[maxn];
int dist[maxn];
std::queue<int> q;
void link(int u, int v, int w)
{
e[++tot] = (Edges){head[u], v, w};
head[u] = tot;
}
void spfa(int s)
{
memset(dist, -1, sizeof(dist));
dist[s] = 0;
q.push(s); vis[s] = true;
while(!q.empty())
{
int u = q.front(); q.pop(); vis[u] = false;
for(int i = head[u]; i != -1; i = e[i].next)
{
int v = e[i].to;
if(dist[u] + e[i].weight > dist[v])
{
dist[v] = dist[u] + e[i].weight;
if(!vis[v])
{
q.push(v); vis[v] = true;
}
}
}
}
}
int main()
{
memset(head, -1, sizeof(head));
scanf("%d%d", &n, &m);
while(m--)
{
int l, r, c; scanf("%d%d%d", &l, &r, &c);
//a[r] - a[l - 1] >= c
link(l - 1, r, c);
}
for(int i = 1; i <= n; i++)
{
//0 <= a[i] - a[i - 1] <= 1
//a[i] - a[i - 1] >= 0
//a[i - 1] - a[i] >= -1
link(i - 1, i, 0);
link(i, i - 1, -1);
}
spfa(0);
printf("%d\n", dist[n]);
return 0;
}
标签:差分约束 ret void cpp empty weight mat queue 约束
原文地址:https://www.cnblogs.com/Garen-Wang/p/9279929.html