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. 关于JAVA Project.waitfor()返回值是1

    Project.waitfor()返回值是1,找了很久从网上没有发现关于1的说明. 这时对源代码调试了一下,发现Project=null.而去根目录下点击被调用的bat文件发现也可以被正确执行. 这时 ...

  2. 陈一舟《情系人人》:先搞钱,再搞人才_DoNews-IT门户-移动互联网新闻-电子商务新闻-游戏新闻-风险投资新闻-IT社交网络社区

    陈一舟<情系人人>:先搞钱,再搞人才_DoNews-IT门户-移动互联网新闻-电子商务新闻-游戏新闻-风险投资新闻-IT社交网络社区 陈一舟<情系人人>:先搞钱,再搞人才

  3. INSTALL_FAILED_MEDIA_UNAVAILABLE错误处理

    问题描写叙述: 在android手机上安装apk的时候,报错例如以下: Installation error: INSTALL_FAILED_MEDIA_UNAVAILABLE Please chec ...

  4. linux+nginx+mysql+php

    LNMP(linux+nginx+mysql+php)服务器环境配置   一.简介 Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为 “engine X”, 是一个高性能的 ...

  5. Eclipse上运行Python,使用PyDev

    转自:http://www.ibm.com/developerworks/cn/opensource/os-cn-ecl-pydev/index.html 级别: 初级 郑 伟芳 (zhengwf@c ...

  6. HUST 1017(DLX)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=65998#problem/A 题意:求01矩阵的精确覆盖. DLX学习资料:ht ...

  7. android插件技术-apkplug于OSGI服务基础-08

    我们提供 apkplug 下OSGI使用demo 源代码托管地址为 http://git.oschina.net/plug/OSGIService 一 OSGI与android Service 异同点 ...

  8. MySQL 触发器结构及三个案例demo

    --你必须拥有相当大的权限才能创建触发器(CREATE TRIGGER),如果你已经是Root用户,那么就足够了.这跟SQL的标准有所不同. CREATE TRIGGER语法 CREATE TRIGG ...

  9. V5

    系统设置--关于手机--版本号点5下--进去开发模式--打开开发选项--打开USB调试.然后在连接第三方助手软件 http://bbs.ztehn.com/thread-19037-1-1.html

  10. aStar算法求第k短路

    A*的概念主意在于估计函数,f(n)=g(n)+h(n),f(n)是估计函数,g(n)是n节点的当前代价,h(n)是n节点的估计代价:而实际中,存在最优的估计函数f'(n)=g'(n)+h'(n),那 ...