题目链接: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. Robot Motion - poj 1573

      Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11269   Accepted: 5486 Description A ...

  2. dynamic与var

    dynamic与var示例 var是一种语法省略写法,编译器会根据上下文推断出正确的类型. , , , , , , , }; foreach (var item in scores) { Consol ...

  3. 【入门】创建express项目

    1.创建项目(图解) 2.访问http://localhost:3000/就看到熟悉的页面了 3.查看项目目录     参考文档:http://jingyan.baidu.com/article/92 ...

  4. 将navigationbar的translucent属性设为No后,子控制器视图整体下移问题

    如果不将navigationbar.translucent = YES 会觉得颜色很浅,因为这是半透明状态 若navigationbar.translucent = NO,颜色问题解决,但是子控制器视 ...

  5. 读书笔记-HBase in Action-第三部分应用-(1)OpenTSDB

    OpenTSDB是基于HBase的开源监控系统,能够支持上万规模集群监控和上亿数据点採集. 当中TSDB代表Time Series Database,OpenTSDB在时间序列数据的存储和查询上都做了 ...

  6. LR报错 No buffer space available Try changing the registry value 端口号不够用了

    报错:Action.c(6): Error -27796: Failed to connect to server "10.16.137.8:10035": [10055] No ...

  7. 仿照ArrayList自己生成的MyList对象

    现在需要自己生成一个list集合,基本雷同ArrayList,不使用API的List接口. 实现如下: MyList的代码: public class MyList<T> { privat ...

  8. POJ1850&&POJ1496

    Code Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9236   Accepted: 4405 Description ...

  9. 【BZOJ4785】[Zjoi2017]树状数组 树套树(二维线段树)

    [BZOJ4785][Zjoi2017]树状数组 Description 漆黑的晚上,九条可怜躺在床上辗转反侧.难以入眠的她想起了若干年前她的一次悲惨的OI 比赛经历.那是一道基础的树状数组题.给出一 ...

  10. AOP原理及其实现

       AOP 是 Aspect-Oriented programming 的缩写,中文翻译为面向切面编程,它是OOP(Object-Oriented Programing,面向对象编程)的补充和完善. ...