杂务

P1113 杂务

这道题我爆0啦,有点dp的小味道,显然,一个工程的完成时间由最慢的人决定(可以同时进行),m[i]代表完成杂物i所需的最少时间,m[i]=max(m[i],m[j]+v[i]),j是需要在i之前完成的杂物,最慢的也要完成,所以要取最大。虽然题目中说准备工作只可能在杂务1..k-1中,但这并不代表着在完成n之前,需要把1~n-1都要完成,所以要扫一遍。

 

#include<bits/stdc++.h>
using namespace std;
queue<int>q;
int n;
int in[100010];
bool b[100010];
int v[100010];
int m[100010];
int ans;
struct node
{
    int n;
    node *next;
}*e[100010];
int main()
{
    cin>>n;
    node *p;
    int x;
    for(int i=1;i<=n;i++)
    {
        p=new node();
        int t;
        cin>>p->n;
        t=p->n;
        cin>>v[p->n];
        cin>>x;
        while(x!=0)
        {
        in[p->n]+=1;
        if(e[x]==NULL)
        e[x]=p;
        else
        {
            p->next=e[x]->next;
            e[x]->next=p;
        }
        cin>>x;
        p=new node();
        p->n=t;
        }
    }
    for(int i=1;i<=n;i++)
    if(in[i]==0)
    {
    q.push(i);
    m[i]=v[i];    
    }
    while(!q.empty())
    {
        node *p;
        p=e[q.front()];
      while(p!=NULL)
     {
        in[p->n]-=1;
        if(in[p->n]==0)
        {
            q.push(p->n);
        }
        m[p->n]=max(m[p->n],m[q.front()]+v[p->n]);
        p=p->next;
     }  
     q.pop();
    }
    for(int i=1;i<=n;i++)
    ans=max(ans,m[i]);
    cout<<m[n];
    return 0;
}

 

posted @ 2017-08-23 14:47  WeiAR  阅读(213)  评论(0编辑  收藏  举报