Test for Job
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 11230   Accepted: 2651

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 i, Vi (0 ≤ |Vi| ≤ 20000)
The next m lines each contain two integers x, y 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

 
注意是无环图从入度为0出发至出度为0
无环图知每个点只能用一次,然后用的时候显然是有顺序的,那么就可以根据入度来排序从而推进答案喽
 
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + ;
int n, m, tot;
int cost[N], in[N], out[N], head[N], dp[N];
bool vis[N];
struct node
{
 int to, nxt;
}e[N * ];
void add(int x, int y)
{
 e[tot].to = y;
 e[tot].nxt = head[x];
 head[x] = tot++;
}
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()
{
 while(scanf("%d%d", &n, &m) != EOF) {
  memset(in, , sizeof(in));
  memset(out, , sizeof(out));
  memset(head, -, sizeof(head));
  memset(vis, false, sizeof(vis));
  tot = ;
  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 ;
}

poj3249 拓扑找最长路的更多相关文章

  1. bzoj1880: [Sdoi2009]Elaxia的路线(spfa,拓扑排序最长路)

    1880: [Sdoi2009]Elaxia的路线 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 1944  Solved: 759[Submit][St ...

  2. 2017 ACM-ICPC(乌鲁木齐赛区)网络赛 H.Skiing 拓扑排序+最长路

    H.Skiing In this winter holiday, Bob has a plan for skiing at the mountain resort. This ski resort h ...

  3. BZOJ 2019 [Usaco2009 Nov]找工作:spfa【最长路】【判正环】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2019 题意: 奶牛们没钱了,正在找工作.农夫约翰知道后,希望奶牛们四处转转,碰碰运气. 而 ...

  4. 2017 ACM-ICPC网络赛 H.Skiing 有向图最长路

    H.Skiing In this winter holiday, Bob has a plan for skiing at the mountain resort. This ski resort h ...

  5. The Largest Clique UVA - 11324( 强连通分量 + dp最长路)

    这题  我刚开始想的是  缩点后  求出入度和出度为0 的点  然后统计个数  用总个数 减去 然而 这样是不可以的  画个图就明白了... 如果  减去度为0的点  那么最后如果出现这样的情况是不可 ...

  6. BZOJ5450: 轰炸(水题,Tarjan缩点求最长路)

    5450: 轰炸 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 43  Solved:18[Submit][Status][Discuss] Desc ...

  7. [BZOJ1663] [Usaco2006 Open]赶集(spfa最长路)

    传送门 按照时间t排序 如果 t[i] + map[i][j] <= t[j],就在i和j之间连一条边 然后spfa找最长路 #include <queue> #include &l ...

  8. luogu 1113 杂务--啥?最长路?抱歉,我不会

    P1113 杂务 题目描述 John的农场在给奶牛挤奶前有很多杂务要完成,每一项杂务都需要一定的时间来完成它.比如:他们要将奶牛集合起来,将他们赶进牛棚,为奶牛清洗乳房以及一些其它工作.尽早将所有杂务 ...

  9. 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur (SCC缩点,SPFA最长路,枚举反边)

    P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 In an effort to better manage the grazing patterns of hi ...

随机推荐

  1. docker中安装nginx,部署前端代码

    最近在学习docker,初次接触,难免遇到磕磕碰碰,遂将其整理成博客,以便日后查看. 1.拉取nginx镜像 直接从官方镜像库拉取简单粗暴: docker pull nginx 2.运行 docker ...

  2. 【React踩坑记六】create-react-app创建的react项目通过iP地址访问(实现局域网内访问)

    同项目组的小伙伴想用自己的电脑访问我电脑上开发阶段的create-react-app创建的react项目. 试过了了各种内网穿透工具ngrok以及localtunnel等. 奈何打开效率实在太过于龟速 ...

  3. INTERVIEW #5

    笔试 150min,3题,每题100分,自己果然还是个蒟蒻呢~ 最近状态好差,虽然做了一些题,但还是考得稀烂,大概有几点需要加强: 独立做题,不要一边看板子一边写代码,更不要一开始就看题解: 对之前研 ...

  4. 重新认识Java注解

    重新认识Java注解 今天Debug看源码的时候,无意间看到这么个东西 首先承认我的无知,看到这个我很惊诧. 也勾起了我的好奇心,于是有了这篇认知记录. 下面就来重新认识下注解吧! 注解的本质 关于运 ...

  5. spark系列-3、缓存、共享变量

    一.persist  和 unpersist 1.1.persist() : 用来设置RDD的存储级别 存储级别 意义 MEMORY_ONLY 将RDD作为反序列化的的对象存储JVM中.如果RDD不能 ...

  6. ssm(spring,spring mvc,mybatis)框架

    ssm框架各个技术的职责 spring :spring是一个IOC DI AOP的 容器类框架 spring mvc:spring mvc 是一个mvc框架 mybatis:是一个orm的持久层框架 ...

  7. Spring官网阅读(十三)ApplicationContext详解(下)

    文章目录 BeanFactory 接口定义 继承关系 接口功能 1.HierarchicalBeanFactory 2.ListableBeanFactory 3.AutowireCapableBea ...

  8. POI问题总结,关于数字级联及多级级联(三级以上)

    目录 最近公司业务需要用到excel,并且要导出模板,今天为止所有的功能都已经实现了,在这里对出现的一些问题做一个总结. 效果图: 这是一个五级联动的数据,其中第一列是数字开头,实现了五级联动 问题1 ...

  9. x86软路由虚拟化openwrt-koolshare-mod-v2.33联通双拨IPV6教程(第一篇)

    本文分两篇发布,此为第一篇,第二篇:https://www.cnblogs.com/zlAurora/p/12433302.html   年前TB购置了一台软路由,对家里网络来了个大改造,实现了PPP ...

  10. Day_08【面向对象】扩展案例3_使用多态的形式创建缉毒狗对象,调用缉毒方法和吼叫方法

    分析以下需求,并用代码实现: 1.定义动物类: 行为: 吼叫:没有具体的吼叫行为 吃饭:没有具体的吃饭行为 2.定义缉毒接口 行为: 缉毒 3.定义缉毒狗:犬的一种 行为: 吼叫:汪汪叫 吃饭:狗啃骨 ...