POJ1201 Intervals【差分约束系统】
DescriptionYou are given n closed,integer intervals [ai,bi] and n integers c1,...,cn. InputThe first line of the input contains an integer n (1 <= n <= 50000) -- 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 <= 50000 and 1 <= ci <= bi - ai+1. OutputThe output contains exactly one integer equal to the minimal size of set Z sharing at least ci elements with interval [ai,n. Sample Input5 Sample Output6 思路发现对于整个区间的约束是可以转化成前缀和的 注意这里的最长路,其实就是满足所有约束的最小代价 //Author: dream_maker #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std; //---------------------------------------------- //typename typedef long long ll; //convenient for #define fu(a,b,c) for (int a = b; a <= c; ++a) #define fd(a,c) for (int a = b; a >= c; --a) #define fv(a,b) for (int a = 0; a < (signed)b.size(); ++a) //inf of different typename const int INF_of_int = 1e9; const ll INF_of_ll = 1e18; //fast read and write template <typename T> void Read(T &x) { bool w = 1;x = 0; char c = getchar(); while (!isdigit(c) && c != '-') c = getchar(); if (c == '-') w = 0,c = getchar(); while (isdigit(c)) { x = (x<<1) + (x<<3) + c -'0'; c = getchar(); } if (!w) x = -x; } template <typename T> void Write(T x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) Write(x / 10); putchar(x % 10 + '0'); } //---------------------------------------------- const int N = 1e5 + 10; struct Edge { int v,w,nxt; Edge(int v = 0,int w = 0,int nxt = 0):v(v),w(w),nxt(nxt) {} } E[N * 3]; int head[N],tot = 0; int n,a[N],b[N],c[N],maxl = 0; int dis[N],inq[N]; void add(int u,int v,int w) { E[++tot] = Edge(v,head[u]); head[u] = tot; } void spfa() { queue<int> q; q.push(0); fu(i,1,maxl) dis[i] = -INF_of_int; while (q.size()) { int u = q.front(); q.pop(); inq[u] = 0; for (int i = head[u]; i != -1; i = E[i].nxt) { int v = E[i].v; if (dis[v] < dis[u] + E[i].w) { dis[v] = dis[u] + E[i].w; if (!inq[v]) { inq[v] = 1; q.push(v); } } } } } int main() { Read(n); memset(head,-1,sizeof(head)); fu(i,n) { Read(a[i]); ++a[i]; Read(b[i]); ++b[i]; Read(c[i]); maxl = max(maxl,max(a[i],b[i])); add(a[i] - 1,b[i],c[i]); } fu(i,maxl) add(i - 1,i,0); fu(i,maxl) add(i,i - 1,-1); spfa(); Write(dis[maxl]); return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |