3389: [Usaco2004 dec]cleaning shifts schedule duty time limit:1 Sec Memory limit:128 MB
submit:218 solved:86
[Submit] [Status] [Discuss] 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. HINT Source
Silver
Exercises
Shortest circuit
Orz Hzwer the shortest circuit. Good God!!!
For each cow guard interval [l,r], we go from L-1 to R to the edge of the edge of 1, and then from each I (1<=i<=t) to i-1 the edge of the edge 0 (that is, you can run in reverse), the shortest way to run.
1#include <bits/stdc++.h>2 using namespacestd;3 #defineMAXN 10000104 #defineINF 1e95 structnode6 {7 intBegin,end,value,next;8}edge[2*MAXN];9 intcnt,head[maxn],t,e,dis[maxn],heap[maxn],pos[maxn],size;Ten voidAddedge (intBbintEeintVV) One { AEDGE[++CNT].BEGIN=BB;EDGE[CNT].END=EE;EDGE[CNT].VALUE=VV;EDGE[CNT].NEXT=HEAD[BB]; head[bb]=CNT; - } - intRead () the { - ints=0, fh=1;CharCh=GetChar (); - while(ch<'0'|| Ch>'9'){if(ch=='-') fh=-1; ch=GetChar ();} - while(ch>='0'&&ch<='9') {s=s*Ten+ (ch-'0'); ch=GetChar ();} + returns*fh; - } + voidPUSH1 (intk) A { at intnow=K,root; - while(now>1) - { -root=now/2; - if(Dis[heap[root]]<=dis[heap[now]])return; - swap (Heap[root],heap[now]); in swap (Pos[heap[root]],pos[heap[now]]); -now=Root; to } + } - voidInsert (intk) the { *heap[++size]=k;pos[k]=SIZE; PUSH1 (SIZE); $ }Panax Notoginseng voidPOP1 (intk) - { the intnow,root=K; +pos[heap[k]]=0; heap[k]=heap[size--];if(size>0) pos[heap[k]]=K; A while(root<=size/2) the { +now=root*2; - if(now<size&&dis[heap[now+1]]<dis[heap[now]]) now++; $ if(Dis[heap[root]]<=dis[heap[now]])return; $ swap (Heap[root],heap[now]); - swap (Pos[heap[root]],pos[heap[now]]); -root=Now ; the } - }Wuyi voidDijkstraintstart) the { - inti,u,v; Wu for(i=0; i<=t;i++) dis[i]=inf;dis[start]=0; - for(i=0; i<=t;i++) Insert (i); About while(size>0) $ { -u=heap[1]; POP1 (Pos[u]); - for(i=head[u];i!=-1; i=edge[i].next) - { Av=Edge[i].end; + if(Dis[v]>dis[u]+edge[i].value) {dis[v]=dis[u]+Edge[i].value; PUSH1 (Pos[v]);} the } - } $ } the intMain () the { the intI,bb,ee,vv,n; theN=read (); T=read ();//t++; e++; -memset (head,-1,sizeof(Head)); Cnt=1; in for(i=1; i<=t;i++) Addedge (i,i-1,0); the for(i=1; i<=n;i++) the { AboutBb=read (); ee=read (); the if(bb>ee) swap (bb,ee); theAddedge (bb-1Ee1); the } +Dijkstra0); - if(Dis[t]==inf) printf ("-1"); the Elseprintf"%d", Dis[t]);Bayi return 0; the}
Bzoj 3389: [Usaco2004 dec]cleaning shifts arrange duty shortest way, divine question