题目链接:Codeforces 459E Pashmak and Graph

题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径。

解题思路:将边依照权值排序,每次将同样权值的边同一时候增加,维护每一个点作为终止点的最大长度就可以。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. using namespace std;
  5. const int maxn = 3 * 1e5+5;
  6. struct edge {
  7. int u, v, w;
  8. }s[maxn];
  9. bool cmp (const edge& a, const edge& b) {
  10. return a.w < b.w;
  11. }
  12. int n, m, d[maxn], f[maxn], val[maxn];
  13. int main () {
  14. scanf("%d%d", &n, &m);
  15. memset(d, 0, sizeof(d));
  16. memset(f, 0, sizeof(f));
  17. memset(val, 0, sizeof(val));
  18. for (int i = 0; i < m; i++)
  19. scanf("%d%d%d", &s[i].u, &s[i].v, &s[i].w);
  20. sort(s, s + m, cmp);
  21. for (int i = 0; i < m; i++) {
  22. int j;
  23. for (j = i; s[j].w == s[i].w && j < m; j++);
  24. for (int k = i; k < j; k++)
  25. d[s[k].v] = max(d[s[k].v], f[s[k].u] + 1);
  26. for (int k = i; k < j; k++)
  27. f[s[k].v] = d[s[k].v];
  28. i = j - 1;
  29. }
  30. int ans = 0;
  31. for (int i = 1; i <= n; i++)
  32. ans = max(ans, f[i]);
  33. printf("%d\n", ans);
  34. return 0;
  35. }

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Codeforces 459E Pashmak and Graph(dp+贪婪)的更多相关文章

  1. CodeForces - 459E Pashmak and Graph[贪心优化dp]

    E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabytes input standard ...

  2. Codeforces 459E Pashmak and Graph:dp + 贪心

    题目链接:http://codeforces.com/problemset/problem/459/E 题意: 给你一个有向图,每条边有边权. 让你找出一条路径,使得这条路径上的边权严格递增. 问你这 ...

  3. Codeforces 459E Pashmak and Graph

    http://www.codeforces.com/problemset/problem/459/E 题意: 给出n个点,m条边的有向图,每个边有边权,求一条最长的边权上升的路径的长度. 思路:用f存 ...

  4. 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 ...

  5. Codeforces Round #261 (Div. 2) E. Pashmak and Graph DP

    http://codeforces.com/contest/459/problem/E 不明确的是我的代码为啥AC不了,我的是记录we[i]以i为结尾的点的最大权值得边,然后wa在第35  36组数据 ...

  6. CF459E Pashmak and Graph (DP?

    Codeforces Round #261 (Div. 2) E - Pashmak and Graph E. Pashmak and Graph time limit per test 1 seco ...

  7. codeforces 459E

    codeforces 459E E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabyte ...

  8. ACM - 最短路 - CodeForces 295B Greg and Graph

    CodeForces 295B Greg and Graph 题解 \(Floyd\) 算法是一种基于动态规划的算法,以此题为例介绍最短路算法中的 \(Floyd\) 算法. 我们考虑给定一个图,要找 ...

  9. cf459E Pashmak and Graph

    E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabytes input standard ...

随机推荐

  1. HDU2037 今年暑假不AC 【贪心】

    今年暑假不AC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  2. Linux查看用户数、登录用户

    如果是系统中全部只要默认shell是bash的就包括那么二楼正解,就是cat /etc/passwd|grep bash|wc -l如果是正在登陆系统的账户中使用bash shell的,那么ps -e ...

  3. java性能缓慢

    虚拟帝国上面有很多营销软件是JAVA开发的!创业公司通常选择开源技术减少项目管理费用. 除了使用Java编程语言,创业公司也可以利用Java开发工具包的好处(JDK),Java运行时环境(JRE)和J ...

  4. 《JavaScript设计模式与开发实践》读书笔记之中介者模式

    1. 中介者模式 中介者模式的作用就是用来解除对象与对象之间的紧耦合关系,增加中介者后,所有相关对象都通过中介者来通信,而不再相互引用 1.1中介者模式的例子 以泡泡堂游戏为例,先定义一个玩家构造函数 ...

  5. ECshop lib_base.php on line 1241 错误解决方法

    ECSHOP做的一个网站,突然报这个错误,整个网站打不开,后来找了很久,终于找到这个方法,亲测可用 Notice: Undefinedvariable: data in D:\wwwroot\KISS ...

  6. SecureCRT学习之道:用SecureCRT来上传和下载数据

    今天才知道,原来SecureCRT可以使用linux下的zmodem协议来快速的传送文件,而且还使用非常方便哦,我还傻傻的找其他软件来sftp,笨死了:(你只要设置一下上传和下载的默认目录就行opti ...

  7. Linux应用环境实战05:在Ubuntu 14.10中借用Windows的字体 (转)

    阅读目录 设置系统字体 安装微软的英文字体 查看系统的配置文件 借用Windows的字体 编写配置文件 在前一篇随笔中,我详细讨论了字体的分类及用途,也以Fedora 20为例,展示了字体配置的思路和 ...

  8. Linux查看进程线程个数

    1.根据进程号进行查询: # pstree -p 进程号 # top -Hp 进程号 2.根据进程名字进行查询: # pstree -p `ps -e | grep server | awk '{pr ...

  9. 强联通块tarjan算法

    http://poj.org/problem?id=1236第一问:需要几个学校存在软件,才能通过传递,使得所有的学校都有软件 用tarjan算法求出强联通分量后,将每个联通分量缩成一个点,那么问题1 ...

  10. SAP ABAP计划 SY-REPID与SY-CPROG差异

    首先,它的两个解释   sy-repid is the name of the current program.  "当前程序的程序名                             ...