POJ - 3249 Test for Job (DAG+topsort)
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 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
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)的更多相关文章
- poj 3249 Test for Job (DAG最长路 记忆化搜索解决)
Test for Job Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 8990 Accepted: 2004 Desc ...
- 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 农夫约翰上个 ...
- POJ 3087 Shuffle'm Up(洗牌)
POJ 3087 Shuffle'm Up(洗牌) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 A common pas ...
- POJ 1426 Find The Multiple(寻找倍数)
POJ 1426 Find The Multiple(寻找倍数) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Given ...
- 【POJ 1716】Integer Intervals(差分约束系统)
id=1716">[POJ 1716]Integer Intervals(差分约束系统) Integer Intervals Time Limit: 1000MS Memory L ...
- poj 2060 Taxi Cab Scheme(DAG图的最小路径覆盖)
题意: 出租车公司有M个订单. 订单格式: hh:mm a b c d 含义:在hh:mm这个时刻客人将从(a,b)这个位置出发,他(她)要去(c,d)这个位置. 规定1:从(a,b) ...
- 【POJ】2187 Beauty Contest(旋转卡壳)
http://poj.org/problem?id=2187 显然直径在凸包上(黑书上有证明).(然后这题让我发现我之前好几次凸包的排序都错了QAQ只排序了x轴.....没有排序y轴.. 然后本题数据 ...
- POJ 3268 Silver Cow Party (双向dijkstra)
题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- POJ - 1426 Find The Multiple(搜索+数论)
转载自:優YoU http://user.qzone.qq.com/289065406/blog/1303946967 以下内容属于以上这位dalao http://poj.org/problem? ...
随机推荐
- nginx源代码分析--高性能server开发 常见的流程模型
1.高性能server 对于高性能server对于.处理速度和占用空间小是典型特性.特别是当server经验C10K问题的时候(网络server在处理数以万计的client连接时.往往出现效率低下甚至 ...
- DHTML【10】--Javascript
大家好,这一节主要介绍Javascript的函数.函数是Javascript的核心中的核心,这么强调一点都不过分,相信没有人反对,如果有人反对,你以后可以不用函数,呵呵,说的有点绝了啊. 下面看一下J ...
- hdu1506(dp求最大子矩阵)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1506 分析: 对于每个单位矩阵,我们先求出连续比它高的最左边的下标假设为l,然后求出比它高的最右边的下 ...
- 【剑指offer】面试题24:二叉搜索树的兴许前序遍历序列
分析: 前序: 根 左 右 后序: 左 由 根 二叉搜索树: 左 < 根 < 右 那么这就非常明显了. def ifpost(postArray, start, end): #one or ...
- 存储管理(一):openfiler介绍及存储理解
openfiler是一个免费的.开源的基于浏览器的网络存储产品,支持基于文件的的网络连接存储(NAS)和基于块的存储区域网(SAN).支持的协议有smb,cifs,nfs,http/dev和ftp. ...
- 《Android内核剖析》读书笔记 第13章 View工作原理【View树遍历】
View状态分类 在View视图中定义了多种和界面效果相关的状态,比如拥有焦点Focused.按下Pressed等,不同的状态一般会显示不同的界面效果,而且视图状态会随着用户的操作而改变,一般通过xm ...
- XAML基础(一)
1.0 XAML是啥? XAML(eXtensible Application Markup Language,可 扩展应用 程序标记语言) 是一种声明性的XML语法 ,像WPF,WF或者Silver ...
- JAVA insert() 插入字符串 reverse() 颠倒 delete()和deleteCharAt() 删除字符 replace() 替换 substring() 截取子串
insert() 插入字符串 StringBuffer insert(int index,String str) StringBuffer insert(int index,char ch) Stri ...
- Hibernate 配置详解(11)
hibernate.session_factory_name_is_jndi 配置hibernate.cfg.xml中SessionFactory的name属性是否作为JNDI名称绑定.默认是true ...
- unity3d教程动态创建简单平面地形
unity3d创建地形是不须要usingUnityEditor的.这里使用了AssetDatabase.所以需using UnityEditor; 创建三步: 1.TerrainData terrai ...