Test for Job
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 11230   Accepted: 2651

Description

Mr.Dog was fired by his company. In order to support his family, he must find a new job as soon as possible. Nowadays, It's hard to have a job, since there are swelling numbers of the unemployed. So some companies often use hard tests for their recruitment.

The test is like this: starting from a source-city, you may pass through some directed roads to reach another city. Each time you reach a city, you can earn some profit or pay some fee, Let this process continue until you reach a target-city. The boss will compute the expense you spent for your trip and the profit you have just obtained. Finally, he will decide whether you can be hired.

In order to get the job, Mr.Dog managed to obtain the knowledge of the net profit Vi of all cities he may reach (a negative Vi indicates that money is spent rather than gained) and the connection between cities. A city with no roads leading to it is a source-city and a city with no roads leading to other cities is a target-city. The mission of Mr.Dog is to start from a source-city and choose a route leading to a target-city through which he can get the maximum profit.

Input

The input file includes several test cases.
The first line of each test case contains 2 integers n and m(1 ≤ n ≤ 100000, 0 ≤ m ≤ 1000000) indicating the number of cities and roads.
The next n lines each contain a single integer. The ith line describes the net profit of the city i, Vi (0 ≤ |Vi| ≤ 20000)
The next m lines each contain two integers x, y indicating that there is a road leads from city x to city y. It is guaranteed that each road appears exactly once, and there is no way to return to a previous city.

Output

The output file contains one line for each test cases, in which contains an integer indicating the maximum profit Dog is able to obtain (or the minimum expenditure to spend)

Sample Input

6 5
1
2
2
3
3
4
1 2
1 3
2 4
3 4
5 6

Sample Output

7

Hint

 
注意是无环图从入度为0出发至出度为0
无环图知每个点只能用一次,然后用的时候显然是有顺序的,那么就可以根据入度来排序从而推进答案喽
 
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + ;
int n, m, tot;
int cost[N], in[N], out[N], head[N], dp[N];
bool vis[N];
struct node
{
 int to, nxt;
}e[N * ];
void add(int x, int y)
{
 e[tot].to = y;
 e[tot].nxt = head[x];
 head[x] = tot++;
}
void toposort()
{
 int cnt = ;
 while(cnt < n) {
  for(int i = ; i <= n; ++i)
   if(in[i] == && !vis[i]) {
    vis[i] = true;
    cnt++;
    for(int j = head[i]; j != -; j = e[j].nxt) {
     int x = e[j].to;
     in[x]--;
     if(dp[i] + cost[x] > dp[x]) dp[x] = dp[i] + cost[x];
    }
   }
 }
}
int main()
{
 while(scanf("%d%d", &n, &m) != EOF) {
  memset(in, , sizeof(in));
  memset(out, , sizeof(out));
  memset(head, -, sizeof(head));
  memset(vis, false, sizeof(vis));
  tot = ;
  for(int i = ; i <= n; ++i)
   scanf("%d", &cost[i]);
  for(int i = ; i <= m; ++i) {
   int x, y;
   scanf("%d%d", &x, &y);
   add(x, y);
   in[y]++;
   out[x]++;
  }
  for(int i = ; i <= n; ++i)
   if(in[i] == ) dp[i] = cost[i];
   else dp[i] = -INF;
  toposort();
  int ans = -INF;
  for(int i = ; i <= n; ++i) if(out[i] == && dp[i] > ans) ans = dp[i];
  printf("%d\n", ans);
 }
 return ;
}

