poj1716 Integer Intervals (差分约束)
来源:互联网 发布:识别颜色的软件 编辑:程序博客网 时间:2024/05/16 08:06
题意:
给定n个区间,要求选一些数,使得每个区间最少包含两个数。问最少要选几个数。
思路:
设s[i]为从0到i选了几个数,
对于每一个区间[l, r],
可知:s[r] - s[l - 1] >= 2
s[i] - s[i - 1] >= 0
s[i] - s[i - 1] <= 1
根据这几个不等式建图,然后求最长路。
代码(1384K,1000MS):
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <vector>#include <queue>using namespace std;struct Edge{int to, v;Edge(int a, int b) : to(a), v(b) {}};int n;vector<Edge> edges[10005];int vis[10005];int dist[10005];void addEdge(int from, int to, int v) {edges[from].push_back(Edge(to, v));}int spfa(int t) {memset(vis, 0, sizeof(vis));for (int i = 0; i <= t; i++)dist[i] = -0x3f3f3f3f;queue<int> q;q.push(0);vis[0] = 1;dist[0] = 0;while (!q.empty()) {int current = q.front();q.pop();for (int i = 0; i < edges[current].size(); i++) {Edge e = edges[current][i];if (dist[current] + e.v > dist[e.to]) {dist[e.to] = dist[current] + e.v;if (!vis[e.to]) {vis[e.to] = 1;q.push(e.to);}}}vis[current] = 0;}return dist[t];}int main() {while (~scanf("%d", &n)) {int a, b;int s = 0, t = 0;for (int i = 0; i <= 10003; i++)edges[i].clear();for (int i = 0; i < n; i++) {scanf("%d %d", &a, &b);addEdge(a, b + 1, 2);t = max(t, b + 1);}for (int i = 0; i <= t; i++) {addEdge(i, i + 1, 0);addEdge(i + 1, i, -1);}int ans = spfa(t);printf("%d\n", ans);}return 0;}
0 0
- poj1716 Integer Intervals (差分约束)
- poj1716 Integer Intervals(差分约束)
- poj1716 Integer Intervals 差分约束
- poj1716——Integer Intervals//差分约束系统
- [POJ1716]Integer Intervals(差分约束系统)
- poj1716 Integer Intervals--单源最短路径&差分约束
- POJ--1716[Integer Intervals] 差分约束
- Poj 1716 Integer Intervals(差分约束)
- 【POJ 1716 Integer Intervals】+ 差分约束
- POJ 1716 Integer Intervals 差分约束
- poj1716 差分约束系统
- poj1716-差分约束+spfa
- POJ 1201 Intervals || POJ 1716 Integer Intervals 差分约束
- POJ1716-Integer Intervals(区间约束)
- Intervals(差分约束)
- poj1716(差分约束+Bellman_Ford)
- poj 1716 Integer Intervals(差分约束系统)
- POJ 1716 Integer Intervals SPFA 差分约束
- 软件资源管理模式(1)
- easyUI的getChanges中的inserted
- 2月份目标
- unity学习之进度条
- C++面向对象编程 友元函数
- poj1716 Integer Intervals (差分约束)
- 如何添加doubango工程
- Python标准库:内置函数iter(object[, sentinel])
- [Linux杂记] 用户添加 删除 及赋权限
- 解析 Linux 中的 VFS 文件系统机制
- C++ Primer(面向对象编程 二)
- 算法篇----粒子群优化(PSO)算法回顾(一)
- 【VC++技术杂谈004】使用微软TTS语音引擎实现文本朗读
- Java 当类的属性与形参的名字相同时