//题意:   给出一个有向无环图,每个顶点都有一个权值。

//         求一条从入度为0的顶点到出度为0的顶点的一条路径,

//         路径上所有顶点权值和最大。

//我觉得只要明白

//图论里的链式前向星   的  建图原理  和  拓扑排序的一点知识就完全有能力打出来

//以后还是可以练练手的  所以写一发吧



//拓扑排序+优化一下

//author keyboarder

//time   2016/4/23 21:52

#include <iostream>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <algorithm>

#include <queue>

#include <math.h>

#include <queue>

#include <stack>

using namespace std;

#define INF 0x3f3f3f

#define pi acos(-1.0)

#define LL long long

#define mm 1000000007

#define N 1000010

struct asd{

    int to;

    int next;

};

asd q[N];

int head[N],tol;

int pre[N];

int out[N];

int val[N];

int dp[N];

int n,m;

void add(int a,int b)

{

    q[tol].to=b;

    q[tol].next=head[a];

    head[a]=tol++;

}

void tuopu()

{

    queue<int>e;

    while(!e.empty())

        e.pop();

    for(int i=1;i<=n;i++)

    {

        if(pre[i]==0)

        {

            pre[i]=-1;

            e.push(i);

        }

    }

    while(!e.empty())

    {

        int u=e.front();

        e.pop();

        for(int v=head[u];v!=-1;v=q[v].next)

        {

            int i=q[v].to;

            dp[i]=max(dp[u]+val[i],dp[i]);

            pre[i]--;

            if(pre[i]==0)

            {

                pre[i]=-1;

                e.push(i);

            }

        }

    }

}

int main()

{

    while(~scanf("%d%d",&n,&m))

    {

        for(int i=1;i<=n;i++)

        {

            scanf("%d",&val[i]);

        }

        int u,v;

        tol=0;

        memset(head,-1,sizeof(head));

        memset(pre,0,sizeof(pre));

        memset(out,0,sizeof(out));

        for(int i=0;i<m;i++)

        {

            scanf("%d%d",&u,&v);

            add(u,v);

            pre[v]++;

            out[u]++;

        }

        for(int i=1;i<=n;i++)

        {

            if(pre[i]==0)

            {

                dp[i]=val[i];

            }

            else

                dp[i]=-INF;

        }

        tuopu();

        int ans=-INF;

        for(int i=1;i<=n;i++)

        {

            if(out[i]==0)

                ans=max(ans,dp[i]);

        }

        printf("%d\n",ans);

    }

    return 0;

}


poj3249【拓扑排序】的更多相关文章

  1. poj3249 拓扑排序+DP

    题意:给出一个有向无环图,每个顶点都有一个权值.求一条从入度为0的顶点到出度为0的顶点的一条路径,路径上所有顶点权值和最大. 思路:因为是无环图,则对于每个点经过的路径求其最大权值有,dp[i]=ma ...

  2. POJ3249 Test for Job(拓扑排序+dp)

    Test for Job Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 10137   Accepted: 2348 Des ...

  3. 算法与数据结构(七) AOV网的拓扑排序

    今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...

  4. 有向无环图的应用—AOV网 和 拓扑排序

    有向无环图:无环的有向图,简称 DAG (Directed Acycline Graph) 图. 一个有向图的生成树是一个有向树,一个非连通有向图的若干强连通分量生成若干有向树,这些有向数形成生成森林 ...

  5. 【BZOJ-2938】病毒 Trie图 + 拓扑排序

    2938: [Poi2000]病毒 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 609  Solved: 318[Submit][Status][Di ...

  6. BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)

    题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...

  7. 图——拓扑排序(uva10305)

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  8. Java排序算法——拓扑排序

    package graph; import java.util.LinkedList; import java.util.Queue; import thinkinjava.net.mindview. ...

  9. poj 3687(拓扑排序)

    http://poj.org/problem?id=3687 题意:有一些球他们都有各自的重量,而且每个球的重量都不相同,现在,要给这些球贴标签.如果这些球没有限定条件说是哪个比哪个轻的话,那么默认的 ...

随机推荐

  1. 在windows Server 2008 R2server上使用infopath不能将表单公布到sharepoint server的问题处理。

    在server 2008 R2 中.你将做好的表单公布到Sharepoint 时候会报错: 这个情况在client是2008 R2 Server 就会出现这个结果. 在角色中启用桌面体验就可以

  2. Eclipse打包Android项目时用到proguard.cfg后,出现的Warning:can&#39;t find referenced class问题的解决方式

    Warning: can't find superclass or interface Warning: can't find referenced class 这两个问题的解决方法: 1.要把你项目 ...

  3. 在linux中安装.sh 文件

    有一些linux的安装包是.sh后缀的安装包,可以通过$bash xxx.sh的命令来进行安装.

  4. [转载]saltstack master配置文件中文翻译

    原文出处:http://blog.coocla.org/301.html Salt系统的配置是令人惊讶的简单,对于salt的两个系统都有各自的配置文件,salt-master是通过一个名为master ...

  5. Struts2+Spring+Hibernate step by step 04 整合Spring之二,从数据库验证username和password

    注:本系列文章部分内容来自王健老师编写ssh整合开发教程 使用Spring的AOP进行项目的事务管理,已经成为非常多企业的首先,Spring做为优秀的开源项目,其在数据库连接.事务管理方面的优势已经显 ...

  6. Centos6.5 安装 Oracle11gR2(64位)

    Centos6.5安装 Oracle11gR2(64位) 安装centos6.5 (我的是虚拟机环境) 1.  下载centos6.5的安装包,不解释. 例如以下图: 2.  下载oracle安装包, ...

  7. 浅谈JavaScript的事件(事件对象)

    在触发DOM上的某个事件时,会产生一个事件对象event,这个对象中包含这所有与事件有关的信息.包括导致事件的元素.事件的类型和事件的相关信息.例如鼠标操作的事件中,会包含鼠标的位置信息.而键盘触发的 ...

  8. Maven group, artifact or version defined in the pom file do not match the file ...

    在把library上传到bintray空间的时候报以下错误 Could not upload to 'https://api.bintray.com/content/ping/maven/comm-a ...

  9. 【翻译自mos文章】即使resource_limit = false, password的 资源限制也会生效

    即使resource_limit = false, password的 资源限制也会生效 參考原文: Resource limits for passwords work even with reso ...

  10. swift的String处理

    import UIKit import CoreText class ViewController: UIViewController { override func viewDidLoad() { ...