poj3249 拓扑找最长路的更多相关文章

  1. bzoj1880: [Sdoi2009]Elaxia的路线(spfa,拓扑排序最长路)

    1880: [Sdoi2009]Elaxia的路线 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 1944  Solved: 759[Submit][St ...

  2. 2017 ACM-ICPC(乌鲁木齐赛区)网络赛 H.Skiing 拓扑排序+最长路

    H.Skiing In this winter holiday, Bob has a plan for skiing at the mountain resort. This ski resort h ...

  3. BZOJ 2019 [Usaco2009 Nov]找工作:spfa【最长路】【判正环】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2019 题意: 奶牛们没钱了,正在找工作.农夫约翰知道后,希望奶牛们四处转转,碰碰运气. 而 ...

  4. 2017 ACM-ICPC网络赛 H.Skiing 有向图最长路

    H.Skiing In this winter holiday, Bob has a plan for skiing at the mountain resort. This ski resort h ...

  5. The Largest Clique UVA - 11324( 强连通分量 + dp最长路)

    这题  我刚开始想的是  缩点后  求出入度和出度为0 的点  然后统计个数  用总个数 减去 然而 这样是不可以的  画个图就明白了... 如果  减去度为0的点  那么最后如果出现这样的情况是不可 ...

  6. BZOJ5450: 轰炸(水题,Tarjan缩点求最长路)

    5450: 轰炸 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 43  Solved:18[Submit][Status][Discuss] Desc ...

  7. [BZOJ1663] [Usaco2006 Open]赶集(spfa最长路)

    传送门 按照时间t排序 如果 t[i] + map[i][j] <= t[j],就在i和j之间连一条边 然后spfa找最长路 #include <queue> #include &l ...

  8. luogu 1113 杂务--啥?最长路?抱歉,我不会

    P1113 杂务 题目描述 John的农场在给奶牛挤奶前有很多杂务要完成,每一项杂务都需要一定的时间来完成它.比如:他们要将奶牛集合起来,将他们赶进牛棚,为奶牛清洗乳房以及一些其它工作.尽早将所有杂务 ...

  9. 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur (SCC缩点,SPFA最长路,枚举反边)

    P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 In an effort to better manage the grazing patterns of hi ...

随机推荐

  1. HyperLeger Fabric开发(三)——HyperLeger Fabric架构

    HyperLeger Fabric开发(三)--HyperLeger Fabric架构 一.HyperLeger Fabric逻辑架构 1.HyperLeger Fabric逻辑架构简介 Fabric ...

  2. SQL语句学习(一)

    这篇文章用来记录再学习SQL语句的过程. 首先,我们从简单的创建表开始.创建表的语法是CREATE TABLE 表名(列1的名字  列1的属性,列2的名字  列2的属性...); 如果希望将某一列作为 ...

  3. linux服务器(CentOS)一键安装express框架

    express框架需要nodejs环境支持,没有安装node.js环境的同学可以参照下面这篇博客 linux服务器安装配置Node.js 好了,言归正传.先使用xshell或者其它软件连接我们的服务器 ...

  4. 使用SWIG将C++接口转换成Java接口

    PS:此文章仅作为个人记录使用,代码属于私密,故无法公开: 以C++类classifier为例,文件保存于百度网盘 https://pan.baidu.com/s/1c2AwhaS(需密码) 系统:U ...

  5. centos监控web目录www下的文件是否被黑、挂马的脚本

    .检查是否有安装inotify rpm -qa inotify-tools 2.没有先安装epol源 wget -O /etc/yum.repos.d/epel.repo http://mirrors ...

  6. 算法竞赛进阶指南--快速幂,求a^b mod p

    // 快速幂,求a^b mod p int power(int a, int b, int p) { int ans = 1; for (; b; b >>= 1) { if (b &am ...

  7. centos7 源码安装goaccess

    1. 使用yum安装在不同服务器上可能失败, 推荐使用源码安装goaccess # 安装依赖 yum install -y ncurses-devel GeoIP-devel.x86_64 tokyo ...

  8. zabbix监控hbase

    项目地址:https://github.com/Staroon/zabbix-hadoop-template/tree/master/hbase-master-template (1).下载脚本,将其 ...

  9. Coursera课程笔记----C程序设计进阶----Week 5

    指针(二) (Week 5) 字符串与指针 指向数组的指针 int a[10]; int *p; p = a; 指向字符串的指针 指向字符串的指针变量 char a[10]; char *p; p = ...

  10. STM32学习笔记——GPIO

    单片机型号STM32F407VET6. 概述 GPIO的分类: 可接受5V输入的(FT),绝大多数引脚都是: 只能接受3.3V输入的(TTa),只有PA4和PA5,就是DAC输出的两个引脚: 其他,包 ...