hdu 1384 Intervals (差分约束)
好歹用了一天,也算是看懂了差分约束的原理,做出第一条查分约束了。
题意是告诉你一些区间中最少有多少元素,最少需要多少个元素才能满足所有要求。
构图的方法是,(a)->(b+1)=c。还有就是所有的相邻的点都要连上(i+1)->(i)=0,(i)->(i+1)=-1。因为我对点离散了,所以就变成(rx[i])->(rx[i+1])=rx[i]-rx[i+1]。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream> using namespace std; const int N = ;
const int INF = 0x55555555;
struct Edge {
int id, nx, val;
Edge() {}
Edge(int id, int nx, int val) : id(id), nx(nx), val(val) {}
} edge[N << ];
int eh[N], ec; void init() {
ec = ;
memset(eh, -, sizeof(eh));
} void addedge(int u, int v, int w) {
edge[ec] = Edge(v, eh[u], w);
eh[u] = ec++;
} int rx[N << ], dis[N];
bool vis[N];
queue<int> Q; void spfa(int s) {
while (!Q.empty()) Q.pop();
memset(vis, , sizeof(vis));
for (int i = ; i < N; i++) dis[i] = -INF;
Q.push(s);
vis[s] = true;
dis[s] = ;
while (!Q.empty()) {
int cur = Q.front();
Q.pop();
vis[cur] = false;
for (int t = eh[cur]; ~t; t = edge[t].nx) {
if (dis[edge[t].id] < dis[cur] + edge[t].val) {
dis[edge[t].id] = dis[cur] + edge[t].val;
if (vis[edge[t].id]) continue;
Q.push(edge[t].id);
vis[edge[t].id] = true;
}
}
}
} int main() {
// freopen("in", "r", stdin);
int n, x, y, z;
while (~scanf("%d", &n)) {
init();
int cnt = ;
for (int i = ; i < n; i++) {
scanf("%d%d%d", &x, &y, &z);
addedge(x, y + , z);
rx[cnt++] = x;
rx[cnt++] = y + ;
}
sort(rx, rx + cnt);
cnt = unique(rx, rx + cnt) - rx;
for (int i = ; i < cnt; i++) addedge(rx[i], rx[i - ], rx[i - ] - rx[i]), addedge(rx[i - ], rx[i], );
spfa(rx[]);
printf("%d\n", dis[rx[cnt - ]]);
}
return ;
}
跑的比较慢,元素进队的次数比较多。用固定大小的queue因为越界WA了好多次。
——written by Lyon
hdu 1384 Intervals (差分约束)的更多相关文章
- hdu 1384 Intervals (差分约束)
Intervals Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- hdu 1384 Intervals (差分约束)
/* 给你 n 个区间 [Ai, Bi],要求从每一个区间中至少选出 Ci 个数出来组成一个序列 问:满足上面条件的序列的最短长度是多少? 则对于 不等式 f(b)-f(a)>=c,建立 一条 ...
- Hdu 1384(差分约束)
题目链接 Intervals Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 1384 Intervals【差分约束-SPFA】
类型:给出一些形如a−b<=k的不等式(或a−b>=k或a−b<k或a−b>k等),问是否有解[是否有负环]或求差的极值[最短/长路径].例子:b−a<=k1,c−b&l ...
- POJ1201 Intervals(差分约束)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 10966 Description You ...
- POJ 1364 / HDU 3666 【差分约束-SPFA】
POJ 1364 题解:最短路式子:d[v]<=d[u]+w 式子1:sum[a+b+1]−sum[a]>c — sum[a]<=sum[a+b+1]−c−1 ...
- poj 1716 Integer Intervals (差分约束 或 贪心)
Integer Intervals Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12192 Accepted: 514 ...
- zoj 1508 Intervals (差分约束)
Intervals Time Limit: 10 Seconds Memory Limit: 32768 KB You are given n closed, integer interva ...
- POJ 3169 Layout (HDU 3592) 差分约束
http://poj.org/problem?id=3169 http://acm.hdu.edu.cn/showproblem.php?pid=3592 题目大意: 一些母牛按序号排成一条直线.有两 ...
随机推荐
- 从页面获取form表单提交的数据
1 使用HttpServletRequest,方便灵活 页面代码,使用action提交一个表单,里边有球的id,球的主人,球的颜色,所在省份,区域 <form action="ball ...
- day18 10.使用ThreadLocal来解决问题
ThreadLocal是一个容器/集合,是一个Map集合.不管你跨多少层,只要你是同一个线程就可以取出来.Service和Dao是同一个线程.Service第一次调用JdbcUtils.getConn ...
- Codeforces 1150D(字符串dp)
反思 三维的dp压根没看出来,看题解以后思路又很直观,找几道字符串dp练练才行 序列自动机和优化一维略 /* __ __ * ____| |_____| |____ * | | * | __ | * ...
- Faster RCNN算法demo代码解析
一. Faster-RCNN代码解释 先看看代码结构: Data: This directory holds (after you download them): Caffe models pre-t ...
- ML面试1000题系列(1-20)
本文总结ML面试常见的问题集 转载来源:https://blog.csdn.net/v_july_v/article/details/78121924 1.简要介绍SVM 全称是support vec ...
- 2016年中国独角兽企业估值榜 TOP300
2016年中国独角兽企业估值榜 TOP300[完整榜单] 类型:品牌资讯/名企动态 阅读:6735次 来源: 中商情报网 我要评论 摘要:独角兽公司是什么?独角兽公司指的是那些估值达到10亿美元以 ...
- 使用Centos7 安装docker的步骤
1.Linuxyum源切换阿里云软件源 备份本机软件源 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.b ...
- .net core/.net 使用 CommandLineParser 来标准化地解析命令行
CommandLineParser 是一款用于解析命令行参数的 NuGet 包.你只需要关注你的业务,而命令行解析只需要极少量的配置代码. 本文将介绍如何使用 CommandLineParser 高效 ...
- vue中element-ui添加按钮
<div v-for="(v,i) in list"> <el-form label-width="120px" size="sma ...
- 剑指offer 1-6
1. 二维数组中的查找 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 分析 ...