Description have T (1≤t≤10^6) time of day. John is going to arrange for his N (1≤n≤25000) cows to come on duty and clean the barn. Each cow has its own free time period [Si,ei] (1≤si≤ei≤t), only the idle cows can be arranged out on duty. Also, a cow must be on duty every time period. So how many cows do you need to take on duty at least? If there is no way to arrange a reasonable plan, then output-1. Input line 1th: N,t. 2nd to N+1 Line: Si,ei. Output minimum number of cows scheduled. Sample Input
3 10
1 7
3 6
6 10
Sample Output
2
Sample Description
Cows 1 and cows 3 participate on duty can.
"In fact, this is the classic interval greedy problem, but the shortest possible to do."
A point is built at each point in time, and I point to I-1 points with a weighted value of 0 of the forward edge
For the interval [L, R], the l−1 to r with a weighted value of 1 with a forward edge. Run from 0 times the most
Short-circuit can. "
--The above is the original of a courseware
And then put the code
1#include <stdio.h>2#include <queue>3#include <string.h>4#include <algorithm>5 #defineINF 20000000006 using namespacestd;7typedef pair<Long Long,int>PII;8 structnode9 {Ten intu, V, W, next; One}a[5000005]; A inttot, N, M, Rxa, Rxc, RYA, RYC; - intdis[1000005]; - intRP, T, first[1000005]; the intdone[1000005]; - voidAddedge (intStintEndintval) - { -a[++tot].u = ST;A[TOT].V = END;A[TOT].W =Val; +A[tot].next =first[st];first[st] =tot; - } + intDij () A { atPriority_queue <pii, Vector<pii>, Greater<pii > >Q; - for(inti =0; I <= T; i++) Dis[i] =inf; -Q.push (Make_pair (0,0)); -dis[0] =0; - while(!q.empty ()) - { in intU =q.top (). Second;q.pop (); - if(Done[u])Continue; toDone[u] =1; + for(inte = First[u]; E! =-1; E =a[e].next) - { the intv =a[e].v; * if(Dis[u] + A[E].W <Dis[v]) $ {Panax NotoginsengDIS[V] = Dis[u] +A[E].W; - Q.push (Make_pair (dis[v], v)); the } + } A } the } + intMain () - { $scanf"%d%d", &n, &T); $ intx, y; -memset (First,-1,sizeof(first)); - for(inti =1; I <= T; i++) theAddedge (i, I-1,0); - for(inti =1; I <= N; i++)Wuyi { thescanf"%d%d", &x, &y); -Addedge (x1Y1); Wu } - Dij (); About if(Dis[t] = = inf) printf ("-1\n"); $ Elseprintf"%d\n", Dis[t]); - return 0; -}
bzoj3389: [Usaco2004 dec]cleaning shifts schedule duty