【HDU1224】Free DIY Tour(动态规划DP)

Categories: 数据结构和算法
Tags: ,
Comments: No Comments
Published on: 2011 年 05 月 11 日

Weiwei is a software engineer of ShiningSoft. He has just excellently fulfilled a software project with his fellow workers. His boss is so satisfied with their job that he decide to provide them a free tour around the world. It's a good chance to relax themselves. To most of them, it's the first time to go abroad so they decide to make a collective tour.

The tour company shows them a new kind of tour circuit - DIY circuit. Each circuit contains some cities which can be selected by tourists themselves. According to the company's statistic, each city has its own interesting point. For instance, Paris has its interesting point of 90, New York has its interesting point of 70, ect. Not any two cities in the world have straight flight so the tour company provide a map to tell its tourists whether they can got a straight flight between any two cities on the map. In order to fly back, the company has made it impossible to make a circle-flight on the half way, using the cities on the map. That is, they marked each city on the map with one number, a city with higher number has no straight flight to a city with lower number.
Note: Weiwei always starts from Hangzhou(in this problem, we assume Hangzhou is always the first city and also the last city, so we mark Hangzhou both 1 and N+1), and its interesting point is always 0.
Now as the leader of the team, Weiwei wants to make a tour as interesting as possible. If you were Weiwei, how did you DIY it?
题意:旅游路线,有很多城市,去不同的城市有不同的值,不能从大编号到小编号城市,找出最大值的线路。
动态规划:路存在并且dp[j]+v[i]>dp[i],

#include<iostream>
using namespace std;
const int maxn=105;

int v[maxn];
int father[maxn];
int map[maxn][maxn];
int dp[maxn];

void show(int n){
	if(n==father[n]){cout<<n;return;}
	show(father[n]);
	cout<<"->"<<n;
}
int main()
{
	freopen("in.txt","r",stdin);
	int t,c=0;cin>>t;
	while(t--){

		memset(map,0,sizeof(map));
		memset(dp,0,sizeof(dp));
		int n;cin>>n;
		for(int i=1;i<=n;i++)cin>>v[i],father[i]=1;
		int m,a,b;cin>>m;
		for(int i=1;i<=m;i++)cin>>a>>b,map[a][b]=1;
		v[n+1]=0;
		for(int i=1;i<=n+1;i++)
			for(int j=1;j<i;j++){
				if(map[j][i]&&dp[j]+v[i]>dp[i]){
					dp[i]=dp[j]+v[i];
					father[i]=j;
				}
			}
	    printf("CASE %d#\n",++c);
        printf("points : %d\n",dp[n+1]);
        printf("circuit : ");
        show(father[n+1]);
        printf("->%d\n",1);
		if(t!=0) printf("\n");
	}
}

我猜你可能也喜欢:

No Comments - Leave a comment

Leave a comment

电子邮件地址不会被公开。 必填项已用*标注

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


Welcome , today is 星期二, 2017 年 10 月 24 日