AOJ-517 序列(差分约束)

来源:互联网 发布:linux 删除匹配文件名 编辑:程序博客网 时间:2024/06/14 05:05
序列
Time Limit: 1000 ms   Case Time Limit: 1000 ms   Memory Limit: 64 MB
Judge By Case
Description
有一个整数序列,它的每个数各不相同,我们不知道它的长度是多少(即整数个数),但我们知道在某些区间中至少有多少个整数,用区间(Li,Ri,Ci)来描述,表示这个整数序列中至少有Ci个数来自区间[Li,Ri],给出若干个这样的区间,问这个整数序列的长度最少能为多少?

Input
第一行一个整数N(N<=1000),表示区间个数;
接下来N行,每行三个整数(Li,Ri,Ci)(0<=Li<=Ri<=1000,1<=Ci<=Ri-Li+1),描述一个区间。

Output
仅一个数,表示该整数序列的最小长度。

Sample Input
OriginalTransformed
44 5 16 10 37 10 35 6 1

Sample Output
OriginalTransformed
4

————————————————————集训22.2的分割线————————————————————

前言:终于用正解写出来了。之前是一个姿势相当复杂的贪心A掉的。。

思路:标准的差分约束。只是给出的是点、点上有数值:0或1,要转换成区间,r++就行了。

P.S. 把给出的区间个数当做点的个数WA到死了!低级错误。

代码如下:

/*ID: j.sure.1PROG:LANG: C++*//****************************************/#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>#include <stack>#include <queue>#include <vector>#include <map>#include <string>#include <iostream>#define INF 0x3f3f3f3f#define LL long longusing namespace std;/****************************************/const int N = 1005, M = 5000;int head[N], dis[N], q[N], tot, m;struct Node {int v, w, next;}edge[M];bool inq[N];void add(int u, int v, int w){edge[tot] = (Node){v, w, head[u]};head[u] = tot++;}void spfa(int st){for(int i = 0; i <= N; i++) {dis[i] = -INF;inq[i] = false;}dis[st] = 0;int fron = 0, rear = 1;q[fron] = st;inq[st] = true;while(fron < rear) {int u = q[fron%N]; fron++;inq[u] = false;for(int i = head[u]; i != -1; i = edge[i].next) {int v = edge[i].v;if(dis[v] < dis[u] + edge[i].w) {dis[v] = dis[u] + edge[i].w;if(!inq[v]) {q[rear%N] = v; rear++;inq[v] = true;}}}}}int main(){#ifdef J_Sure//freopen("000.in", "r", stdin);//freopen(".out", "w", stdout);#endiftot = 0;memset(head, -1, sizeof(head));scanf("%d", &m);int l, r, w, R = -1, L = N;for(int i = 0; i < m; i++) {scanf("%d%d%d", &l, &r, &w);r++;R = max(R, r); L = min(L, l);//S[r] - S[l] >= wadd(l, r, w);}for(int i = L+1; i <= R; i++) {//S[i] - S[i-1] <= 1add(i, i-1, -1);//S[i] - S[i-1] >= 0add(i-1, i, 0);}spfa(L);printf("%d\n", dis[R]);return 0;}


0 0
原创粉丝点击