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

反向建图+SPFA可以AC0.0

Posted by mengxiang000 at 2016-06-01 19:30:42 on Problem 3249
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int head[200005];
struct EdgeNode
{
    int to;
    int w;
    int next;
}e[200005*20];
int degree[200005];
int val[200005];
int vis[200005];
int dis[200005];
int n,m;
int cont;
void add(int from,int to)
{
    e[cont].to=to;
    e[cont].next=head[from];
    head[from]=cont++;
}
void SPFA()
{
    queue<int >s;
    for(int i=0;i<n;i++)dis[i]=-0x3f3f3f3f;
    for(int i=0;i<n;i++)
    {
        if(degree[i]==0)
        {
            dis[i]=val[i];
            vis[i]=1;
            s.push(i);
        }
    }
    while(!s.empty())
    {
        int u=s.front();
        s.pop();vis[u]=0;
        for(int k=head[u];k!=-1;k=e[k].next)
        {
            int v=e[k].to;
            if(dis[v]<dis[u]+val[v])
            {
                dis[v]=dis[u]+val[v];
                if(vis[v]==0)
                {
                    vis[v]=1;
                    s.push(v);
                }
            }
        }
    }
    int output=-0x3f3f3f3f;
    for(int i=0;i<n;i++)
    {
        if(head[i]==-1)
        output=max(output,dis[i]);
    }
    printf("%d\n",output);
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        cont=0;
        memset(vis,0,sizeof(vis));
        memset(degree,0,sizeof(degree));
        memset(head,-1,sizeof(head));
        for(int i=0;i<n;i++)
        {
            scanf("%d",&val[i]);
        }
        for(int i=0;i<m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            x--;y--;
            add(y,x);
            degree[x]++;
        }
        SPFA();
    }
}

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