poj3249 拓扑找最长路
| 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 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
Sample Input
6 5
1
2
2
3
3
4
1 2
1 3
2 4
3 4
5 6
Sample Output
7
Hint

#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 拓扑找最长路的更多相关文章
- bzoj1880: [Sdoi2009]Elaxia的路线(spfa,拓扑排序最长路)
1880: [Sdoi2009]Elaxia的路线 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 1944 Solved: 759[Submit][St ...
- 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 ...
- BZOJ 2019 [Usaco2009 Nov]找工作:spfa【最长路】【判正环】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2019 题意: 奶牛们没钱了,正在找工作.农夫约翰知道后,希望奶牛们四处转转,碰碰运气. 而 ...
- 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 ...
- The Largest Clique UVA - 11324( 强连通分量 + dp最长路)
这题 我刚开始想的是 缩点后 求出入度和出度为0 的点 然后统计个数 用总个数 减去 然而 这样是不可以的 画个图就明白了... 如果 减去度为0的点 那么最后如果出现这样的情况是不可 ...
- BZOJ5450: 轰炸(水题,Tarjan缩点求最长路)
5450: 轰炸 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 43 Solved:18[Submit][Status][Discuss] Desc ...
- [BZOJ1663] [Usaco2006 Open]赶集(spfa最长路)
传送门 按照时间t排序 如果 t[i] + map[i][j] <= t[j],就在i和j之间连一条边 然后spfa找最长路 #include <queue> #include &l ...
- luogu 1113 杂务--啥?最长路?抱歉,我不会
P1113 杂务 题目描述 John的农场在给奶牛挤奶前有很多杂务要完成,每一项杂务都需要一定的时间来完成它.比如:他们要将奶牛集合起来,将他们赶进牛棚,为奶牛清洗乳房以及一些其它工作.尽早将所有杂务 ...
- 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur (SCC缩点,SPFA最长路,枚举反边)
P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 In an effort to better manage the grazing patterns of hi ...
随机推荐
- vue2.x学习笔记(二十四)
接着前面的内容:https://www.cnblogs.com/yanggb/p/12663909.html. 插件 插件通常是用来为vue添加全局功能的. 插件的功能范围 插件的功能范围没有严格的限 ...
- 【集群实战】NFS服务常见故障排查和解决方法
NFS,全名叫Network File System,中文叫网络文件系统,是Linux.UNIX系统的分布式文件系统的一个组成部分,可实现在不同网络上共享远程文件系统. NFS由Sun公司开发,目前已 ...
- Linux硬盘分区知识
前言 硬盘使用前,一般要分区,格式化(创建文件系统)<== 存放数据 类比,房子使用前,一般要隔断,装修,买家具,再住人. 分区 一块硬盘: 主分区.扩展分区.逻辑分区 主分区+扩展分区的数量& ...
- FastReport.Net中使用列表和数组作为报表数据源
大多数现代报告工具允许您使用几乎任何数据库,然而,并不是所有报表工具都能以一个数据源的列表或数组来工作.本文中将展示如何使用FastReport .Net报表工具来实现. 请注意以下重要几点: 清单中 ...
- Spring PropertyPlaceholderConfigurer类载入外部配置
2019独角兽企业重金招聘Python工程师标准>>> 通常在Spring项目中如果用到配置文件时,常常会使用org.springframework.beans.factory.co ...
- 四、CentOS 6.5 上传和安装Nginx
CentOS 6.5 上传和安装Nginx
- vue中给window添加滚动监听无效的解决方案
原文链接: 点我 页面中有这么一个需求,当页面滚动到一定高度之后,页面中的某些元素进行吸顶,固定到顶部位置,或者是滚动到一定程度进行更新数据的操作.我相信不少网友查阅过类似的资料,网友给出的解决方案, ...
- 图论--最短路-- Dijkstra模板(目前见到的最好用的)
之前的我那个板子,老是卡内存,不知道为什么,我看别人过的那个题都是结构体,我就开始对自己板子做了修改,然后他奶奶的就过了,而且速度也提高了,内存也小了.(自从用了这个板子,隔壁小孩馋哭了)也不知道为啥 ...
- SpringBoot:整合Shiro
目录 1.Shiro简介 1.1.什么是Shiro? 1.2.有哪些功能 1.3.Shiro架构(外部) 1.4.Shiro架构(内部) 2.HelloWorld 3.Shiro整合Spring Bo ...
- search(10)- elastic4s-multi_match:多字段全文搜索
在全文搜索中我们常常会在多个字段中匹配同一个查询条件或者在不同的字段中匹配不同的条件.比如下面这个例子: GET /books/_search { "query": { " ...