将边排序后dp一下就可以了。

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define clr(x,c) memset(x,c,sizeof(x))
int read(){
int x=0;char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) x=x*10+c-'0',c=getchar();
return x;
}
const int nmax=5e4+5;
const int inf=0x7f7f7f7f;
struct node{
int u,v,d;
node(int u,int v,int d):u(u),v(v),d(d){};
node(){};
bool operator<(const node&rhs)const{
return d<rhs.d;}
};
node ns[nmax];
void maxs(int &a,int b){
if(a<b) a=b;
}
int g[nmax],dp[nmax];
int main(){
int n=read(),m=read(),u,v,d;
rep(i,1,m) ns[i].u=read(),ns[i].v=read(),ns[i].d=read();
sort(ns+1,ns+m+1);
int last=0;
rep(i,1,m){
if(i==m||ns[i].d<ns[i+1].d){
rep(j,last+1,i) g[ns[j].u]=dp[ns[j].u],g[ns[j].v]=dp[ns[j].v];
rep(j,last+1,i){
maxs(dp[ns[j].u],g[ns[j].v]+1);
maxs(dp[ns[j].v],g[ns[j].u]+1);
}
last=i;
}
}
int ans=0;
rep(i,0,n-1) maxs(ans,dp[i]);
printf("%d\n",ans);
return 0;
}

  

题目来源: Codility
基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题
 收藏
 关注
一个无向图,可能有自环,有重边,每条边有一个边权。你可以从任何点出发,任何点结束,可以经过同一个点任意次。但是不能经过同一条边2次,并且你走过的路必须满足所有边的权值严格单调递增,求最长能经过多少条边。
 
 
以此图为例,最长的路径是:
3 -> 1 -> 2 -> 3 -> 2 或
3 -> 1 -> 2 -> 3 -> 4 长度为4。
Input
第1行:2个数N, M,N为节点的数量,M为边的数量(1 <= N <= 50000, 0 <= M <= 50000)。节点编号为0 至 N - 1。
第2 - M + 1行:每行3个数S, E, W,表示从顶点S到顶点E,有一条权值为W的边(0 <= S, E <= N - 1, 0 <= W <= 10^9)。
Output
输出最长路径的长度。
Input示例
6 8
0 1 4
1 2 3
1 3 2
2 3 5
3 4 6
4 5 6
5 0 8
3 2 7
Output示例
4

51nod1274 最长递增路径的更多相关文章

  1. 题解 [51nod1274] 最长递增路径

    题面 解析 这题一眼DP啊. 然而想了半天毫无思路. 后来看题解后发现可以按边权的大小顺序DP. 将边权从小到大排序,对于权值相同的边分为一组. 设\(f[i][0]\)表示经过当前权值的边后到达\( ...

  2. 【题解】最长递增路径 [51nod1274]

    [题解]最长递增路径 [51nod1274] 传送门:最长递增路径 \([51nod1274]\) [题目描述] 一个可能有自环有重边的无向图,每条边都有边权.输入两个整数 \(n,m\) 表示一共 ...

  3. [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

  4. [Swift]LeetCode329. 矩阵中的最长递增路径 | Longest Increasing Path in a Matrix

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

  5. Leetcode 329.矩阵中的最长递增路径

    矩阵中的最长递增路径 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例 1: 输入: n ...

  6. Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)

    Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩 ...

  7. LeetCode. 矩阵中的最长递增路径

    题目要求: 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例: 输入: nums = [ ...

  8. Java实现 LeetCode 329 矩阵中的最长递增路径

    329. 矩阵中的最长递增路径 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例 1: ...

  9. 51nod最长递增路径:(还不错的图)

    一个无向图,可能有自环,有重边,每条边有一个边权.你可以从任何点出发,任何点结束,可以经过同一个点任意次.但是不能经过同一条边2次,并且你走过的路必须满足所有边的权值严格单调递增,求最长能经过多少条边 ...

随机推荐

  1. Random的用法

    import java.util.Random; public class RandomTest { public static void main(String[] args) { Random r ...

  2. UVA 562 Dividing coins (01背包)

    题意:给你n个硬币,和n个硬币的面值.要求尽可能地平均分配成A,B两份,使得A,B之间的差最小,输出其绝对值.思路:将n个硬币的总价值累加得到sum,   A,B其中必有一人获得的钱小于等于sum/2 ...

  3. UITableView中cell的圆角(第一个和最后一个)

    , , _width, _height)];     ;     view.clipsToBounds = YES;          _viewDetal = [[UIView alloc]init ...

  4. ZOJ题目分类

    ZOJ题目分类初学者题: 1001 1037 1048 1049 1051 1067 1115 1151 1201 1205 1216 1240 1241 1242 1251 1292 1331 13 ...

  5. DevExpress TreeList 那些事儿

    1:TreeList绑定数据源 当我们给予TreeList 的 parentFieldName 和 KeyFieldName 两个属性之后 会自动的生成树结构. 1 var sql = @" ...

  6. Visual Studio 常用快捷键 (二)

    想不到上一篇 [Visual Studio 常用快捷键] 受这么多人的欢迎.看来大家对Visual Studio的用法非常感兴趣. 接下来我准备写一个 “Visual Studio使用技巧 ” 一个系 ...

  7. MyEclipse — Maven+Spring+Struts+Hibernate 整合 [学习笔记-2]

    引入Spring 修改 pox.xml 文件 添加jar包引用 <!-- spring3 --> <dependency> <groupId>org.springf ...

  8. CoreData的简单使用(一)数据库的创建

    iOS有多种数据持久化得方式 plist文件(属性列表) preference(偏好设置,NSUserDefaults) NSKeyedArchiver(归档,用的不多) SQLite 3 (需要导入 ...

  9. 多项式求ln,求exp,开方,快速幂 学习总结

    按理说Po姐姐三月份来讲课的时候我就应该学了 但是当时觉得比较难加上自己比较懒,所以就QAQ了 现在不得不重新弄一遍了 首先说多项式求ln 设G(x)=lnF(x) 我们两边求导可以得到G'(x)=F ...

  10. weka平台

    weka平台界面简介 纵向排列的四个主要功能 1.探索(写自己的代码) 2.实验(比较算法) 3.可视化 4.命令行 1.探索 先将weka-src.jar文件解压到一个文件夹 将文件夹导入到Elip ...