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: sudaweitong@gmail.com
 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 }

 

posted on 2014-08-26 11:07  Stomach_ache  阅读(249)  评论(0编辑  收藏  举报

导航