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条路径, 问一条路的最大权值是多少.
思路:原本思路是BFS枚举..然而MLE了,那么只能用dp来优化空间了。。两者都是枚举所有情况,前者将每次情况存入队列时容易因为图稠密而MLE;后者仅不断更新dp数组,在空间上节省了很多。
 #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 + ;

 const int INF = 0x3f3f3f3f;

 const int MAXN = 1e5 + ;

 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 * ];

 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 = ;

     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(int argc, char const *argv[])

 {

     while(scanf("%d%d", &n, &m) != EOF) {

         memset(in, , sizeof(in));

         memset(out, , sizeof(out));

         memset(head, -, sizeof(head));

         memset(vis, false, sizeof(vis));

         num = ;

         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 ;

 }

代码转自(https://blog.csdn.net/gkhack/article/details/50223357)

 

POJ 3249 Test for Job(拓扑排序+dp优化空间)的更多相关文章

  1. POJ 3249 Test for Job (拓扑排序+DP)

    POJ 3249 Test for Job (拓扑排序+DP) <题目链接> 题目大意: 给定一个有向图(图不一定连通),每个点都有点权(可能为负),让你求出从源点走向汇点的路径上的最大点 ...

  2. POJ 3249 拓扑排序+DP

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 静态页面缓存(thymeleaf模板writer)

    //前端html <!DOCTYPE html><html lang="en"> <head> <meta charset="U ...

  2. PAT Basic 1075 链表元素分类(25) [链表]

    题目 给定⼀个单链表,请编写程序将链表元素进⾏分类排列,使得所有负值元素都排在⾮负值元素的前⾯,⽽[0, K]区间内的元素都排在⼤于K的元素前⾯.但每⼀类内部元素的顺序是不能改变的.例如:给定链表为 ...

  3. ZJNU 1538 - YN!ngC的取子游戏--高级

    Nim博弈 因为移动到第0阶会消失 所以可以得到从最后一个人操作必定是把第1阶所有子全部移动到第0阶 递推可得,最后一个能把奇数阶的子移动到偶数阶上的人将会必胜 所以这个必胜条件就是奇数阶上的子全部为 ...

  4. vuex的优缺点

    vuex的优点 1.解决了非父子组件的消息传递(将数据存放在state中) 2.减少了AJAX请求次数,有些情景可以直接从内存中的state获取 vuex的缺点 1.刷新浏览器,vuex中的state ...

  5. CodeForces 993A Two Squares(数学 几何)

    https://codeforces.com/problemset/problem/993/A 题意: 给你两个矩形,第一行是一个正面表示的矩形,第二个是一个旋转四十五度角的矩形,问这两个矩形是否相交 ...

  6. 第04项目:淘淘商城(SpringMvc+Spring+Mybatis) 的学习实践总结【第三天】

    淘淘商城(SpringMVC+Spring+Mybatis)  是传智播客在2015年9月份录制的,几年过去了.由于视频里课上老师敲的代码和项目笔记有些细节上存在出入,只有根据日志报错信息作出适当的调 ...

  7. 897A. Scarborough Fair# 斯卡布罗集市(模拟)

    题目出处:http://codeforces.com/problemset/problem/897/A 题目大意:将某个范围内的某个字符换成另外一个字符 #include<iostream> ...

  8. 拿到新linux服务器,从配置环境到跑起项目的部署流程

    今早翻开手机看到某云一条广告Linux1 核 2 GB 1 Mbps服务器10元一个月,正巧最近在搞linux,于是下单了一台2个月20. 上班来到公司后,借空闲时间一顿研究,一波骚操作配置后,浏览器 ...

  9. html中table的px与百分比格式设置

    PX是Pixel的缩写,也就是说像素是指基本原色素及其灰度的基本编码,由 Picture(图像) 和 Element(元素)这两个单词的字母所组成的 Html设置table格式时面临着百分比还是px的 ...

  10. OAuth 2.0安全案例回顾

    转载自:http://www.360doc.com/content/14/0311/22/834950_359713295.shtml 0x00 背景 纵观账号互通发展史,可以发现OAuth比起其它协 ...