Hdu 1384(差分约束)

题目链接

Intervals

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2931    Accepted Submission(s): 1067

Problem Description

You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn.

Write a program that:

> reads the number of intervals, their endpoints 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

The
first line of the input contains an integer n (1 <= n <= 50 000) -
the number of intervals. The following n lines describe the intervals.
The i+1-th line of the input contains three integers ai, bi and ci
separated by single spaces and such that 0 <= ai <= bi <= 50
000 and 1 <= ci <= bi - ai + 1.

Process to the end of file.

Output

The
output contains exactly one integer equal to the minimal size of set Z
sharing at least ci elements with interval [ai, bi], for each i = 1, 2,
..., n.

Sample Input

5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1

Sample Output

6

令1..i共选d[i]个数,那么有0 <= d[i + 1] - d[i] <= 1;

并且对于每个约束a, b, c, 都有 d[b] - d[a - 1] >= c

这样就是一个线性规划问题,可以用最短路求解。

对于求最大值,就将不等式转化为求最短路中的三角不等式,即:d[u] + w >= d[v], 然后求最短路即可。

对于求最小值,就将不等式转化为求最长路中的三角不等式,即:d[u] + w <= d[v], 然后求最长路即可。

当然求最小值时,也可以按求最大值的方法加边,只不过这时候加的边都是反向边,从终点到起点跑最短路,然后结果取反就可以了。

Accepted Code:

 1 /*************************************************************************
 2     > File Name: 1384.cpp
 3     > Author: Stomach_ache
 4     > Mail: [email protected]
 5     > Created Time: 2014年08月26日 星期二 08时59分19秒
 6     > Propose:
 7  ************************************************************************/
 8 #include <queue>
 9 #include <cmath>
10 #include <string>
11 #include <cstdio>
12 #include <vector>
13 #include <fstream>
14 #include <cstring>
15 #include <iostream>
16 #include <algorithm>
17 using namespace std;
18 /*Let‘s fight!!!*/
19
20 const int INF = 0x3f3f3f3f;
21 const int MAX_N = 50050;
22 typedef pair<int, int> pii;
23 vector<pii> G[MAX_N];
24 int n, d[MAX_N];
25 bool inq[MAX_N];
26
27 void AddEdge(int u, int v, int w) {
28       G[u].push_back(pii(v, w));
29 }
30
31 void spfa(int s) {
32       queue<int> Q;
33     memset(d, 0x3f, sizeof(d));
34     memset(inq, false, sizeof(inq));
35     d[s] = 0;
36     inq[s] = true;
37     Q.push(s);
38     while (!Q.empty()) {
39           int u = Q.front(); Q.pop(); inq[u] = false;
40         for (int i = 0; i < G[u].size(); i++) {
41               int v = G[u][i].first, w = G[u][i].second;
42             if (d[u] + w < d[v]) {
43                   d[v] = d[u] + w;
44                 if (!inq[v]) Q.push(v), inq[v] = true;
45             }
46         }
47     }
48 }
49
50 int main(void) {
51       while (~scanf("%d", &n)) {
52           for (int i = 0; i <= 50005; i++) G[i].clear();
53         int s = INF, t = -1;
54         for (int i = 0; i < n; i++) {
55               int a, b, c;
56             scanf("%d %d %d", &a, &b, &c);
57             b++;
58             s = min(s, a); t = max(t, b);
59             AddEdge(b, a, -c);
60         }
61         for (int i = s; i < t; i++) AddEdge(i, i + 1, 1), AddEdge(i + 1, i, 0);
62
63         spfa(t);
64
65         printf("%d\n", -d[s]);
66     }
67
68     return 0;
69 }
时间: 2024-11-07 16:19:03

Hdu 1384(差分约束)的相关文章

hdu 1364(差分约束)

King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12056   Accepted: 4397 Description Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen prayed: ``If my child was a son and if only he was a sound kin

hdu 3666(差分约束,手动栈解决超时问题)

THE MATRIX PROBLEM Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8016    Accepted Submission(s): 2092 Problem Description You have been given a matrix CN*M, each element E of CN*M is positive

Instrction Arrangement (hdu 4109 差分约束)

Instrction Arrangement Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1395    Accepted Submission(s): 584 Problem Description Ali has taken the Computer Organization and Architecture course th

hdu 1534(差分约束)

Schedule Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1715    Accepted Submission(s): 757Special Judge Problem Description A project can be divided into several parts. Each part shoul

HDU 1384 Intervals【差分约束-SPFA】

类型:给出一些形如a−b<=k的不等式(或a−b>=k或a−b<k或a−b>k等),问是否有解[是否有负环]或求差的极值[最短/长路径].例子:b−a<=k1,c−b<=k2,c−a<=k3.将a,b,c转换为节点:k1,k2,k3转换为边权:减数指向被减数,形成一个有向图: 由题可得(b−a) + (c−b) <= k1+k2,c−a<=k1+k2.比较k1+k2与k3,其中较小者就是c−a的最大值.由此我们可以得知求差的最大值,即上限被约束,此时我

hdu 差分约束题集

[HDU]1384 Intervals 基础差分约束★1529 Cashier Employment 神级差分约束★★★★ 1531 King 差分约束★1534 Schedule Problem 差分约束输出一组解★3440 House Man 比较好的差分约束★★3592 World Exhibition 简单★3666 THE MATRIX PROBLEM 中等★★4274 Spy's Work [先处理出欧拉序列,然后就是差分约束了...] [POJ]1201 Intervals1275

Hdu 3666 THE MATRIX PROBLEM(差分约束)

题目地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=3666 思路:差分约束. 取对数将乘除转化为加减. L<=m[i][j]*a[i]/b[j]<=U log(L/m[i][j])<=log(a[i])-log(b[j])<=log(U/m[i][j]) 则 : log(a[i])<=log(b[j])+log(U/m[i][j]) log(b[j])<=log(a[i])+log(m[i][j]/L) SPFA判

Hdu 4594 Difference(奇圈判断+差分约束)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4598 思路:由题意可知两相连点ai符号一定相反,所以若存在奇圈则一定无解.染色,colour[i]==1表示为正,colour[i]==2表示为负.由于(b)条件为充要条件,所以对于图中的点| a[i]-a[j] | >= T,对于非图中点| a[i]-a[j] | < T,即| a[i]-a[j] | <= T-1 .所以图中点,若colour[i]==1,a[i]-a[j] >=

POJ 1201 &amp;&amp; HDU 1384 Intervals(差分约束系统)

题目地址:POJ 1201   HDU 1384 根据题目意思,可以列出不等式如下: Sj-Si>=c; Si-S(i-1)>=0; S(i-1)-Si>=-1; 然后用最短路spfa来解决这个不等式.用max来当源点,0为终点.最终的-d[0]就是答案. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <

HDU 3666 THE MATRIX PROBLEM (差分约束)

题意:给定一个最大400*400的矩阵,每次操作可以将某一行或某一列乘上一个数,问能否通过这样的操作使得矩阵内的每个数都在[L,R]的区间内. 析:再把题意说明白一点就是是否存在ai,bj,使得l<=cij*(ai/bj)<=u (1<=i<=n,1<=j<=m)成立. 首先把cij先除到两边去,就变成了l'<=ai/bj<=u',由于差分约束要是的减,怎么变成减法呢?取对数呗,两边取对数得到log(l')<=log(ai)-log(bj)<=l