Codeforces 459E Pashmak and Graph(dp+贪婪)
题目链接:Codeforces 459E Pashmak and Graph
题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径。
解题思路:将边依照权值排序,每次将同样权值的边同一时候增加,维护每一个点作为终止点的最大长度就可以。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 3 * 1e5+5;
struct edge {
int u, v, w;
}s[maxn];
bool cmp (const edge& a, const edge& b) {
return a.w < b.w;
}
int n, m, d[maxn], f[maxn], val[maxn];
int main () {
scanf("%d%d", &n, &m);
memset(d, 0, sizeof(d));
memset(f, 0, sizeof(f));
memset(val, 0, sizeof(val));
for (int i = 0; i < m; i++)
scanf("%d%d%d", &s[i].u, &s[i].v, &s[i].w);
sort(s, s + m, cmp);
for (int i = 0; i < m; i++) {
int j;
for (j = i; s[j].w == s[i].w && j < m; j++);
for (int k = i; k < j; k++)
d[s[k].v] = max(d[s[k].v], f[s[k].u] + 1);
for (int k = i; k < j; k++)
f[s[k].v] = d[s[k].v];
i = j - 1;
}
int ans = 0;
for (int i = 1; i <= n; i++)
ans = max(ans, f[i]);
printf("%d\n", ans);
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
Codeforces 459E Pashmak and Graph(dp+贪婪)的更多相关文章
- CodeForces - 459E Pashmak and Graph[贪心优化dp]
E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces 459E Pashmak and Graph:dp + 贪心
题目链接:http://codeforces.com/problemset/problem/459/E 题意: 给你一个有向图,每条边有边权. 让你找出一条路径,使得这条路径上的边权严格递增. 问你这 ...
- Codeforces 459E Pashmak and Graph
http://www.codeforces.com/problemset/problem/459/E 题意: 给出n个点,m条边的有向图,每个边有边权,求一条最长的边权上升的路径的长度. 思路:用f存 ...
- codeforces 459E E. Pashmak and Graph(dp+sort)
题目链接: E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabytes input st ...
- Codeforces Round #261 (Div. 2) E. Pashmak and Graph DP
http://codeforces.com/contest/459/problem/E 不明确的是我的代码为啥AC不了,我的是记录we[i]以i为结尾的点的最大权值得边,然后wa在第35 36组数据 ...
- CF459E Pashmak and Graph (DP?
Codeforces Round #261 (Div. 2) E - Pashmak and Graph E. Pashmak and Graph time limit per test 1 seco ...
- codeforces 459E
codeforces 459E E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabyte ...
- ACM - 最短路 - CodeForces 295B Greg and Graph
CodeForces 295B Greg and Graph 题解 \(Floyd\) 算法是一种基于动态规划的算法,以此题为例介绍最短路算法中的 \(Floyd\) 算法. 我们考虑给定一个图,要找 ...
- cf459E Pashmak and Graph
E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabytes input standard ...
随机推荐
- CMS垃圾回收机制
详解CMS垃圾回收机制 原创不易,未经允许,不得转载~~~ 什么是CMS? Concurrent Mark Sweep. 看名字就知道,CMS是一款并发.使用标记-清除算法的gc. CMS是针对老 ...
- sql大小转换函数
将字段值转换成大写 UPDATE t SET [name]=UPPER([name]) 将字段值转换成小写 UPDATE t SET [name]=LOWER([name])
- Eclipse中的SVN的冲突解决方案详解
版本冲突原因: 假设A.B两个用户都在版本号为100的时候,更新了kingtuns.txt这个文件,A用户在修改完成之后提交kingtuns.txt到服务器,这个时候提交成功,这个时候kingtuns ...
- Codeforces #252 (Div. 2) B. Valera and Fruits
题目倒是不难,可是读起来非常恶心 依据题目的描写叙述不easy找到适合存储的方法 后来我就想不跟着出题人的思路走 我自己开一个数组c 令c[a[i]] = b[i] 则c[i] == [j] 代表第i ...
- loading加载中效果
(function(){ try{ var ui={ loading:{ addCssStyle:function(text) { var head = document.getElementsByT ...
- ecshop首页调用指定分类的所有产品(指定一级调二级)
第一种方法 第一 在/includes/lib_goods.php下增加如下代码,用过网上的直接换掉就可以 function index_get_cat_id_goods_best_list($cat ...
- hdu4738(割桥)
找人去炸边,炸完之后分成两个连通块(炸割桥) 每条边上有w个守卫,派去炸桥的人不能比守卫少 所以, 如果原本不连通,那么输出0 如果没有桥,输出-1 如果有桥没有守卫,那么是输出1,而不是0(tric ...
- LeetCode18:4Sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- SQL Server :理解Page Free Space (PFS) 页
原文:SQL Server :理解Page Free Space (PFS) 页 我们已经讨论了GAM与SGAM页,数据页(Data Page) ,现在我们来看下页面自由空间页(Page Free S ...
- iOS: NSMutableArray的方法removeObject:inRange:
- (void)removeObject:(id)anObject inRange:(NSRange)aRange