Title Link: http://poj.org/problem?id=1201
Description
You are given n closed, integer intervals [AI, bi] and n integers c1, ..., CN.
Write a program:
Reads the number of intervals, their end points and integers C1, ..., CN from the standard input,
Computes the minimal size of a set Z of integers which have at least CI common elements with interval [AI, bi], for each i= ,..., N,
Writes the answer to the standard output.
Input
The 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 & lt;= bi <= 50000 and 1 <= ci <= bi-ai+1.
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
53 7 38 10 36 8 11 3 110 11 1
Sample Output
6
Source
Southwestern Europe 2002
Test instructions: (EXT)
The [AI, bi] interval and point set Z have at least one common element of CI, that is, if I use SI to denote at least how many elements in the interval [0,i] interval, then Sbi-sai >= ci, so that we construct a series of edges, the weights are CI, but this is not enough, because there are many points still not connected (that is, from the beginning may not be at the end of the route), at this time, we look at the definition of Si, it is not difficult to write 0<=si-si-1<=1 restrictions , although it does not seem to make any sense, but if you also construct a series of edges, the shortest path from the starting point to the end point is a logical occurrence.
We write the above restrictions in the form of consent:
Sbi-sai >= CI
Si-si-1 >= 0
Si-1-Si >=-1
In this way, three weights of the edges are constructed, and the shortest path is naturally no problem.
However, it is important to note that because the check-and-control system often has a negative right side, so in order to avoid negative power loops, often with bellman-ford or SPFA solution (there is a negative power loop, the shortest circuit does not exist).
Ps:
Because we are asking for [ai,bi] intervals, we need to add the edges (U-1, V, W)!
Initialize the distance dis to negative infinity, if (Dis[v] < Dis[u] + W)!
The code is as follows:
#include <cstdio> #include <cstring> #include <stack> #include <iostream> #include < algorithm>using namespace std; #define INF 0x3f3f3f3f#define n 50017#define M 50017int N, M, K;int Edgehead[n], dis[n];s truct edge{int v,w,next;} Edge[3*m];bool vis[n];//int cont[n];int Minn, maxx;int MIN (int a, int b) {if (a < b) return A; return b;} int MAX (int a, int b) {if (a > B) return A; return b;} void Addedge (int u, int v, int w) {edge[k].next = Edgehead[u]; EDGE[K].W = W; EDGE[K].V = v; Edgehead[u] = k++;} int SPFA (int start)//stack{int sta[n]; int top = 0; for (int i = 1; I <= n; i++) dis[i] =-inf; Dis[start] = 0; ++cont[start]; memset (vis,false,sizeof (VIS)); Sta[++top] = start; Vis[start] = true; while (top) {int u = sta[top--]; Vis[u] = false; for (int i = edgehead[u]; i =-1; i = edge[i].next)//Note {int v = EDGE[I].V; int w = EDGE[I].W; if (Dis[v] < Dis[u] + W) {dis[v] = dis[u]+w; if (!vis[v])//Prevent the occurrence of the ring {sta[++top] = V; VIS[V] = true; }//if (++cont[v] > N)//negative ring//return-1; }}} return Dis[maxx];} int main () {int u, V, W; while (~SCANF ("%d", &n))//n is the destination {k = 1; memset (edgehead,-1,sizeof (Edgehead)); Minn = INF; Maxx =-1; for (int i = 1; I <= n; i++) {scanf ("%d%d%d", &u,&v,&w); Addedge (U-1,V,W); Maxx = MAX (V,maxx); Minn = MIN (U-1,minn); } for (int i = minn; I <= Maxx; i++)//new Edge, ensure that the connectivity of the graph must also add the edge {i,i+1 (Addedge) of each adjacent two integer points i,i+1,0; Addedge (i+1,i,-1); } int ans = SPFA (minn);//starting from point Minn to find the shortest way printf ("%d\n", ans); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 1201 Intervals (differential constrained +SPFA to find the longest path)