您的位置:首页 > 其它

bzoj 3389 [Usaco2004 Dec]Cleaning Shifts安排值班

2016-08-10 15:46 639 查看
Description

一天有T(1≤T≤10^6)个时段.约翰正打算安排他的N(1≤N≤25000)只奶牛来值班,打扫打扫牛棚卫生.每只奶牛都有自己的空闲时间段Si,Ei,只能把空闲的奶牛安排出来值班.而且,每个时间段必需有奶牛在值班. 那么,最少需要动用多少奶牛参与值班呢?如果没有办法安排出合理的方案,就输出-1.

Input

第1行:N,T.
第2到N+1行:Si,Ei.


Output

最少安排的奶牛数.


Sample Input

3 10

1 7

3 6

6 10

Sample Output

2

样例说明

奶牛1和奶牛3参与值班即可.


这题可以抽象成最短路,从0到T。连边的方式可以自己先琢磨琢磨,然后跑一遍堆优化的dij就可以了。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;
int n,T,x,y,t,w,ty,tot;
int head[1000005],Next[1030005],to[1030005],len[1030005];
int pos[1000005],heap[1000005],dis[1000005];
void add(int x,int y,int z)
{
tot++;
Next[tot]=head[x];
to[tot]=y;
len[tot]=z;
head[x]=tot;
}
void up(int id)
{
while(id/2>=1)
{
int x=heap[id];
int y=heap[id/2];
if(dis[x]<dis[y])
{
swap(heap[id],heap[id/2]);
swap(pos[x],pos[y]);
id=id/2;
}
else break;
}
}
void down(int id)
{
int j;
while(id*2<=ty)
{
int x=heap[id*2];
int y=heap[id*2+1];
if(id*2+1>ty||dis[x]<dis[y]) j=id*2; else j=id*2+1;
x=heap[id];
y=heap[j];
if(dis[x]>dis[y])
{
swap(heap[id],heap[j]);
swap(pos[x],pos[y]);
id=j;
}
else break;
}
}
void dij()
{
for(int i=1;i<=T;i++) dis[i]=1e9;
dis[0]=0;
ty=1;
heap[1]=0;
pos[0]=1;
while(ty>0)
{
int y=heap[1];
pos[y]=0;
swap(heap[1],heap[ty]);
swap(pos[heap[1]],pos[heap[ty]]);
ty--;
down(1);
for(int i=head[y];i!=-1;i=Next[i])
if(dis[y]+len[i]<dis[to[i]])
{
dis[to[i]]=dis[y]+len[i];
if(pos[to[i]]==0)
{
ty++;
heap[ty]=to[i];
pos[to[i]]=ty;
}
up(pos[to[i]]);
}
}
}
int main()
{
cin>>n>>T;
for(int i=0;i<=T;i++) head[i]=-1;
for(int i=1;i<=T;i++) add(i,i-1,0);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&x,&y);
add(x-1,y,1);
}
dij();
if(dis[T]==1e9) cout<<"-1"; else cout<<dis[T];
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: