【交互 / 差分约束】

题目

代码

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;
}
相关推荐
沙威玛_LHE2 小时前
树和二叉树
数据结构·算法
py有趣3 小时前
LeetCode算法学习之两数之和 II - 输入有序数组
学习·算法·leetcode
夏鹏今天学习了吗4 小时前
【LeetCode热题100(62/100)】搜索二维矩阵
算法·leetcode·矩阵
吃着火锅x唱着歌6 小时前
LeetCode 1128.等价多米诺骨牌对的数量
算法·leetcode·职场和发展
十八岁讨厌编程6 小时前
【算法训练营 · 补充】LeetCode Hot100(中)
算法·leetcode
橘颂TA6 小时前
【剑斩OFFER】算法的暴力美学——最小覆盖字串
算法·c/c++·就业
wearegogog1236 小时前
基于混合蛙跳算法和漏桶算法的无线传感器网络拥塞控制与分簇新方法
网络·算法
Tiandaren7 小时前
大模型应用03 || 函数调用 Function Calling || 概念、思想、流程
人工智能·算法·microsoft·数据分析
2301_795167208 小时前
玩转Rust高级应用 如何进行理解Refutability(可反驳性): 模式是否会匹配失效
开发语言·算法·rust
小当家.1058 小时前
[LeetCode]Hot100系列.贪心总结+思想总结
算法·leetcode·职场和发展