Codeforces Round 261 Div.2 E Pashmak and Graph --DAG上的DP
来源:互联网 发布:淘宝申诉失败再申述 编辑:程序博客网 时间:2024/06/06 09:11
题意:n个点,m条边,每条边有一个权值,找一条边数最多的边权严格递增的路径,输出路径长度。
解法:先将边权从小到大排序,然后从大到小遍历,dp[u]表示从u出发能够构成的严格递增路径的最大长度。 dp[u] = max(dp[u],dp[v]+1),因为有重复的边权值,所以用dis数组先记录,到不重复时一起更新重复的那些边权。
代码: (非原创)
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <algorithm>using namespace std;#define N 100007struct 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%d",&n,&m); for(i=0;i<m;i++) scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w); memset(dp,0,sizeof(dp)); memset(dis,0,sizeof(dis)); sort(edge,edge+m,cmp); edge[m].w = -1; int ans = 1; int maxi = 1; for(i=m-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++) //更新相同边权的dp值 { dp[edge[j].u] = dis[edge[j].u]; if(edge[j].w != edge[j+1].w) break; } } } printf("%d\n",ans); return 0;}
0 0
- Codeforces Round 261 Div.2 E Pashmak and Graph --DAG上的DP
- Codeforces Round #261 (Div. 2) E. Pashmak and Graph【DP】
- Codeforces Round #261 (Div. 2) E. Pashmak and Graph DP
- Codeforces Round #261 (Div. 2) E. Pashmak and Graph
- Codeforces Round #261 (Div. 2) E. Pashmak and Graph (sorting + dp)
- Codeforces Round #261(Div 2) E Pashmak and Graph(图中严格递增的最长路径、思维)
- Codeforces Round #261 (Div. 2)——Pashmak and Graph
- Codeforces Round #261 (Div. 2)E题(DP:DAG上的最长路)
- Codeforces 459E Pashmak and Graph【Dp】
- codeforces 459E E. Pashmak and Graph(dp)
- codeforces 459E E. Pashmak and Graph(dp)
- Codeforces 459E Pashmak and Graph(dp+贪心)
- Codeforces 459E. Pashmak and Graph (DP)
- Codeforces Round #261 (Div. 2) A. Pashmak and Garden (水题)
- Codeforces Round #261 (Div. 2) A. Pashmak and Garden
- Codeforces Round #261 (Div. 2)B. Pashmak and Flowers
- Codeforces Round #261 (Div. 2)——Pashmak and Buses
- Codeforces Round #261 (Div. 2) C. Pashmak and Buses
- UVALive 6093 Emergency Room --优先队列实现的模拟
- HDU 4865 Peter's Hobby --概率DP
- 树形DP求树的重心 --SGU 134
- HDU 4930 Fighting the Landlords --多Trick,较复杂模拟
- Codeforces Round 261 Div.2 D Pashmak and Parmida's problem --树状数组
- Codeforces Round 261 Div.2 E Pashmak and Graph --DAG上的DP
- UVALive 6269 Digital Clock --枚举,模拟
- UVALive 6656 Watching the Kangaroo --二分
- POJ 2528 Mayor's posters --线段树+离散化
- HDU 4964 Emmet --模拟
- ZOJ 2301 / HDU 1199 Color the Ball 离散化+线段树区间连续最大和
- POJ 1151 / HDU 1542 Atlantis 线段树求矩形面积并
- UVA 11983 Weird Advertisement --线段树求矩形问题
- HDU 1828 / POJ 1177 Picture --线段树求矩形周长并