CF459E Pashmak and Graph【貪心+dp】

來源:互聯網
上載者:User

標籤:codeforces   algorithm   dp   

題目:CF459E Pashmak and Graph


題意:給出n個點,m條邊的圖,然後讓你每次只能向權值更大邊走,求最大的邊數。可以走多次


分析:由於點比較多,我們可以先對權值從小到大排序,然後從小的開始,更新它的到的節點的值為前一個節點值+1,但是還會出現權值相等成環的情況,所以我們可以對相等的先不更新,儲存起來,等相等的結束了再更新。


代碼:

#include<cstdio>#include<algorithm>using namespace std;const int N = 305000;int dp[N],g[N];struct edge{int u,v,w;bool operator <(const edge& obj) const{return w<obj.w;}}e[300010];int main(){int n,m;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,ans=0;for(int i=0;i<m;i++){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);return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.