2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 H (简单DP)

In this winter holiday, Bob has a plan for skiing at the mountain resort.

This ski resort has MM different ski paths and NN different flags situated at those turning points.

The ii-th path from the S_iSi-th flag to the T_iTi-th flag has length L_iLi.

Each path must follow the principal of reduction of heights and the start point must be higher than the end point strictly.

An available ski trail would start from a flag, passing through several flags along the paths, and end at another flag.

Now, you should help Bob find the longest available ski trail in the ski resort.

Input Format

The first line contains an integer TT, indicating that there are TT cases.

In each test case, the first line contains two integers NN and MM where 0 < N \leq 100000<N10000and 0 < M \leq 1000000<M100000 as described above.

Each of the following MM lines contains three integers S_iSiT_iTi, and L_i~(0 < L_i < 1000)Li (0<Li<1000)describing a path in the ski resort.

Output Format

For each test case, ouput one integer representing the length of the longest ski trail.

样例输入
1
5 4
1 3 3
2 3 4
3 4 1
3 5 2
样例输出
6

简单dp只要把每个子状态的最优求出就行


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
#define maxn 100100

struct No
{
	vector<int> len;
	vector<int> next;
 } ;
 No node[maxn];
int ans[maxn];
int cal(int a){
	if(ans[a] != 0)return ans[a];
	for(int i=0; i<node[a].len.size(); i++){
		ans[a] = max( (node[a].len[i]+cal(node[a].next[i])), ans[a]);
	}
	return ans[a];
}

int main()
{
	int T;
	scanf("%d", &T);
	while(T--){
		memset(node, 0, sizeof(node));
		memset(ans, 0, sizeof(ans));
		
		int m, n;
		
		scanf("%d%d", &n, &m);
		int temp1, temp2, temp3;
		for(int i=0; i<m; i++){
			scanf("%d%d%d", &temp1, &temp2, &temp3);
			node[temp1].next.push_back(temp2);
			node[temp1].len.push_back(temp3);
		}
		int res = 0;
		for(int i=0; i<n; i++){
			res = max(cal(i), res);
		}
		cout<<res<<endl;
	}
   return 0;
}




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_我走路带风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值