POJ 1949 Chores

水题:拓扑排序

View Code
#include <stdio.h>
#include <memory.h>

#define N 10002
#define M 1000000
#define MOV(x) (x=(x+1)%N)

int needtime[N],finaltime[N],indegree[N];
int queue[N],font,rear;
int nodevp[N];
int nodeu[M],next[M],ind;

void addedge(int v,int u)
{
nodeu[ind]=u;
next[ind]=nodevp[v];
nodevp[v]=ind++;
}

void solve()
{
int n,i,j,v,u;

scanf("%d",&n);
memset(nodevp,-1,sizeof(nodevp));
for(i=1;i<=n;i++)
{
scanf("%d %d",needtime+i,indegree+i);
if(indegree[i]!=0)
{
for(j=0;j<indegree[i];j++)
{
scanf("%d",&v);
addedge(v,i);
}
}
else queue[MOV(rear)]=i;
}

while(font!=rear)
{
v=queue[MOV(font)];
for(i=nodevp[v];~i;i=next[i])
{
u=nodeu[i];
if(--indegree[u] == 0) queue[MOV(rear)]=u;
if(finaltime[v]+needtime[v]>finaltime[u]) finaltime[u]=finaltime[v]+needtime[v];
}
}

for(v=0,i=1;i<=n;i++)
{
finaltime[i]+=needtime[i];
if(finaltime[v]<finaltime[i]) v=i;
}

printf("%d\n",finaltime[v]);
}

int main()
{
// freopen("input.txt","r",stdin);

solve();

return 0;
}



你可能感兴趣的:(poj)