Codeforces Round 261 Div.2 E Pashmak and Graph --DAG上的DP
2014-11-27 18:43
537 查看
题意:n个点,m条边,每条边有一个权值,找一条边数最多的边权严格递增的路径,输出路径长度。
解法:先将边权从小到大排序,然后从大到小遍历,dp[u]表示从u出发能够构成的严格递增路径的最大长度。 dp[u] = max(dp[u],dp[v]+1),因为有重复的边权值,所以用dis数组先记录,到不重复时一起更新重复的那些边权。
代码: (非原创)
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
View Code
解法:先将边权从小到大排序,然后从大到小遍历,dp[u]表示从u出发能够构成的严格递增路径的最大长度。 dp[u] = max(dp[u],dp[v]+1),因为有重复的边权值,所以用dis数组先记录,到不重复时一起更新重复的那些边权。
代码: (非原创)
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#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%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; }
View Code
相关文章推荐
- Codeforces Round 261 Div.2 E Pashmak and Graph --DAG上的DP
- Codeforces Round 261 Div.2 D Pashmak and Parmida&#39;s problem --树状数组
- Codeforces Round #261 (Div. 2)——Pashmak and Buses
- Codeforces Round #261 (Div. 2)B. Pashmak and Flowers(容易)
- Round 6 B - Greg and Graph CodeForces - 295Div.1B - Floyd && DP
- Codeforces Round #261 (Div. 2)459D. Pashmak and Parmida's problem(求逆序数对)
- Codeforces Round #261 (Div. 2) A. Pashmak and Garden【水】
- Codeforces Round #261 (Div. 2) C. Pashmak and Buses
- Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- Codeforces Round #430(Div.2)Problem C Ilya And The Tree(DFS)
- Codeforces Round #235 (Div. 2) / 410D Roman and Numbers (带有整除性质的数位DP)
- Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】
- Codeforces Round #293 (Div. 2) D. Ilya and Escalator(概率DP)
- Codeforces Round #410 (Div.2) C.Mike and gcd problem-数学
- Codeforces Round #382 (Div. 1) C. Ostap and Tree(树形DP)
- Codeforces Round #294 (Div. 2) D. A and B and Interesting Substrings [dp 前缀和 ]
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- Codeforces Round #299 (Div. 1)C. Tavas and Pashmaks (凸壳)
- Codeforces Round #299 (Div. 1)C. Tavas and Pashmaks