51nod 1274 最长递增路径(图上的dp)
来源:互联网 发布:禁忌的边界线动作数据 编辑:程序博客网 时间:2024/05/16 08:08
一个无向图,可能有自环,有重边,每条边有一个边权。你可以从任何点出发,任何点结束,可以经过同一个点任意次。但是不能经过同一条边2次,并且你走过的路必须满足所有边的权值严格单调递增,求最长能经过多少条边。
以此图为例,最长的路径是:
3 -> 1 -> 2 -> 3 -> 2 或
3 -> 1 -> 2 -> 3 -> 4 长度为4。
Input
第1行:2个数N, M,N为节点的数量,M为边的数量(1 <= N <= 50000, 0 <= M <= 50000)。节点编号为0 至 N - 1。
第2 - M + 1行:每行3个数S, E, W,表示从顶点S到顶点E,有一条权值为W的边(0 <= S, E <= N - 1, 0 <= W <= 10^9)。
Output
输出最长路径的长度。
Input示例
6 8
0 1 4
1 2 3
1 3 2
2 3 5
3 4 6
4 5 6
5 0 8
3 2 7
Output示例
4
代码:
#include <map>#include <set>#include <stack>#include <queue>#include <cmath>#include <string>#include <vector>#include <cstdio>#include <cctype>#include <cstring>#include <sstream>#include <cstdlib>#include <iostream>#include <algorithm>#pragma comment(linker,"/STACK:102400000,102400000")using namespace std;#define MAX 100005#define MAXN 1000005#define maxnode 105#define sigma_size 30#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define lrt rt<<1#define rrt rt<<1|1#define middle int m=(r+l)>>1#define LL long long#define ull unsigned long long#define mem(x,v) memset(x,v,sizeof(x))#define lowbit(x) (x&-x)#define pii pair<int,int>#define bits(a) __builtin_popcount(a)#define mk make_pair#define limit 10000//const int prime = 999983;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f;const double pi = acos(-1.0);const double inf = 1e18;const double eps = 1e-8;const LL mod = 1e9+7;const ull mx = 133333331;/*****************************************************/inline void RI(int &x) { char c; while((c=getchar())<'0' || c>'9'); x=c-'0'; while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0'; }/*****************************************************/struct Edge{ int v,next,c;}edge[MAX];int head[MAX];int tot;int dp[MAX];void init(){ mem(head,-1); tot=0;}void add_edge(int a,int b,int c){ edge[tot]=(Edge){b,head[a],c}; head[a]=tot++;}void dfs(int u,int id){ dp[id]=1; for(int i=head[u];i!=-1;i=edge[i].next){ int v=edge[i].v; if(edge[i].c<=edge[id].c) continue; if(!dp[i]) dfs(v,i); dp[id]=max(dp[id],dp[i]+1); }}int main(){ //freopen("in.txt","r",stdin); int n,m; cin>>n>>m; init(); for(int i=0;i<m;i++){ int a,b,c; scanf("%d%d%d",&a,&b,&c); add_edge(a,b,c); add_edge(b,a,c); } mem(dp,0); for(int i=0;i<tot;i++){ if(!dp[i]) dfs(edge[i].v,i); } int ans=0; for(int i=0;i<tot;i++){ ans=max(ans,dp[i]); } cout<<ans<<endl; return 0;}
0 0
- 51nod 1274 最长递增路径(图上的dp)
- 51nod 1274 最长递增路径【dp】
- 51nod 1274 最长递增路径(dp)
- 【DP】51Nod 1274 最长递增路径
- 51nod 1274 最长递增路径【DP】
- 51nod 1274 最长递增路径[dp]
- [DP] 51 Nod 1274——最长递增路径
- 51NOD 1274 最长递增路径
- 51nod-1274 最长递增路径
- 51nod 1274 最长递增路径
- 51Nod-1274-最长递增路径
- 51NOd 1134 最长递增子序列(dp)
- 51nod 1134 最长递增子序列(dp)
- 51nod 1134 最长递增子序列 dp(经典)
- 51nod 1218 最长递增子序列 V2 [dp]
- 51nod 1134 最长递增子序列 DP
- 51Nod 1376 最长递增子序列的数量(dp+树状数组)
- 51nod 1376 最长递增子序列的数量(dp、CDQ分治 | BIT)
- Linux下函数指针简单样例
- Spring中ApplicationContext和beanfactory区别 .
- 腾讯云CMQ消息队列测试
- Lua5.3 数学函数
- 提交表单信息常用格式验证,防止冒泡,移动端避免键盘挡输入框,取消回退(持续更新)
- 51nod 1274 最长递增路径(图上的dp)
- centos vi 使用
- UIDocumentInterRactionController 使用时的注意事项(其它应用打开)
- 【广告】win10 uwp 水印图床 含代码
- php的常用的正则判断
- qemu vl.c源码学习
- 阿里云RDS MySQL主机配置支持emoji
- 自定义微信公众号菜单一二级选项(一)
- VS2010 c++ 错误的处理方法