POJ1201 Intervals差分约束系统(最短路)
Description
Write a program that:
reads the number of intervals, their end points and integers c1, ..., cn from the standard input,
computes the minimal size of a set Z of integers which has at least ci common elements with interval [ai, bi], for each i=1,2,...,n,
writes the answer to the standard output.
Input
Output
Sample Input
- 5
- 3 7 3
- 8 10 3
- 6 8 1
- 1 3 1
- 10 11 1
Sample Output
- 6
Source
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <queue>
- #include <stack>
- #include <vector>
- using namespace std;
- const int INF = 0x3f3f3f3f;
- const int N = 5e4 + ;
- struct Edge {
- int v, cost;
- Edge() {}
- Edge(int v, int cost) : v(v), cost(cost) {}
- };
- vector<Edge> E[N];
- void add(int u, int v, int w) {
- E[u].push_back(Edge(v, w));
- }
- bool vis[N];
- int d[N];
- void SPFA(int st, int n) {
- memset(vis, false, sizeof vis);
- for(int i = ; i <= n; ++i) d[i] = -INF;
- d[st] = ;
- vis[st] = true;
- queue<int> que;
- while(!que.empty()) que.pop();
- que.push(st);
- while(!que.empty()) {
- int u = que.front();
- que.pop();
- vis[u] = false;
- int sx = E[u].size();
- for(int i = ; i < sx; ++i) {
- int v = E[u][i].v;
- int w = E[u][i].cost;
- if(d[v] < d[u] + w) {
- d[v] = d[u] + w;
- if(!vis[v]) {
- vis[v] = true;
- que.push(v);
- }
- }
- }
- }
- }
- int main() {
- int n;
- while(~scanf("%d", &n)) {
- int a, b, c, m = , st = INF;
- for(int i = ; i < n; ++i) {
- scanf("%d%d%d", &a, &b, &c);
- a+=; b+=;
- add(a - , b, c);
- m = max(m, b);
- st = min(st, a - );
- }
- for(int i = ; i <= m; ++i) {
- add(i, i - , -);
- add(i - , i, );
- }
- SPFA(st, m);
- printf("%d\n", d[m]);
- }
- return ;
- }
POJ1201 Intervals差分约束系统(最短路)的更多相关文章
- POJ1201 Intervals[差分约束系统]
Intervals Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26028 Accepted: 9952 Descri ...
- [BZOJ2330][SCOI2011]糖果 差分约束系统+最短路
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2330 类似于题目中这种含有不等式关系,我们可以建立差分约束系统来跑最长路或最短路. 对于一 ...
- Intervals(差分约束系统)
http://poj.org/problem?id=1201 题意:给定n个整数闭区间[a,b]和n个整数c,求一个最小的整数集合Z,满足Z里边的数中范围在闭区间[a,b]的个数不小于c个. 思路:根 ...
- POJ 1201 Intervals (差分约束系统)
题意 在区间[0,50000]上有一些整点,并且满足n个约束条件:在区间[ui, vi]上至少有ci个整点,问区间[0, 50000]上至少要有几个整点. 思路 差分约束求最小值.把不等式都转换为&g ...
- POJ1201 Intervals(差分约束)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 10966 Description You ...
- poj1201 Intervals——差分约束
题目:http://poj.org/problem?id=1201 差分约束裸题: 设 s[i] 表示到 i 选了数的个数前缀和: 根据题意,可以建立以下三个限制关系: s[bi] >= s[a ...
- poj 1201/zoj 1508 intervals 差分约束系统
// 思路 : // 图建好后 剩下的就和上一篇的 火烧连营那题一样了 求得解都是一样的 // 所以稍微改了就过了 // 最下面还有更快的算法 速度是这个算法的2倍#include <ios ...
- PKU 1201 Intervals(差分约束系统+Spfa)
题目大意:原题链接 构造一个集合,这个集合内的数字满足所给的n个条件,每个条件都是指在区间[a,b]内至少有c个数在集合内.问集合最少包含多少个点.即求至少有多少个元素在区间[a,b]内. 解题思路: ...
- POJ1201 Intervals(差分约束系统)
与ZOJ2770一个建模方式,前缀和当作点. 对于每个区间[a,b]有这么个条件,Sa-Sb-1>=c,然后我就那样连边WA了好几次. 后来偷看数据才想到这题还有两个隐藏的约束条件. 这题前缀和 ...
随机推荐
- net 页面跳转
前台: < a href="xx.html" target="_blank"> 后台: Response.Redirect("XXX.as ...
- java课后作业
课后作业之字串加密: 设计思想: 1.输入要加密的英文子串str 2.定义num=str的字符串长度 3.将字符串转化为单个字符 4.每个字符+3,向后移3个 5.定义str1,将新得到的每个字符加到 ...
- vs c# int & int32
在vs c#中,int就等价于int32, 所以通常也是使用int32 当在统计总数时,最好使用int32,int16数值范围太小,如果超出,就会变成随机数.
- FILE文件操作
http://www.jb51.net/article/37688.htm fopen(打开文件)相关函数 open,fclose表头文件 #include<stdio.h>定义函数 FI ...
- mybatis配置问题
//当构造函数有多个参数时,可以使用constructor-arg标签的index属性,index属性的值从0开始. <bean id="sqlSession" class= ...
- 利用drozer进行Android渗透测试
一.安装与启动 1. 安装 第一步:从 http://mwr.to/drozer 下载Drozer (Windows Installer) 第二步:在 Android 设备中安装 agent.apk ...
- 对Object类中方法的深入理解
看一下API中关于Object的介绍: 类 Object 是类层次结构的根类.每个类都使用 Object 作为超类.所有对象(包括数组)都实现这个类的方法. 那么Object中到底有哪些方法,各自有什 ...
- sdut 2445 小学数学
小学数学 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 题目链接:http://acm.sdut.edu.cn/sdutoj/p ...
- GMap.Net开发之在WinForm和WPF中使用GMap.Net地图插件
GMap.NET是什么? 来看看它的官方说明:GMap.NET is great and Powerful, Free, cross platform, open source .NET contro ...
- CodeForces 371D Vessels(树状数组)
树状数组,一个想法是当往p注水时,认为是其容量变小了,更新时二分枚举,注意一些优化. #include<cstdio> #include<iostream> #include& ...