P1113 chores (topological sort)

Meaning of the questions:

There are n tasks, each has its required completion time, and its predecessors have, to complete all tasks asked how long (not directly related to the task can be started at the same time)

Ideas:

Write your own a little bit more complicated

First built in map, for a mission to all of its predecessors even a directed edge (from the front even to themselves), and read into the record a point

Before traversing again, the 0-degree point is added to the queue, which all adjacent edge points into a reduced degree, if the degree of the 0-point, the addition of the queue

More crucial point is how to update dp [nex] = max (dp [nex], dp [i] + tim [nex])

My writing is more complex, with more than one queue save time, with a DP will be able to directly address the array

#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#define inf 0x3f3f3f3f
 using namespace std;
 const int maxn=1e5+10;
 queue<int> a,b;
 vector<int> edge[maxn];
 int in[maxn],tim[maxn],ans,n,mx[maxn];
 void solve()
 {
     for(int i=1;i<=n;i++){
         if(!in[i]){
             a.push(i),b.push(tim[i]);
         }
     }
     while(!a.empty()){
         int x=a.front(),y=b.front();
         a.pop(),b.pop();
         ans=max(ans,y);
         for(int i=0;i<edge[x].size();i++){
             in[edge[x][i]]--;
             mx[edge[x][i]]=max(y,mx[edge[x][i]]);
             if(!in[edge[x][i]]){
                 a.push(edge[x][i]);
                 b.push(tim[edge[x][i]]+mx[edge[x][i]]);
             }
         }
     }
 }
 int main()
 {
     int x,y,temp;
     scanf("%d",&n);
     for(int i=1;i<=n;i++){
         scanf("%d%d",&x,&y);
         tim[x]=y;
         while(scanf("%d",&temp)&&temp){
             edge[temp].push_back(x);
             in[x]++;
         }
     }
     memset(mx,-inf,sizeof(mx));
     ans=-inf;
     solve();
     cout<<ans<<endl;
 }

 

Guess you like

Origin www.cnblogs.com/overrate-wsj/p/12309442.html