您的位置:首页 > 其它

hdu 1224 Free DIY Tour

2011-07-18 18:48 323 查看
某小朋友给的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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: