Codeforces 459E Pashmak and Graph:dp + 贪心
题目链接:http://codeforces.com/problemset/problem/459/E
题意:
给你一个有向图,每条边有边权。
让你找出一条路径,使得这条路径上的边权严格递增。
问你这样的路径最长有多长。
题解:
先将所有边按边权从小到大排序,以保证边权递增。
表示状态:
dp[i] = max len
表示以点i为终点时的最长路径长度。
找出答案:
ans = max dp[i]
如何转移:
枚举每条边e[i],则有:
dp[e[i].t] = max(dp[e[i].t], dp[e[i].s]+1)
但是要注意,当枚举一些边的边权相同时,直接更新dp[e[i].t]是不行的。
比如一条链上的边权均相同的情况。
所以当边权相同时,先暂时将新的dp[e[i].t]存到f数组中,等这些边权相同的边枚举完之后,再用f数组更新dp。
边界条件:
set dp & f = 0
AC Code:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#define MAX_N 300005 using namespace std; struct Edge
{
int s;
int t;
int len;
Edge(int _s,int _t,int _len)
{
s=_s;
t=_t;
len=_len;
}
Edge(){}
friend bool operator < (const Edge &a,const Edge &b)
{
return a.len<b.len;
}
}; int n,m;
int ans=;
int f[MAX_N];
int dp[MAX_N];
Edge edge[MAX_N]; void read()
{
scanf("%d%d",&n,&m);
int a,b,v;
for(int i=;i<m;i++)
{
scanf("%d%d%d",&a,&b,&v);
edge[i]=Edge(a,b,v);
}
} void work()
{
sort(edge,edge+m);
memset(dp,,sizeof(dp));
memset(f,,sizeof(f));
int pos=;
for(int i=;i<m;i++)
{
Edge temp=edge[i];
f[temp.t]=max(f[temp.t],dp[temp.s]+);
ans=max(ans,f[temp.t]);
if(i==m- || temp.len!=edge[i+].len)
{
while(pos<=i)
{
dp[edge[pos].t]=f[edge[pos].t];
pos++;
}
}
}
printf("%d\n",ans);
} int main()
{
read();
work();
}
Codeforces 459E Pashmak and Graph:dp + 贪心的更多相关文章
- Codeforces 459E Pashmak and Graph(dp+贪婪)
题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边依 ...
- CodeForces - 459E Pashmak and Graph[贪心优化dp]
E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces 459E Pashmak and Graph
http://www.codeforces.com/problemset/problem/459/E 题意: 给出n个点,m条边的有向图,每个边有边权,求一条最长的边权上升的路径的长度. 思路:用f存 ...
- codeforces 459E E. Pashmak and Graph(dp+sort)
题目链接: E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabytes input st ...
- Codeforces Round #261 (Div. 2) E. Pashmak and Graph DP
http://codeforces.com/contest/459/problem/E 不明确的是我的代码为啥AC不了,我的是记录we[i]以i为结尾的点的最大权值得边,然后wa在第35 36组数据 ...
- CF459E Pashmak and Graph (DP?
Codeforces Round #261 (Div. 2) E - Pashmak and Graph E. Pashmak and Graph time limit per test 1 seco ...
- codeforces 459E
codeforces 459E E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabyte ...
- ACM - 最短路 - CodeForces 295B Greg and Graph
CodeForces 295B Greg and Graph 题解 \(Floyd\) 算法是一种基于动态规划的算法,以此题为例介绍最短路算法中的 \(Floyd\) 算法. 我们考虑给定一个图,要找 ...
- cf459E Pashmak and Graph
E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabytes input standard ...
随机推荐
- 篇三、开发前知识补充:Android的长度单位和屏幕分辨率,这个也是转载~~
这篇文章有点早,不过很实用.单位的实用看最后的红色标注的部分. 屏幕分辨率基础 1.术语和概念 术语 说明 备注 Screen size(屏幕尺寸) 指的是手机实际的物理尺寸,比如常用的2.8英寸,3 ...
- maven 依赖文件 pom.xml 编译 mvn compile 运行 不用mvn exec:java -Dexec.mainClass="hello.HelloWorld" 打成jar包 mvn package mvn install http://blog.csdn.net/yaya1943/article/details/48464371
使用maven编译Java项目 http://blog.csdn.net/yaya1943/article/details/48464371 使用"mvn clean"命令清除编 ...
- linux 上安装apache 出现 configure: error: APR not found. Please read the documentation错误
今日编译apache时出错: #./configure --prefix……检查编辑环境时出现: checking for APR... noconfigure: error: APR not fou ...
- SharePoint 2013的100个新特性 免费电子书下载
简介:这本电子书对SharePoint 2013的100个新特性和改进的功能提供了一个简短的说明,这些功能分为以下几类: 1. SharePoint 2013内容管理 2. SharePoint 20 ...
- lua注册函数
#include <stdio.h> #include <math.h> #define MAX_COLOR 255 extern "C" { #inclu ...
- Jmeter 03 Jmeter脚本开发
JMeter 工作区介绍 JMeter Http 协议录制 JMeter 脚本调测 JMeter 关联 JMeter 参数化 JMeter 检查点 JMeter 事务 JMeter 集合点 JMete ...
- vue项目在APP禁止页面缩放
veu-cli自动生成的项目中,index.html中meta 标签内容如下,放在手机上浏览 是可以放大缩小的<meta name="viewport" content=&q ...
- socket java 实例
简单的java socket 示例 一.搭建服务器端 a).创建ServerSocket对象绑定监听端口. b).通过accept()方法监听客户端的请求. c).建立连接后,通过输入输出流读取客户端 ...
- EventLoop(netty源码死磕4)
精进篇:netty源码 死磕4-EventLoop的鬼斧神工 目录 1. EventLoop的鬼斧神工 2. 初识 EventLoop 3. Reactor模式回顾 3.1. Reactor模式的组 ...
- struts2 封装获取表单数据的方式
一.属性封装 1.在action中设置成员变量,变量名与表单中的name属性值相同 2.生成变量的set方法 实例 获取用户输入的用户名和密码 jsp页面 java代码 二.模型驱动(常用) 1.ac ...