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

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 iVi (0 ≤ |Vi| ≤ 20000) 

The next m lines each contain two integers xy 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

题目链接:点击打开链接

给出n个点的权值和m条路径, 问一条路的最大权值是多少.

起点入度为0, 对全部入度为0的点赋值到dp数组, 进行拓扑排序, 最后遍历出度为0的点, 求得最大值.

AC代码:

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "queue"
#include "stack"
#include "cmath"
#include "utility"
#include "map"
#include "set"
#include "vector"
#include "list"
#include "string"
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5 + 5;
int n, m, num;
int cost[MAXN], in[MAXN], out[MAXN], head[MAXN], dp[MAXN];
bool vis[MAXN];
struct node
{
/* data */
int fr, to, nxt;
}e[MAXN * 10];
void add(int x, int y)
{
e[num].fr = x;
e[num].to = y;
e[num].nxt = head[x];
head[x] = num++;
}
void toposort()
{
int cnt = 1;
while(cnt < n) {
for(int i = 1; i <= n; ++i)
if(in[i] == 0 && !vis[i]) {
vis[i] = true;
cnt++;
for(int j = head[i]; j != -1; 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(int argc, char const *argv[])
{
while(scanf("%d%d", &n, &m) != EOF) {
memset(in, 0, sizeof(in));
memset(out, 0, sizeof(out));
memset(head, -1, sizeof(head));
memset(vis, false, sizeof(vis));
num = 1;
for(int i = 1; i <= n; ++i)
scanf("%d", &cost[i]);
for(int i = 1; i <= m; ++i) {
int x, y;
scanf("%d%d", &x, &y);
add(x, y);
in[y]++;
out[x]++;
}
for(int i = 1; i <= n; ++i)
if(in[i] == 0) dp[i] = cost[i];
else dp[i] = -INF;
toposort();
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;
}

POJ3249 Test for Job(拓扑排序+dp)的更多相关文章

  1. POJ 3249 拓扑排序+DP

    貌似是道水题.TLE了几次.把所有的输入输出改成scanf 和 printf ,有吧队列改成了数组模拟.然后就AC 了.2333333.... Description: MR.DOG 在找工作的过程中 ...

  2. [NOIP2017]逛公园 最短路+拓扑排序+dp

    题目描述 给出一张 $n$ 个点 $m$ 条边的有向图,边权为非负整数.求满足路径长度小于等于 $1$ 到 $n$ 最短路 $+k$ 的 $1$ 到 $n$ 的路径条数模 $p$ ,如果有无数条则输出 ...

  3. 洛谷P3244 落忆枫音 [HNOI2015] 拓扑排序+dp

    正解:拓扑排序+dp 解题报告: 传送门 我好暴躁昂,,,怎么感觉HNOI每年总有那么几道题题面巨长啊,,,语文不好真是太心痛辣QAQ 所以还是要简述一下题意,,,就是说,本来是有一个DAG,然后后来 ...

  4. 【BZOJ-1194】潘多拉的盒子 拓扑排序 + DP

    1194: [HNOI2006]潘多拉的盒子 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 456  Solved: 215[Submit][Stat ...

  5. 【BZOJ5109】[CodePlus 2017]大吉大利,晚上吃鸡! 最短路+拓扑排序+DP

    [BZOJ5109][CodePlus 2017]大吉大利,晚上吃鸡! Description 最近<绝地求生:大逃杀>风靡全球,皮皮和毛毛也迷上了这款游戏,他们经常组队玩这款游戏.在游戏 ...

  6. bzoj1093[ZJOI2007]最大半连通子图(tarjan+拓扑排序+dp)

    Description 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v的有向路径或者从v到u ...

  7. 【bzoj4011】[HNOI2015]落忆枫音 容斥原理+拓扑排序+dp

    题目描述 给你一张 $n$ 个点 $m$ 条边的DAG,$1$ 号节点没有入边.再向这个DAG中加入边 $x\to y$ ,求形成的新图中以 $1$ 为根的外向树形图数目模 $10^9+7$ . 输入 ...

  8. 【bzoj1093】[ZJOI2007]最大半连通子图 Tarjan+拓扑排序+dp

    题目描述 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:对于u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v的有向路径或者从v到u的有向路径. ...

  9. 【bzoj4562】[Haoi2016]食物链 拓扑排序+dp

    原文地址:http://www.cnblogs.com/GXZlegend/p/6832118.html 题目描述 如图所示为某生态系统的食物网示意图,据图回答第1小题 现在给你n个物种和m条能量流动 ...

随机推荐

  1. 装饰模式(Decorator)C++实现

    装饰模式 层层包装,增强功能.这就是装饰模式的要旨!装饰器模式就是基于对象组合的方式,可以很灵活的给对象添加所需要的功能.它把需要装饰的功能放在单独的类中,并让这个类包装它所要装饰的对象. 意图: 动 ...

  2. 运行Tomcat闪退问题,报的错误:Unsupported major.minor version 51.0

    在MyEclipse中运行tomcat,tomcat闪退并且报以下错误. java.lang.UnsupportedClassVersionError: org/apache/catalina/sta ...

  3. 详解sqlserver查询表索引

    SELECT   索引名称=a.name ,表名=c.name ,索引字段名=d.name ,索引字段位置=d.colid ? 1 2 3 4 5 6 7 8 FROM  sysindexes  a ...

  4. 项目随笔之springmvc中freemark如何获取项目路径

    转载:http://blog.csdn.net/whatlookingfor/article/details/51538995 在SpringMVC框架中使用Freemarker试图时,要获取根路径的 ...

  5. B/S架构的网站测试

      一.功能测试 1.链接测试 链接是Web应用系统的一个主要特征,它是在页面之间切换和指导用户去一些不知道地址的页面的主要手段.链接测试可分为三个方面.首先,测试所有链接是否按指示的那样确实链接到了 ...

  6. Docker镜像的备份和恢复

    备份: docker save -o [tar包真实路径] [镜像名 ] 如:docker save -o /usr/docker_data/mongo-backup.tar mongo   导出: ...

  7. hibernate简单集合映射和获取

    简单集合映射(可以直接获取) // javabean设计 public class User { private int userId; private String userName; // 一个用 ...

  8. 取/etc/password文件最后一个单词的最后一个字符

    三种方法都可以 [root@localhost ~]# sed -n "1,5 s#.*\(.\)#\1#p" /etc/passwd [root@localhost ~]# se ...

  9. Nginx 支持websocket的配置

    Nginx 支持websocket的配置server { listen 80; server_name 域名; location / { proxy_pass http://127.0.0.1:808 ...

  10. [总结-动态规划]经典DP状态设定和转移方程

    马上区域赛,发现DP太弱,赶紧复习补上. #普通DP CodeForces-546D Soldier and Number Game 筛法+动态规划 待补 UVALive-8078 Bracket S ...