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 profitVi 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 cityi,
Vi (0 ≤ |Vi| ≤ 20000)

The next m lines each contain two integers x, y indicating that there is a road leads from cityx 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

题意:一个人去找工作遇到了一道面试题。面试官要求给出一些城市和城市之间的道路,每到达一个城市。可能会赚一些钱,可是也可能会有损失。

终于面试者的所得会决定他能否得到这份工作。显而易见,越多越好。

思路:由于是有向无环图(DAG)并且事实上求的是从一个0入度到0出度的路径,所以我们能够用topsort来处理,再加上简单的DP 即可了

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int maxn = 100005;
const int inf = 0x3f3f3f3f; struct Node {
int v, next;
}node[maxn*20];
int n, m, cnt;
int profit[maxn];
int ind[maxn], out[maxn], dp[maxn], adj[maxn]; void topsort() {
queue<int> q;
for (int i = 1; i <= n; i++)
if (ind[i] == 0) {
q.push(i);
dp[i] = profit[i];
}
while (!q.empty()) {
int cur = q.front();
q.pop();
for (int i = adj[cur]; i != -1; i = node[i].next) {
int v = node[i].v;
if (dp[v] < dp[cur]+profit[v])
dp[v] = dp[cur]+profit[v];
if (--ind[v] == 0)
q.push(v);
}
}
} int main() {
while (scanf("%d%d", &n, &m) != EOF) {
cnt = 0;
memset(adj, -1, sizeof(adj));
memset(ind, 0, sizeof(ind));
memset(out, 0, sizeof(out));
for (int i = 1; i <= n; i++) {
dp[i] = -inf;
scanf("%d", &profit[i]);
}
for (int i = 0; i < m; i++) {
int a, b;
scanf("%d%d", &a, &b);
out[a]++;
ind[b]++;
node[cnt].v = b;
node[cnt].next = adj[a];
adj[a] = cnt++;
}
topsort();
int ans = -inf;
for (int i = 1; i <= n; i++)
if (out[i] == 0 && dp[i] > ans)
ans = dp[i];
printf("%d\n", ans);
}
return 0;
}

POJ - 3249 Test for Job (DAG+topsort)的更多相关文章

  1. poj 3249 Test for Job (DAG最长路 记忆化搜索解决)

    Test for Job Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 8990   Accepted: 2004 Desc ...

  2. Luogu 1894 [USACO4.2]完美的牛栏The Perfect Stall / POJ 1274 The Perfect Stall(二分图最大匹配)

    Luogu 1894 [USACO4.2]完美的牛栏The Perfect Stall / POJ 1274 The Perfect Stall(二分图最大匹配) Description 农夫约翰上个 ...

  3. POJ 3087 Shuffle'm Up(洗牌)

    POJ 3087 Shuffle'm Up(洗牌) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 A common pas ...

  4. POJ 1426 Find The Multiple(寻找倍数)

    POJ 1426 Find The Multiple(寻找倍数) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Given ...

  5. 【POJ 1716】Integer Intervals(差分约束系统)

    id=1716">[POJ 1716]Integer Intervals(差分约束系统) Integer Intervals Time Limit: 1000MS   Memory L ...

  6. poj 2060 Taxi Cab Scheme(DAG图的最小路径覆盖)

    题意: 出租车公司有M个订单. 订单格式:     hh:mm  a  b  c  d 含义:在hh:mm这个时刻客人将从(a,b)这个位置出发,他(她)要去(c,d)这个位置. 规定1:从(a,b) ...

  7. 【POJ】2187 Beauty Contest(旋转卡壳)

    http://poj.org/problem?id=2187 显然直径在凸包上(黑书上有证明).(然后这题让我发现我之前好几次凸包的排序都错了QAQ只排序了x轴.....没有排序y轴.. 然后本题数据 ...

  8. POJ 3268 Silver Cow Party (双向dijkstra)

    题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  9. POJ - 1426 Find The Multiple(搜索+数论)

    转载自:優YoU  http://user.qzone.qq.com/289065406/blog/1303946967 以下内容属于以上这位dalao http://poj.org/problem? ...

随机推荐

  1. jquery实现菜单功能(单击展开或者关闭)-一般应用于后台

    <!doctype html> <html> <head> <meta charset="gb2312"> <title> ...

  2. C++教材

    C++语言: 1.<Essential C++>:Stanley B.Lipman著. 旁枝暂略,主攻核心,轻薄短小.附习题与解答,适合刚開始学习的人. 2.<The C++ Pro ...

  3. 如何解决KEIL 5 编KEIL4同RTX系统的project解决方法

    1.我个人KEIL5与KEIL4对照 相较于KEIL 5 的"华丽".笔者还是喜欢KEIL4的"内敛",主要也还是习惯了.懒得换了.由于工作的  原      ...

  4. thinkphp中URL传参数的几种方式

    在thinkphp中,url传参合asp.net中原理类似,下面就单个参数和多个参数传递方式进行一个简单讲解 1.传单个参数 单个参数这种比较简单,例如 想像edit操作里面传递一个id值,如下写法_ ...

  5. Android 的独特shell命令

    Android本来就是一个linux操作系统,所以大部分都是linux的命令,如mkdir,ls,netstat,mount,ps 等,这里就不具体介绍了, 主要介绍几个Android特有的. get ...

  6. 怎样用js得到当前页面的url信息方法(JS获取当前网址信息)

    设置或获取对象指定的文件名称或路径.window.location.pathname 设置或获取整个 URL 为字符串.window.location.href; 设置或获取与 URL 关联的端口号码 ...

  7. UVA 10828 - Back to Kernighan-Ritchie(概率+高斯消元)

    UVA 10828 - Back to Kernighan-Ritchie 题目链接 题意:给图一个流程图,有结点的流程,每次进入下一个流程概率是均等的,有q次询问,求出每次询问结点的运行期望 思路: ...

  8. Libevent使用样例,从简单到复杂

            转载请注明出处:http://blog.csdn.net/luotuo44/article/details/39670221 本文从简单到复杂.展示怎样使用libevent.网上的很多 ...

  9. iPhone、iPad强制关机

    情景:iPad測试应用过程中死机了. 解决:同一时候按住右上方的电源键和屏幕下方的HOME键大约10秒左右. 就会自己主动强制断电关机,然后重新启动. 强制重新启动后你会看到进入苹果的标志,然后进入主 ...

  10. Hibernate实体对象继承策略

    Hibernate继承策略总共同拥有三种,一种是共用一张表:一种是每一个类一张表,表里面储存子类的信息和父类的信息:另一种是通过表连接的方式.每一个类都有一张表,可是子类相应的表仅仅保存自己的信息,父 ...