hdu 1224 Free DIY Tour

某小朋友给的DP题。

求从1然后再到达1值的最大,给出路径了。只能有小节点到大节点的路。

一开始感觉,这不就是FLOYD 么 = =。。不过需要输出路径,我还没写过floyd记录路径的,只写过dijkstra的。。。

然后往DP方面想,类似数字三角形了都,从下往上推,注意更新的时候一定是更新可以到达终点的。

#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>

using namespace std;

const int MAX = 105;
int dp[MAX];
int a[MAX];
bool map[MAX][MAX];
int pre[MAX];
bool f[MAX];
int main()
{
	int ncases,n,m,x,y,ind = 1;
	
	scanf("%d",&ncases);
	
	while( ncases-- )
	{
		if( ind != 1 )
			printf("\n");
		memset(dp,0,sizeof(dp));
		memset(map,false,sizeof(map));
		memset(pre,-1,sizeof(pre));
		memset(f,false,sizeof(f));
		scanf("%d",&n);
		for(int i=1; i<=n; i++)
			scanf("%d",&a[i]);
		scanf("%d",&m);
		while( m-- )
		{
			scanf("%d%d",&x,&y);
			map[x][y] = true;
			if( y == n+1 ) f[x] = true;
		}
		f[n+1] = true; dp[n+1] = 0;
		for(int i=n; i>0; i--)
			for(int k=i+1; k<=n+1; k++)
				if( map[i][k] && f[k] )
					if( dp[i] < dp[k] + a[i] )
					{
						f[i] = true;
						dp[i] = dp[k] + a[i];
						pre[i] = k;
					}

		printf("CASE %d#\n",ind++);
		printf("points : %d\n",dp[1]);
		printf("circuit : 1");

		queue<int> q;
		y = 1;
		while( pre[y] != -1 )
		{
			q.push(pre[y]);
			y = pre[y];
		}
		while( !q.empty() )
		{
			x = q.front();
			if( x == n+1 ) x = 1;
			printf("->%d",x);
			q.pop();
		}
		printf("\n");
	}

return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值