bzoj1672[Usaco2005 Dec]Cleaning Shifts 清理牛棚*

bzoj1672[Usaco2005 Dec]Cleaning Shifts 清理牛棚

题意:

n头奶牛,第i头愿意在时刻si到ti打扫牛棚,费用为ci,求打扫S到T时刻的最小费用。n≤10000,时刻≤90000。

题解:

最短路,si和ti+1连边,长度为ci,以及所有时刻ai和ai-1连边,长度为0,以保证覆盖的情况被处理。然后求S到T的最短路。好像大部分的神犇用的是线段树。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <queue>
 5 #define inc(i,j,k) for(int i=j;i<=k;i++)
 6 #define maxn 100010
 7 #define INF 10000000000000000
 8 #define ll long long
 9 using namespace std;
10 
11 inline int read(){
12     char ch=getchar(); int f=1,x=0;
13     while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
14     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
15     return f*x;
16 }
17 struct e{int t,w,n;}es[maxn]; int g[maxn],ess,n,be,en; bool inq[maxn]; queue<int>q; ll d[maxn];
18 void pe(int f,int t,int w){es[++ess]=(e){t,w,g[f]}; g[f]=ess;}
19 void spfa(int s){
20     while(!q.empty())q.pop(); memset(inq,0,sizeof(inq)); inc(i,be,en)d[i]=INF;
21     q.push(s); inq[s]=1; d[s]=0;
22     while(!q.empty()){
23         int x=q.front(); q.pop(); inq[x]=0;
24         for(int i=g[x];i;i=es[i].n)if(d[es[i].t]>d[x]+es[i].w){
25             d[es[i].t]=d[x]+es[i].w;
26             if(!inq[es[i].t])q.push(es[i].t),inq[es[i].t]=1;
27         }
28     }
29 }
30 int main(){
31     n=read(); be=read(); en=read()+1; inc(i,1,n){int a=read(),b=read()+1,c=read(); pe(a,b,c);}
32     inc(i,be,en)pe(i+1,i,0); spfa(be); printf("%lld",d[en]==INF?-1:d[en]); return 0;
33 }

20160923

原文地址:https://www.cnblogs.com/YuanZiming/p/5906118.html