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 ...
随机推荐
- iOS GCD倒计时
GCD倒计时的好处在于不用考虑是否定时器无法释放的问题,runloop的问题,还有精度更加高 使用GCD创建定时器方法 -(void)startCountDown:(NSInteger)maxTime ...
- bzoj3531【SDOI2014】旅行
3531: [Sdoi2014]旅行 Time Limit: 20 Sec Memory Limit: 512 MB Submit: 850 Solved: 433 [Submit][Status ...
- 51系列xdata、idata、data的用法
从数据存储类型来说,8051系列有片内.片外程序存储器,片内.片外数据存储器,片内程序存储器还分直接寻址区和间接寻址类型,分别对应code.data.xdata.idata以及根据51系列特点而设定的 ...
- python3读取BJDA药品经营企业数据
#-*- coding:utf-8 -*- #读取北京FDA的药品经营企业数据 # 20161125 zhangshaohua import re import urllib.request impo ...
- 由浅到深理解ROS(1)
ROS机器人操作系统 ( Robot Operating System 或简称 ROS),可以帮助提高机器人软件的开发效率.ROS能够提供类似传统操作系统的诸多功能,如硬件抽象.底层设备控制.常用功能 ...
- Mac下python连接mysql数据库
一.下载Mysql官方connector驱动 地址:https://dev.mysql.com/downloads/connector/python/ 根据提示安装.dmg文件即可. 二.验证是否安装 ...
- Apache禁止ip访问
网站突然让禁止ip访问,于是就通过配置Apache达到了想要的效果. 我们网站用的是Apache+tomcat集群,所以需要配置虚拟主机,虚拟主机我在这里就不说了,不明白的上网搜搜吧,这里只说禁止ip ...
- 九度OJ 1190:大整数排序 (大数运算、排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3219 解决:1467 题目描述: 对N个长度最长可达到1000的数进行排序. 输入: 输入第一行为一个整数N,(1<=N<=1 ...
- tfboys——tensorflow模块学习(一)
Tensorflow的基本使用 TensorFlow 的特点: 使用图 (graph) 来表示计算任务. 在被称之为 会话 (Session) 的上下文 (context) 中执行图. 使用 tens ...
- java基础入门之数组排序冒泡法
public class ArrayTest03{ /* Name:数组排序,冒泡法 Power by :Stuart Date:2015-4-23*/ public static void main ...