Question: n vertices, m edges, each edge has a weight value. Find a path with the largest number of edges, and the length of the output path.
Solution: Sort edge weights from small to large, and traverse them from large to small. DP [u] indicates the maximum length of the strictly incrementing path that can be formed from U. DP [u] = max (DP [u], DP [v] + 1). Because there are duplicate edge weights, we use the DIS array to record them first, to update the duplicate edge weights when they are not repeated.
Code: (non-original)
# Include <iostream> # include <cstdio> # include <cstring> # include <cstdlib> # include <cmath> # include <algorithm> using namespace STD; # define n 100007 struct node {int U, V, W;} edge [3 * n]; int DP [3 * n], DIS [3 * n]; int CMP (node ka, node KB) {return ka. W <kb. w;} int main () {int n, m, I, j; scanf ("% d", & N, & M); for (I = 0; I <m; I ++) scanf ("% d", & edge [I]. u, & edge [I]. v, & edge [I]. w); memset (DP, 0, sizeof (DP); memset (DIS, 0, sizeo F (DIS); sort (edge, edge + M, CMP); edge [M]. W =-1; int ans = 1; int Maxi = 1; for (I = s-1; I> = 0; I --) {int u = edge [I]. u; int v = edge [I]. v; int W = edge [I]. w; Maxi = DP [v] + 1; DIS [u] = max (DIS [u], max (Maxi, DP [u]); ans = max (ANS, maxi); if (I = 0 ||w> edge [I-1]. w) {for (j = I; j <m; j ++) // update the Dp value of the same Edge Weight {DP [edge [J]. u] = dis [edge [J]. u]; If (edge [J]. w! = Edge [J + 1]. W) Break ;}} printf ("% d \ n", ANS); Return 0 ;}
View code