https://codility.com/programmers/challenges/magnesium2014

图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况;每个节点记录以该节点结束的最长路径,这样加入新的路径时去更新。注意路径是双向的~

#include <vector>
#include <algorithm>
using namespace std; struct Road {
int start;
int end;
int val;
}; bool cmp(const Road &a, const Road &b) {
return a.val < b.val;
} int solution(int N, vector<int> &A, vector<int> &B, vector<int> &C) {
int M = A.size();
vector<Road> roads(M);
for (int i = 0; i < M; i++) {
roads[i].start = A[i];
roads[i].end = B[i];
roads[i].val = C[i];
}
sort(roads.begin(), roads.end(), cmp);
vector<pair<int, int>> dp(N); // first: the longest length ends with this node; second: the last path val to this node;
int result = 0;
for (int i = 0; i < M; i++) {
int x2y_len = dp[roads[i].end].first;
int x2y_val = dp[roads[i].end].second;
if (roads[i].val > dp[roads[i].start].second &&
dp[roads[i].start].first + 1 > dp[roads[i].end].first) {
x2y_len = dp[roads[i].start].first + 1;
x2y_val = roads[i].val;
result = max(x2y_len, result);
}
// the other side
int y2x_len = dp[roads[i].start].first;
int y2x_val = dp[roads[i].start].second;
if (roads[i].val > dp[roads[i].end].second &&
dp[roads[i].end].first + 1 > dp[roads[i].start].first) {
y2x_len = dp[roads[i].end].first + 1;
y2x_val = roads[i].val;
result = max(y2x_len, result);
}
dp[roads[i].end].first = x2y_len;
dp[roads[i].end].second = x2y_val;
dp[roads[i].start].first = y2x_len;
dp[roads[i].start].second = y2x_val;
}
return result;
}

  

*[codility]AscendingPaths的更多相关文章

  1. Codility NumberSolitaire Solution

    1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...

  2. codility flags solution

    How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...

  3. GenomicRangeQuery /codility/ preFix sums

    首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...

  4. *[codility]Peaks

    https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...

  5. *[codility]Country network

    https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...

  6. *[codility]MaxDoubleSliceSum

    https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...

  7. *[codility]Fish

    https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...

  8. *[codility]CartesianSequence

    https://codility.com/programmers/challenges/upsilon2012 求笛卡尔树的高度,可以用单调栈来做. 维持一个单调递减的栈,每次进栈的时候记录下它之后有 ...

  9. [codility]CountDiv

    https://codility.com/demo/take-sample-test/count_div 此题比较简单,是在O(1)时间里求区间[A,B]里面能被K整除的数字,那么就计算一下就能得到. ...

随机推荐

  1. js----对象的创建

    js创建对象的三种方法 在介绍之前一定要弄清楚一个概念,比如var a = new Object(); 这里的a并不是一个对象,而是一个对象的实例. 一.用Json创造 var a = {b:1,c: ...

  2. js正则学习及一些正则集合

    正则中文版详细说明请看中文版w3cschool-----http://www.w3school.com.cn/jsref/jsref_obj_regexp.asp微软正则表达式语言-快速参考:http ...

  3. jQuery插件css3动画模拟confirm弹窗

    相比浏览器自带的alert.confirm,能力所及,我更喜欢所有的东西都是自定义:首先在head标签(当然喜欢其他地方自己看着办)内引入插件样式表和js.<link rel="sty ...

  4. php出现“syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM”错误的一种情况,及解决方法

    PHP中的“syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM”错误,可能是因为美元符号$的误用,看下面一种情况 class Test{         s ...

  5. 植物大战僵尸中文第二版和年度版 游戏分析及delphi源码

    00413184 |. E8 77E30100   |CALL PlantsVs.00431500                  ; 地上的物品00413189 |. 8D7424 10     ...

  6. .NET SDK和下载

    http://blogs.msdn.com/b/dotnet/p/dotnet_sdks.aspx .NET SDK和下载 您可以通过下载.NET框架针对包和软件开发工具包,并使用它们与Visual ...

  7. 移植Oracle procedure 到 postgresql

    1.登录postgresql psql -h 192.168.137.131 -p 5432 postgres satusc@6789#JKL 2.创建用户 CREATE USER name thun ...

  8. COUNT(*),count(1),COUNT(ALL expression),COUNT(DISTINCT expression)

    创建一个测试表 IF OBJECT_ID( 'dbo.T1' , 'U' )IS NOT NULL BEGIN DROP TABLE dbo.T1; END; GO )); GO INSERT INT ...

  9. mysql 配置主从

    1.选择2个ip,1个为主,1个为从:例:主:192.168.12.76 从:192.168.12.772.在192.168.12.76的my.cnf 配置master,添加如下:(红色为添加的内容) ...

  10. iTween基础之Rotate(旋转角度)

    一.基础介绍:二.基础属性 原文地址 :http://blog.csdn.net/dingkun520wy/article/details/50696489 一.基础介绍 RotateTo:旋转游戏物 ...