标签:codeforces dp
给出n个点,m条边的有向图,每个边有边权,求一条最长的边权上升的路径的长度。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define MAX 300007
using namespace std;
int n,m;
int dp[MAX],g[MAX];
struct Edge
{
int u,v,w;
bool operator < ( const Edge& a ) const
{
return w < a.w;
}
}e[MAX];
int main ( )
{
while ( ~scanf ( "%d%d" , &n , &m ) )
{
for ( int i = 0 ; i < m ; i++ )
scanf ( "%d%d%d" , &e[i].u , &e[i].v , &e[i].w );
sort ( e , e+m );
int t = 0;
e[m].w = -1;
int ans = 0;
for ( int i = 0 ; i < m ; i++ )
{
int v = e[i].v;
int u = e[i].u;
int w = e[i].w;
dp[i] = g[e[i].u]+1;
if ( e[i].w != e[i+1].w )
{
for ( int j = t ; j <= i ; j++ )
g[e[j].v] = max ( g[e[j].v] , dp[j] );
t = i+1;
}
ans = max ( ans , dp[i] );
}
printf ( "%d\n" , ans );
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
codeforces 459E E. Pashmak and Graph(dp)
标签:codeforces dp
原文地址:http://blog.csdn.net/qq_24451605/article/details/48877073