题目链接: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. codeforces #364d As Fast As Possible

    题意:一群学生,要到离这里为l的地方去.有一辆车,车只有k个座位.人和车的速度分别v1,v2,问你所有人到达的最小时间. 思路:数学题.最小时间就是要求所有同学同时到达.每个同学最多上一次车.那么显然 ...

  2. Redis学习笔记-Redis内部数据结构

    Redis内部数据结构 Redis和其他key-value数据库的很大区别是它支持非字符串类型的value值.它支持的value值的类型如下: sds (simple dynamic string) ...

  3. Nginx 配置指令的执行顺序

    在一个 location 中使用 content 阶段指令时,通常情况下就是对应的 Nginx 模块注册该 location 中的“内容处理程序”.那么当一个 location 中未使用任何 cont ...

  4. Currency Exchange - poj 1860

    Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22111   Accepted: 7986 Description Seve ...

  5. 下载某资源文件并加载其中的所有Prefab到场景中

    using System.Collections; using System.Collections.Generic; using UnityEngine; /// <summary> / ...

  6. Linux进程间通信(二) - 消息队列

    消息队列 消息队列是Linux IPC中很常用的一种通信方式,它通常用来在不同进程间发送特定格式的消息数据. 消息队列和之前讨论过的管道和FIFO有很大的区别,主要有以下两点(管道请查阅我的另一篇文章 ...

  7. iOS 平台如何使用 TestFlight 进行 Beta 测试

    使用 TestFlight,你可以向测试人员发布你 App 的 prerelease 版本来收集反馈信息,为将来发布 App 的正式版做准备.现在 TestFlight 是一个可选功能,你也可以不使用 ...

  8. 旋转卡壳求两个凸包最近距离poj3608

    #include <iostream> #include <cmath> #include <vector> #include <string.h> # ...

  9. Pollard-Rho大整数拆分模板

    随机拆分,简直机智. 关于过程可以看http://wenku.baidu.com/link?url=JPlP8watmyGVDdjgiLpcytC0lazh4Leg3s53WIx1_Pp_Y6DJTC ...

  10. 经过两个多月的攻关,终于搞定了live555多线程并稳定压测通过

    live555已经发展了十几年了,不得不钦佩作者坚持不懈的奉献和国外的开源生态环境,live555可以说是大部分的安防从业者的入门之选,尤其是在嵌入式或者Linux系统上,其应用还是蛮广泛的,主要是其 ...