题目链接:http://codeforces.com/problemset/problem/459/E

题意:

  给你一个有向图,每条边有边权。

  让你找出一条路径,使得这条路径上的边权严格递增。

  问你这样的路径最长有多长。

题解:

  先将所有边按边权从小到大排序,以保证边权递增。

  表示状态:

    dp[i] = max len

    表示以点i为终点时的最长路径长度。

  找出答案:

    ans = max dp[i]

  如何转移:

    枚举每条边e[i],则有:

      dp[e[i].t] = max(dp[e[i].t], dp[e[i].s]+1)

    但是要注意,当枚举一些边的边权相同时,直接更新dp[e[i].t]是不行的。

    比如一条链上的边权均相同的情况。

    所以当边权相同时,先暂时将新的dp[e[i].t]存到f数组中,等这些边权相同的边枚举完之后,再用f数组更新dp。

  边界条件:

    set dp & f = 0

AC Code:

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#define MAX_N 300005 using namespace std; struct Edge
{
int s;
int t;
int len;
Edge(int _s,int _t,int _len)
{
s=_s;
t=_t;
len=_len;
}
Edge(){}
friend bool operator < (const Edge &a,const Edge &b)
{
return a.len<b.len;
}
}; int n,m;
int ans=;
int f[MAX_N];
int dp[MAX_N];
Edge edge[MAX_N]; void read()
{
scanf("%d%d",&n,&m);
int a,b,v;
for(int i=;i<m;i++)
{
scanf("%d%d%d",&a,&b,&v);
edge[i]=Edge(a,b,v);
}
} void work()
{
sort(edge,edge+m);
memset(dp,,sizeof(dp));
memset(f,,sizeof(f));
int pos=;
for(int i=;i<m;i++)
{
Edge temp=edge[i];
f[temp.t]=max(f[temp.t],dp[temp.s]+);
ans=max(ans,f[temp.t]);
if(i==m- || temp.len!=edge[i+].len)
{
while(pos<=i)
{
dp[edge[pos].t]=f[edge[pos].t];
pos++;
}
}
}
printf("%d\n",ans);
} int main()
{
read();
work();
}

Codeforces 459E Pashmak and Graph:dp + 贪心的更多相关文章

  1. Codeforces 459E Pashmak and Graph(dp+贪婪)

    题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边依 ...

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

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

  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. eclipse 开发 scala

    (环境:jdk1.7,scala插件scala-2.1.1.2-site.zip) 1:下载scala插件 http://download.scala-ide.org/sdk/helium/e38/s ...

  2. 【数据挖掘】分类之Naïve Bayes(转载)

    [数据挖掘]分类之Naïve Bayes 1.算法简介 朴素贝叶斯(Naive Bayes)是监督学习的一种常用算法,易于实现,没有迭代,并有坚实的数学理论(即贝叶斯定理)作为支撑. 本文以拼写检查作 ...

  3. 对LCD接口的认识

    LCD接口类型: 1.首先我们以传递的信号类型来区分主要有两大类:- 模拟信号: - VGA: Video Graphics Array- 数字信号 - TTL: Transistor Transis ...

  4. 《HBase in Action》 第二章节的学习总结 ---- HBase基本组成

    准备工作:采用的HBase版本是:CDH4.5,其中的Hadoop版本是:hadoop-2.0.0-cdh4.5.0:HBase版本是:hbase-0.94.6-cdh4.5.0: Hbase的配置文 ...

  5. spring boot配置文件

    1.spring boot通常打成一个jar文件发布,想修改配置文件比较麻烦,但他提供了一种读取外部配置文件的方式.在代码的主类中增加如下代码 System.setProperty("spr ...

  6. 如何落地全球最大 Kubernetes 生产集群

        鲍永成   京东基础架构部技术总监,   DevOps 标准核心编写专家   前言   JDOS 就是京东数据中心操作系统,随着数据中心规模不断的扩大,我们需要对数据中心做综合的考虑.所以一开 ...

  7. 10 Memcached 一致性哈希分布式算法原理与实现[PHP实现]

    <?php header("Content-type:text/html;charset=utf-8"); interface hash{ public function _ ...

  8. CardView的具体使用方法(转)

    转载自:CardView的具体使用方法  因为学习做此记录方便查找使用 今天主要是CardView的用法,CardView是在安卓5.0提出的卡片式控件.首先介绍一下它的配置. 在gradle文件下添 ...

  9. PHP -- 问题

    @.源码安装,最开始,自己通过 ./config  make  make install三步,啥参数也没加,安装好之后,发现/usr/local/php下就一个man文件夹,死都没找到php-fpm之 ...

  10. Java多线程中的竞争条件、锁以及同步的概念

    竞争条件 1.竞争条件: 在java多线程中,当两个或以上的线程对同一个数据进行操作的时候,可能会产生“竞争条件”的现象.这种现象产生的根本原因是因为多个线程在对同一个数据进行操作,此时对该数据的操作 ...