洛谷 P3074 [USACO13FEB]牛奶调度Milk Scheduling
题目大意:给i奶牛挤奶前必须给
XX奶牛挤奶,每头牛挤奶需要时间t[i].
求最小时间
题解:拓扑+最长路
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> #include<cstdio> #include<cstring> #define N 10009 using namespace std; int n,m,h,t,ans,sumedge; int ti[N],rd[N],cd[N],q[N],c[N],head[N]; struct Edge{ int x,y,nxt; Edge(int x=0,int y=0,int nxt=0): x(x),y(y),nxt(nxt){} }edge[N*5]; void add(int x,int y){ edge[++sumedge]=Edge(x,y,head[x]); head[x]=sumedge; } int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++)scanf("%d",&ti[i]); for(int i=1;i<=m;i++){ int x,y; scanf("%d%d",&x,&y); add(x,y);cd[x]++;rd[y]++; } h=1;t=0; for(int i=1;i<=n;i++)if(!rd[i])c[i]=ti[i],q[++t]=i; for(;h<=t;h++){ int now=q[h]; for(int i=head[now];i;i=edge[i].nxt){ int v=edge[i].y; c[v]=max(c[v],ti[v]+c[now]); rd[v]--; if(rd[v]==0)q[++t]=v; } } for(int i=1;i<=n;i++){ if(cd[i]==0)ans=max(ans,c[i]); } cout<<ans<<endl; return 0; }