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 ...
随机推荐
- HDU2037 今年暑假不AC 【贪心】
今年暑假不AC Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- Linux查看用户数、登录用户
如果是系统中全部只要默认shell是bash的就包括那么二楼正解,就是cat /etc/passwd|grep bash|wc -l如果是正在登陆系统的账户中使用bash shell的,那么ps -e ...
- java性能缓慢
虚拟帝国上面有很多营销软件是JAVA开发的!创业公司通常选择开源技术减少项目管理费用. 除了使用Java编程语言,创业公司也可以利用Java开发工具包的好处(JDK),Java运行时环境(JRE)和J ...
- 《JavaScript设计模式与开发实践》读书笔记之中介者模式
1. 中介者模式 中介者模式的作用就是用来解除对象与对象之间的紧耦合关系,增加中介者后,所有相关对象都通过中介者来通信,而不再相互引用 1.1中介者模式的例子 以泡泡堂游戏为例,先定义一个玩家构造函数 ...
- ECshop lib_base.php on line 1241 错误解决方法
ECSHOP做的一个网站,突然报这个错误,整个网站打不开,后来找了很久,终于找到这个方法,亲测可用 Notice: Undefinedvariable: data in D:\wwwroot\KISS ...
- SecureCRT学习之道:用SecureCRT来上传和下载数据
今天才知道,原来SecureCRT可以使用linux下的zmodem协议来快速的传送文件,而且还使用非常方便哦,我还傻傻的找其他软件来sftp,笨死了:(你只要设置一下上传和下载的默认目录就行opti ...
- Linux应用环境实战05:在Ubuntu 14.10中借用Windows的字体 (转)
阅读目录 设置系统字体 安装微软的英文字体 查看系统的配置文件 借用Windows的字体 编写配置文件 在前一篇随笔中,我详细讨论了字体的分类及用途,也以Fedora 20为例,展示了字体配置的思路和 ...
- Linux查看进程线程个数
1.根据进程号进行查询: # pstree -p 进程号 # top -Hp 进程号 2.根据进程名字进行查询: # pstree -p `ps -e | grep server | awk '{pr ...
- 强联通块tarjan算法
http://poj.org/problem?id=1236第一问:需要几个学校存在软件,才能通过传递,使得所有的学校都有软件 用tarjan算法求出强联通分量后,将每个联通分量缩成一个点,那么问题1 ...
- SAP ABAP计划 SY-REPID与SY-CPROG差异
首先,它的两个解释 sy-repid is the name of the current program. "当前程序的程序名 ...