[POJ3171] Cleaning Shifts

题意:FJ有一段时间[S,T]需要奶牛们打扫卫生,他有N头牛,每头牛可以在[si,ti]打扫卫生,但需要付vi元钱,求[S,T]每时每刻都有牛打扫卫生的最小代价

题解:

spfa(最短路转化)

[si,ti]打扫卫生相当于从si连一条边到ti+1,连完所有的牛之后,还要添加回溯路径,最后一遍spfa即可

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define ll long long
using namespace std;

const int N = 100010;

int n,s,t,e_num,inf;
int nxt[N*2],to[N*2],w[N*2],h[N],dis[N];
bool in[N];

queue<int> q;

void add(int x, int y, int z) {
  nxt[++e_num]=h[x],to[e_num]=y,w[e_num]=z,h[x]=e_num;
}

void spfa() {
  memset(dis,63,sizeof(dis));
  inf=dis[0],dis[s]=0,in[s]=1,q.push(s);
  while(!q.empty()) {
    int u=q.front();
    in[u]=0,q.pop();
    for(int i=h[u]; i; i=nxt[i]) {
      int v=to[i];
      if(dis[u]+w[i]<dis[v]) {
	dis[v]=dis[u]+w[i];
	if(!in[v]) in[v]=1,q.push(v);
      }
    }
  }
}

int gi() {
  int x=0,o=1; char ch=getchar();
  while(ch!=‘-‘ && (ch<‘0‘ || ch>‘9‘)) ch=getchar();
  if(ch==‘-‘) o=-1,ch=getchar();
  while(ch>=‘0‘ && ch<=‘9‘) x=x*10+ch-‘0‘,ch=getchar();
  return o*x;
}

int main() {
  n=gi(),s=gi(),t=gi();
  for(int i=1; i<=n; i++) {
    int x=gi(),y=gi(),z=gi();
    if(y<s || x>t) continue;
    if(x<s) x=s;
    if(y>t) y=t;
    add(x,y+1,z);
  }
  for(int i=s+1; i<=t+1; i++) add(i,i-1,0);
  spfa();
  if(dis[t+1]==inf) puts("-1");
  else printf("%d", dis[t+1]);
  return 0;
}
时间: 2024-11-08 21:49:40

[POJ3171] Cleaning Shifts的相关文章

POJ3171 Cleaning Shifts DP,区间覆盖最值

题目大意,N个区间覆盖[T1,T2]及对应的代价S,求从区间M到E的全部覆盖的最小代价是多少. (1 <= N <= 10,000),(0 <= M <= E <= 86,399). 思路是DP,首先将每个区间按照T2从小到大排序,设dp(k)为从m覆盖到k所需最小代价,则有 dp(T2[i]) = min(dp(T2[i]), {dp(j) + Si,  T1[i] - 1<=j <= T2[i]}),对于 {dp(j) + Si,  T1[i] - 1<

POJ3171——Cleaning Shifts

Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2813   Accepted: 976 Description Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. They now require their barn to be immaculate. Farmer J

poj3171 Cleaning Shifts【线段树(单点修改区间查询)】【DP】

Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4422   Accepted: 1482 Description Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. They now require their barn to be immaculate. Farmer

poj2376 Cleaning Shifts【线段树】【DP】

Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32561   Accepted: 7972 Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one co

Cleaning Shifts(POJ 2376 贪心)

Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15143   Accepted: 3875 Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one co

BZOJ 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚

题目 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚 Time Limit: 5 Sec  Memory Limit: 64 MB Description Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. They now require their barn to be immaculate. Farmer John, the most

POJ 2376 Cleaning shifts 贪心 基础题

Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13042   Accepted: 3373 Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one co

[POJ 2376] Cleaning Shifts

Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12874   Accepted: 3331 Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one co

[BZOJ1672][Usaco2005 Dec]Cleaning Shifts 清理牛棚

1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 953  Solved: 407 [Submit][Status][Discuss] Description Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. They now require the