SCU 4427 Miss Zhao's Graph (dp)
2015-04-09 21:15
393 查看
题目链接
However, she doesn't want to be looked down upon by Mr Jiang, who is always trying to laugh at her and call her "little fool".
Therefore, she turns to you for help.
There is a weighted directed graph which has n vertices and m edges. You should find a path with maximum number of edges, and the weight of each edge must be strictly greater than the weight of the provious one.
Print the number of edges in the path.
PS: There may be multiple edges with two nodes and self-loops in this graph.
Then for each case:
The first line contains two integers n,m(2<=n<=3*10^5;1<=m<=min(n*(n-1),3*10^5)).
Then, m lines follow. The i-th line contains three integers:
u,v,w(1<=u,v<=n;1<=w<=10^5) which indicates that there's a directed edge with weight w from vertex u to vertex v.
题意:求一个有向图的边权严格上升的最长路径。
题解:将边排序,然后再dp。用dp[i] 表示到当前的边,以i为终点的最长的路径。但是由于前面的边可能等于当前这条边。所以对于每一个dp[i],我们要记录当前最优值对应的边长,还要记录每个dp[i]的次优值(这个次优值对应的边长要严格小于最优值对应的边长)。代码如下:
4427: Miss Zhao's Graph
Submit your solution Discuss this problem Best solutions |
Time: 1000ms
The Problem
Mr Jiang gives Miss Zhao a problem about graphs. Unfortunately, she is not very good at graph theory.However, she doesn't want to be looked down upon by Mr Jiang, who is always trying to laugh at her and call her "little fool".
Therefore, she turns to you for help.
There is a weighted directed graph which has n vertices and m edges. You should find a path with maximum number of edges, and the weight of each edge must be strictly greater than the weight of the provious one.
Print the number of edges in the path.
PS: There may be multiple edges with two nodes and self-loops in this graph.
Input
The first line of input is the number of test case.Then for each case:
The first line contains two integers n,m(2<=n<=3*10^5;1<=m<=min(n*(n-1),3*10^5)).
Then, m lines follow. The i-th line contains three integers:
u,v,w(1<=u,v<=n;1<=w<=10^5) which indicates that there's a directed edge with weight w from vertex u to vertex v.
Output
Print a single integer. The length of the path.Example Input
2 3 3 1 2 1 2 3 2 3 1 3 6 7 1 2 1 3 2 5 2 4 2 2 5 2 2 6 9 5 4 3 4 3 4
Example Output
3 6
Author
mrxy56
题意:求一个有向图的边权严格上升的最长路径。
题解:将边排序,然后再dp。用dp[i] 表示到当前的边,以i为终点的最长的路径。但是由于前面的边可能等于当前这条边。所以对于每一个dp[i],我们要记录当前最优值对应的边长,还要记录每个dp[i]的次优值(这个次优值对应的边长要严格小于最优值对应的边长)。代码如下:
#include<cstdio> #include<cstring> #include<set> #include<iostream> #include<map> #include<cmath> #include<string> #include<vector> #include<queue> #include<cctype> #include<algorithm> #define inf 0x3fffffff #define nn 310000 #define mod 1000000007 typedef long long LL; using namespace std; int n,m; struct node { int st,en,len; }a[nn]; bool cmp(node x,node y) { return x.len<y.len; } pair<int,int>dp[nn][2]; int main() { int i; int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(i=1;i<=m;i++) { scanf("%d%d%d",&a[i].st,&a[i].en,&a[i].len); } sort(a+1,a+m+1,cmp); for(i=1;i<=n;i++) { dp[i][0]=make_pair(0,-inf); dp[i][1]=make_pair(-inf,-inf); } int u,v; int ans=-inf; for(i=1;i<=m;i++) { u=a[i].st,v=a[i].en; if(dp[u][0].second<a[i].len) { if(dp[u][0].first+1>dp[v][0].first) { if(dp[v][0].second<a[i].len) dp[v][1]=dp[v][0]; dp[v][0]=make_pair(dp[u][0].first+1,a[i].len); } } else { if(dp[u][1].first+1>dp[v][0].first) { if(dp[v][0].second<a[i].len) dp[v][1]=dp[v][0]; dp[v][0]=make_pair(dp[u][1].first+1,a[i].len); } } } for(i=1;i<=n;i++) ans=max(ans,dp[i][0].first); printf("%d\n",ans); } return 0; }
相关文章推荐
- [HDU] 3660 Alice and Bob's Trip -- 树形DP?
- 【poj2081】Recaman's Sequence (动态规划DP)
- ZOJ 3380 Patchouli's Spell Cards(DP,大数)
- 【DP_概率DP专辑】【10、4最新更新】【from zeroclock's blog】
- 【DP_树形DP专辑】【9月9最新更新】【from zeroclock's blog】
- [PKU 2411]Mondriaan's Dream(状压DP)
- HDOJ 1400 & POJ 2411 - Mondriaan's Dream 状态压缩DP
- HDU 4274 Spy's Work(12年长春 树形DP)
- HDU 4274 Spy's Work(树形dp)
- UVa 11795 - Mega Man's Mission 状态压缩dp
- hdu 1160 FatMouse's Speed(水DP)
- zju 1108||hdu 1160 FatMouse's Speed(dp,最长单调序列)
- `process_cache_miss' 错误
- hdu 3576 Elevators in Jiayuan Students' Apartment(DP)
- POJ 2411 Mondriaan's Dream 状态压缩dp
- zoj 1108 FatMouse's Speed(DP)
- JOJ 2190: Mondriaan's Dream (状态压缩DP +DFS)
- 1011. Lenny's Lucky Lott (dp)
- poj 2411/hdu 1400 Mondriaan's Dream 状态压缩dp
- hdoj 1028/poj 2704 Pascal's Travels(记忆化搜索||dp)