nssl1446-小智的旅行【dp】
生活随笔
收集整理的這篇文章主要介紹了
nssl1446-小智的旅行【dp】
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
正題
題目大意
求一條最大的權(quán)值嚴(yán)格上升的路徑。
解題思路
將邊權(quán)排序,然后從fxf_xfx?轉(zhuǎn)移到fy+1f_y+1fy?+1即可,要注意的是因?yàn)閲?yán)格上升,所以此次轉(zhuǎn)移用的fff不能是相同權(quán)值轉(zhuǎn)移時(shí)轉(zhuǎn)移的。
codecodecode
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std; const int N=5e5+10; struct node{int x,y,w; }a[N]; queue<pair<int,int> > q; int n,m,f[N],ans; bool cmp(node x,node y) {return x.w<y.w;} int main() {scanf("%d%d",&n,&m);for(int i=1;i<=m;i++){scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w);a[i].x++;a[i].y++;}sort(a+1,a+1+m,cmp);for(int i=1;i<=m;i++){if(a[i].w!=a[i-1].w){while(!q.empty()){int x=q.front().first,w=q.front().second;f[x]=max(f[x],w);q.pop();}}q.push(make_pair(a[i].y,f[a[i].x]+1));q.push(make_pair(a[i].x,f[a[i].y]+1));}while(!q.empty()){int x=q.front().first,w=q.front().second;f[x]=max(f[x],w);q.pop();}for(int i=1;i<=n;i++)ans=max(ans,f[i]);printf("%d",ans); } 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的nssl1446-小智的旅行【dp】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 古代亭长是什么官
- 下一篇: nssl1447-小智的糖果【dp】