【交互 / 差分约束】

题目

代码

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int N = 10510;
const int M = 200 * 500 + 10;
int h[N], ne[M], e[M], w[M], idx;
ll d[N];
int n, m;
bool st[N];
int cnt[N];

void add(int a, int b, int c)
{
	w[idx] = c, e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
ll spfa()
{
	memset(d, 0x3f, sizeof d);
	queue<int> q;
	for(int i = 1; i <= n+m; i++)
		q.push(i);
	
	while(q.size())
	{
		int u = q.front(); q.pop();
		st[u] = 0;
		
		for(int i = h[u]; ~i; i = ne[i])
		{
			int j = e[i];
			if(d[j] > d[u] + w[i])
			{
				d[j] = d[u] + w[i];
				cnt[j] += 1;
				if(cnt[j] >= m) return -1;
				if(!st[j])
				{
					q.push(j);
					st[j] = 1;
				}
			}
		}
	}
	
	ll maxx = d[1], minn = d[1];
	for(int i = 2; i <= m; i++)
		maxx = max(maxx, d[i]), minn = min(minn, d[i]);
	return maxx - minn;
}	
int main()
{	
	cin >> n >> m;
	
	memset(h, -1, sizeof h);
	
	
	for(int t = 1; t <= n; t++)
	{
		int l, r, p, q, ans;
		cin >> l >> r >> p >> q >> ans;
		
		for(int i = l; i <= r; i++)
			add(i, m+t, 0);
		for(int j = p; j <= q; j++)
			add(m+t, j, -ans);
	}
	
	ll ans = spfa();
	if(ans == -1) puts("No Solution");
	else cout << ans;
}
相关推荐
机器学习与统计学4 分钟前
阿里牛逼,又开源两个遥遥领先的模型(向量化、重排),知识库要翻天地覆了
算法
小河豚oO8 分钟前
LeetCode刷题---贪心算法---944
算法·leetcode·贪心算法
【杨(_> <_)】10 分钟前
信号处理分析工具——时频分析(一)
算法·matlab·信号处理
还不起来学习?14 分钟前
常见算法题目5 -常见的排序算法
java·算法·排序算法
Once_day26 分钟前
代码训练LeetCode(23)随机访问元素
算法·leetcode
小河豚oO42 分钟前
LeetCode 热题 100 - 哈希 - 128
算法·leetcode·哈希算法
客卿12343 分钟前
力扣100题之128. 最长连续序列
算法·leetcode·哈希算法
T1an-143 分钟前
【力扣链表篇】206.反转链表
算法·leetcode·链表
xphjj2 小时前
树形数据模糊搜索
前端·javascript·算法
Once_day2 小时前
代码训练LeetCode(24)数组乘积
算法·leetcode