Codeforces 459E. Pashmak and Graph (DP)
2017-05-28 14:57
513 查看
题目描述
传送门
题目大意:求边权单调递增的最长路题解
将所有的边按照边权从小到大排序。f[i]表示到达i点的最长合法路径的长度。对于相同边权的一起处理,因为之前加入的都是比当前边权小的边,所以一定可以连接起来,直接用起点更新终点的答案即可。
代码
#include<iostream> #include<cstdio> #include<cmath> #include<algorithm> #include<cstring> #define N 300005 using namespace std; struct data{ int x,y,val; }e ; int n,m,f ,g ; int cmp(data a,data b) { return a.val<b.val; } int main() { freopen("a.in","r",stdin); scanf("%d%d",&n,&m); for (int i=1;i<=m;i++) scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].val); sort(e+1,e+m+1,cmp); int mx=e[m].val; int l=1; for (int i=1;i<=mx;i++) { int t=l; while (e[l].val==i&&l<=m) { g[e[l].y]=max(g[e[l].y],f[e[l].x]+1); l++; } for (int j=t;j<l;j++) f[e[j].y]=max(f[e[j].y],g[e[j].y]),g[e[j].y]=0; } int ans=0; for (int i=1;i<=n;i++) ans=max(ans,f[i]); printf("%d\n",ans); }
相关文章推荐
- CodeForces - 459E - Pashmak and Graph
- Codeforces Round 261 Div.2 E Pashmak and Graph --DAG上的DP
- Codeforces Round 261 Div.2 E Pashmak and Graph --DAG上的DP
- Codeforces Round #261 (Div. 2) E. Pashmak and Graph【DP】
- codeforces 295B B. Greg and Graph(floyd+dp)
- Codeforces 340E Iahub and Permutations【思维+错排Dp】
- Round 6 B - Greg and Graph CodeForces - 295Div.1B - Floyd && DP
- codeforces 713C C. Sonya and Problem Wihtout a Legend(dp)(将一个数组变成严格单增数组的最少步骤)
- codeforces 366 C Dima and Salad dp
- Codeforces 467C George and Job(dp)
- codeforces 366D Dima and Trap Graph 题解(搜索+剪枝or二分+枚举)
- CodeForces 296 B.Yaroslav and Two Strings(dp)
- 【codeforces 734E】Anton and Tree【缩点+DP】
- CodeForces 314 B.Sereja and Periods(dp)
- 【CodeForces 261B】Maxim and Restaurant(DP,期望)
- codeforces 467C George and Job dp
- Codeforces 318 div2.D Bear and Blocks(dp,贪心)
- Codeforces 811C Vladik and Memorable Trip【Dp】
- 【DP+二分】[CodeForces - 713D] Animals and Puzzle
- 【CodeForces 624C】Graph and String