题目链接: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+贪婪)的更多相关文章

  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. HTML学习笔记之中的一个(input文件选择框的封装)

    方式一:直接透明隐藏 .file_button_container,.file_button_container input {background: transparent url(./img/BT ...

  2. redhat linux 5上创建本地yum源

    1.挂载光驱 [root@rh5rac1 ~]#mkdir -p /mnt/cdrom [root@rh5rac1 ~]#mount /dev/cdrom /mnt/cdrom 2.将redhat光盘 ...

  3. Blob API及问题记录

    接上一篇<js创建下载文件>, 记录核心部分 Blob 的API, >>传送门 , 同时说下使用过程中碰到的一个问题. 先说问题: 用Blob创建后缀为.sql的文件, 内容是 ...

  4. 计算机本科/硕士找offer的总结 节选

    在这样的目标定位下,我的求职历程从2006年10月8日开始,到2007年1月10日正式结束,一共持续了3个月的时间.整个过程可以分为三个阶段:第一阶段:2006年10月份 找工作刚刚开始的阶段,什么都 ...

  5. /etc/sysconfig/network-scripts/ifcfg-eth0

    以下各值常见于所有的基本配置文件中:* DEVICE=name,这里name是物理设备的名字(动态分配的PPP设备应当除外,它的名字是“逻辑名”.* IPADDR=addr, 这里addr是IP地址. ...

  6. STM32F103 TIM1输出PWM设置

    //TIM1 分频 #define TIM1_DIV1 (1-1) #define TIM1_DIV2 (2-1) #define TIM1_DIV4 (4-1) #define TIM1_DIV8 ...

  7. [C++] WinAES问题

    WinAES这是一个很好windows CAPI包. 假设C++项目需求和java程序aes加密和通信的解密,然后WinAES代码是有问题. java的aes默认代码未设置IV和用途ECB模式. 因此 ...

  8. php:修改NetBeans默认字体

    在Netbeans中由于使用了Swing进行开发,所以其中界面的字体也是由Java虚拟机进行配置而不是随操作系统的.在安装完Netbeans后默认的字体大小是11px.而在Windows下的宋体最小支 ...

  9. Java 8 时间日期库的20个使用示例

    java 8是如何处理时间及日期的 有人问我学习一个新库的最佳途径是什么?我的回答是,就是在实际项目中那样去使用它.在一个真实的项目中会有各种各样的需求,这会促使开发人员去探索和研究这个新库.简言之, ...

  10. 第1周 SQL Server 如何执行一个查询

    原文:第1周 SQL Server 如何执行一个查询 大家好,欢迎来到第1周的SQL Server性能调优培训.在我们进入SQL Server性能调优里枯燥难懂的细节内容之前,我想通过讲解SQL Se ...