Cleaning Shifts bzoj-3389 Usaco-2004Dec

题目大意:每天有n个时间段,每个时间段都必须安排一个奶牛值班。有m个奶牛,每个奶牛只有一个空闲时间s[i]~e[i],求至少动用多少奶牛。

注释:$1\le n\le 10^6$,$1\le m\le 25,000$。

想法:神题

我们将所有的时间点排成一排,然后对每一个i+1向i连一条无代价的边。

对于每一个s[i]向其对应的e[i]连边,代价为1.

然后求1到n的最短路即可

这样建图的道理:首先,从后面的时间点向前面的时间点连边是没有任何问题的。这就相当于我已经管理到了x时间段,我雇佣一个开始于x之前的奶牛,显然是可行且无代价的。

那么从s[i]向e[i]连边,就是说我雇佣这头奶牛,此时我已经管理到了e[i]这个时间点。

最后,附上丑陋的代码... ...

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define M 1000010
#define N 30010
using namespace std;
int dis[M];
bool v[M];
struct cmp
{
bool operator()(int x,int y)
{
return dis[x]>dis[y];
}
};
priority_queue<int,vector<int>,cmp>q;
int head[M],to[N+M],nxt[N+M],val[N+M],tot;
inline void add(int x,int y,int z)
{
to[++tot]=y;
val[tot]=z;
nxt[tot]=head[x];
head[x]=tot;
}
inline void original()
{
memset(dis,0x3f,sizeof dis);
}
int main()
{
int n,m;
cin >> n >> m ;
for(int i=1;i<=m;i++) add(i,i-1,0);
original();
for(int x,y,i=1;i<=n;i++)
{
scanf("%d%d",&x,&y);
add(x-1,y,1);
}
dis[0]=0;
q.push(0);
while(!q.empty())
{
int x=q.top();q.pop();
if(v[x]) continue;
for(int i=head[x];i;i=nxt[i])
{
if(v[to[i]]) continue;
if(dis[to[i]]>dis[x]+val[i])
{
dis[to[i]]=dis[x]+val[i];
q.push(to[i]);
}
}
}
if(dis[m]==0x3f3f3f3f) printf("-1\n");
else printf("%d\n",dis[m]);
return 0;
}

小结:好题,感觉自己图论菜的一匹... ...

[bzoj3389][Usaco2004Dec]Cleaning Shifts安排值班_最短路的更多相关文章

  1. BZOJ3389: [Usaco2004 Dec]Cleaning Shifts安排值班

    3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 45  Solved:  ...

  2. Bzoj 3389: [Usaco2004 Dec]Cleaning Shifts安排值班 最短路,神题

    3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 218  Solved: ...

  3. BZOJ 3389: [Usaco2004 Dec]Cleaning Shifts安排值班

    题目 3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec  Memory Limit: 128 MB Description      ...

  4. 3389: [Usaco2004 Dec]Cleaning Shifts安排值班

    3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 102  Solved: ...

  5. bzoj 3389: [Usaco2004 Dec]Cleaning Shifts安排值班 -- 贪心

    3389: [Usaco2004 Dec]Cleaning Shifts安排值班 Time Limit: 1 Sec  Memory Limit: 128 MB Description     一天有 ...

  6. bzoj3389:[Usaco2004 Dec]Cleaning Shifts安排值班

    思路:可以贪心,也可以最短路. 贪心写法:因为在保证合法的前提下,我们选择的区间一定要右端点尽量靠后才行,于是我们每次就选择一个合法的并且右端点最靠后的区间就好了(如果没有合法的输出-1即可).时间复 ...

  7. 【BZOJ】3389: [Usaco2004 Dec]Cleaning Shifts安排值班(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3389 显然左端点排序后,依次取. 要考虑下一次取的方案: 待选点为a[j].x<=a[now] ...

  8. BZOJ_1672_[Usaco2005 Dec]Cleaning Shifts 清理牛棚_动态规划+线段树

    BZOJ_1672_[Usaco2005 Dec]Cleaning Shifts 清理牛棚_动态规划+线段树 题意:  约翰的奶牛们从小娇生惯养,她们无法容忍牛棚里的任何脏东西.约翰发现,如果要使这群 ...

  9. poj 2376 Cleaning Shifts 最小区间覆盖

    Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40751   Accepted: 9871 ...

随机推荐

  1. hdu 2063 (二分匹配 匈牙利算法)

    过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  2. 最短路--Dijkstra&&Floyed&&SPFA

    最短路径是一个很常见的问题,这里有3种方法,可供参考. 一.Dijkstra#include<iostream> #include<cstdio> #include<cs ...

  3. 复习--二叉树&&树

    树是一种很常用的数据结构,日后的学习中会经常碰到运用树的知识. //构造二叉树#include<cstdio> #include<iostream> #include<a ...

  4. 排序系列 之 归并排序算法 —— Java实现

    基本思想: 归并排序法是分治法的典型实例,分为分割和归并两部分. 把一个数组分为大小相近的子数组(分割),分别把子数组排好序后,通过合成一个大的排好序的数组(归并). 实例: 先分割成每个子序列只有一 ...

  5. Oracle Instant Client 安装配置

    一.下载 下载地址:http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html 这是Ora ...

  6. Moon Http Server,强大如斯的全脚本web服务器

    Moon Http Server(MHS) 是一个使用Pascal脚本的高性能Web服务器. 昨天晚上是第一次接触.花了30分钟入门,非常强大.是Delphi 者开发Web的福音. 引用一下作者的介绍 ...

  7. java.util.concurrent常用类(CountDownLatch,Semaphore,CyclicBarrier,Future)

    CyclicBarrier CyclicBarrier是用来一个关卡来阻挡住所有线程,等所有线程全部执行到关卡处时,再统一执行下一步操作.假设一个场景:每个线程代表一个跑步运动员,当运动员都准备好后, ...

  8. windows服务安装错误 在‘安装’过程发生异常:System.ComponentModel.Win32Exception:系统正在关机

    今天安装windows服务的时候先是在本地安装测试通过,但是一到服务器就一直安装失败 在‘安装’过程发生异常:System.ComponentModel.Win32Exception:系统正在关机 然 ...

  9. 努比亚 Z17 mini s (Nubia NX589J) 解锁BootLoader 并刷入recovery ROOT

    首先下载好工具链接:链接:https://pan.baidu.com/s/1gher4T9 密码:rypn 备用下载链接:https://pan.baidu.com/s/1nxdzt9Z 本篇教程教你 ...

  10. MSP430之section(1)

    1 Intro The smallest unit of an object file is a section. A section is a block of code or data that ...