[POJ 1201]Intervals[差分约束]
来源:互联网 发布:韩国女主播软件 编辑:程序博客网 时间:2024/06/04 18:43
题目链接:[POJ 1201]Intervals[差分约束]
题意分析:
区间[ai, bi]至少有ci个点在集合Z中,现在问:集合Z中,元素最少是多少个?
解题思路:
题目相当于是说点[ai,bi + 1)间,有>=ci的点在集合Z中,即:ai->bi+1 >= ci。把点看作图中的点,则有:dis[bi + 1] >= ci + dis[ai],翻译成转移语言:if (dis[bi + 1] < ci + dis[ai]) dis[bi + 1] = dis[ai] + ci。就是求图中的最长距离了。这样还遗留一个问题,起始点到终点路不全啊,这个时候我们就要挖掘隐含条件了1 >= dis[i + 1] - dis[i] >= 0。这样就能建图了。答案就是从最小点到最大点的最长路径。
个人感受:
老是不能很好地根据不等式确定是怎么样的求路径。用转移写出来就好理解太多了,赞!另外隐含条件没好好去想,列出式子就GG了。。。。
具体代码如下:
#include<algorithm>#include<cctype>#include<cmath>#include<cstdio>#include<cstring>#include<iomanip>#include<iostream>#include<map>#include<queue>#include<set>#include<sstream>#include<stack>#include<string>#define pr(x) cout << #x << " = " << (x) << '\n';using namespace std;const int INF = 0x7f7f7f7f;const int MAXN = 5e4 + 111;const int MAXM = 2e5 + 111;struct Edge { int to, next, w;}edge[MAXM];int head[MAXN], tol, dis[MAXN];void addedge(int u, int v, int w) { edge[tol].to = v; edge[tol].next = head[u]; edge[tol].w = w; head[u] = tol++;}bool in[MAXN];void spfa(int s) { queue<int> q; q.push(s); dis[s] = 0; in[s] = 1; while (q.size()) { int u = q.front(); q.pop(); in[u] = 0; for (int i = head[u]; ~i; i = edge[i].next) { int v = edge[i].to; if (dis[v] < dis[u] + edge[i].w) { dis[v] = dis[u] + edge[i].w; if (!in[v]) { in[v] = 1; q.push(v); } } } }}int main(){ int n, mi = INF, mx = 0, u, v, w; memset(head, -1, sizeof head); tol = 0; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d%d%d", &u, &v, &w); addedge(u, v + 1, w); mi = min(mi, u); mx = max(mx, v + 1); } for (int i = mi; i < mx; ++i) { addedge(i, i + 1, 0); addedge(i + 1, i, -1); dis[i] = -INF; } dis[mx] = -INF; spfa(mi); printf("%d\n", dis[mx]); return 0;}
0 0
- POJ 1201 Intervals 差分约束+spfa
- poj 1201 Intervals(差分约束)
- poj 1201 Intervals 差分约束 + spfa
- POJ 1201 Intervals [差分约束]
- POJ 1201 Intervals(差分约束)
- POJ 1201 Intervals 差分约束系统
- POJ 1201 Intervals (差分约束)
- poj 1201 Intervals 差分约束+spfa
- POJ 1201 Intervals (差分约束)
- poj-1201- Intervals-差分约束问题
- POJ 1201 Intervals 差分约束系统
- poj 1201 Intervals (差分约束)
- poj 1201 Intervals(差分约束)
- POJ 1201 Intervals(差分约束)
- poj 1201 Intervals 差分约束
- POJ 1201 Intervals(差分约束基础)
- poj 1201 Intervals [差分约束]
- POJ 1201-Intervals(差分约束系统)
- 打印非负整数阶乘的函数
- 求字符串长度
- 基于DES加密的TCP聊天程序
- react学习札记(三)--自己的日历控件
- 模式匹配基本语法与在集合中的使用
- [POJ 1201]Intervals[差分约束]
- linux 扫描端口
- 第4周项目4程序分析(1)
- 第三次上机上机实践项目-项目5-(1)
- LeetCode-78-Subsets(回溯法)-Medium
- Python常用内置函数
- vsftpd的虚拟账户配置
- 阿里巴巴2017届实习生笔试题反思
- 关于python爬虫的一点心得