Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

求大神看看 DAG错哪里了

Posted by 1164996391 at 2016-08-23 15:28:07 on Problem 3249
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <climits>

using namespace std;

int n,m,a[100001],d[100001];
bool cc[100001];
vector<int> b[100001];

int solve(int i)
{
    if(d[i] != INT_MIN)
        return d[i];
    d[i] = a[i];
    for(int j=0;j<b[i].size();++j)
        d[i] = max(d[i],a[i]+solve(b[i][j]));
    return d[i];
}

int main()
{
    while(scanf("%d %d",&n,&m) != EOF)
    {
        for(int i=1;i<=n;++i)
            scanf("%d",&a[i]);
        a[0] = 0;
        int x,y;
        for(int i=0;i<=n;++i)
        {
            b[i].clear();
            d[i] = INT_MIN;
            cc[i] = false;
        }
        for(int i=1;i<=m;++i)
        {
            scanf("%d %d",&x,&y);
            b[x].push_back(y);
            cc[y] = true;
        }
        for(int i=1;i<=n;++i)
            if(b[i].size()!=0&&!cc[i])
                b[0].push_back(i);
        printf("%d\n",solve(0));
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator