HDU 1224 Free DIY Tour 简单DP

                Free DIY Tour

 

Problem Description
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 andN+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?
 

 

Input
The input will contain several cases. The first line is an integer T which suggests the number of cases. Then T cases follows.
Each case will begin with an integer N(2 ≤ N ≤ 100) which is the number of cities on the map.
Then N integers follows, representing the interesting point list of the cities.
And then it is an integer M followed by M pairs of integers [Ai, Bi] (1 ≤ i ≤ M). Each pair of [Ai, Bi] indicates that a straight flight is available from City Ai to City Bi.
 

 

Output
For each case, your task is to output the maximal summation of interesting points Weiwei and his fellow workers can get through optimal DIYing and the optimal circuit. The format is as the sample. You may assume that there is only one optimal circuit. 

Output a blank line between two cases.
 

 

Sample Input
2
3
0 70 90
4
2
1 3
2 4
3 4
3
0 90 70
4
1 2
1 3
2 4
3 4
 

 

Sample Output
CASE 1#
points : 90
circuit : 1->3->1
CASE 2#
points : 90
circuit : 1->2->1
 
 
题意:n座城市,编号为1到n,然后给出每个城市的有趣值,规定1为0
   再给出一些路,
   问现在从1沿着哪些路去旅行,再回到1,有趣值最大。
   
   而且,题目规定,从编号大的城市不能回到编号小的城市
   (刚开始看成有趣值大的不能到有趣值小的)
   这样,问题就简单的多了
   
   假设不回到1了,而是去一座城市,为n+1,其值为inf
   就是求一个子序列,使得这个序列的和最大
   最后的和再减去inf
   条件:有路
   
   dp[i]表示从1到i的最大有趣值
 
 1 #include<iostream>
 2 #include<cstring>
 3 #include<algorithm>
 4 
 5 using namespace std;
 6 
 7 const int maxn=104;
 8 const int inf=0x3f3f3f;
 9 
10 
11 bool vis[maxn][maxn];           //标记2个点是否有路
12 int dp[maxn];                   //表示从1到i的最大值
13 int pre[maxn];
14 int poi[maxn];
15 
16 void output(int cnt)
17 {
18     if(pre[cnt]!=-1)
19         output(pre[cnt]);
20     cout<<cnt<<"->";
21 }
22 
23 int main()
24 {
25     int test;
26     int cas=1;
27 
28     cin>>test;
29 
30     while(test--)
31     {
32         int n;
33         cin>>n;
34 
35         for(int i=1;i<=n;i++)
36             cin>>poi[i];
37 
38         int m;
39         cin>>m;
40 
41         memset(vis,false,sizeof(vis));
42 
43         for(int i=1;i<=m;i++)
44         {
45             int u,v;
46             cin>>u>>v;
47 
48             vis[u][v]=vis[v][u]=true;
49         }
50 
51         poi[n+1]=inf;
52         memset(dp,0,sizeof(dp));
53         memset(pre,-1,sizeof(pre));
54 
55         for(int i=1;i<=n+1;i++)
56             for(int j=1;j<i;j++)
57             {
58                if(vis[i][j]&&dp[i]<dp[j]+poi[i])
59                {
60                     dp[i]=dp[j]+poi[i];
61                     pre[i]=j;
62                }
63             }
64         
65         if(cas>1)
66             cout<<"\n";
67         
68         cout<<"CASE "<<cas++<<"#"<<endl;
69         cout<<"points : "<<dp[n+1]-poi[n+1]<<endl;
70         cout<<"circuit : ";
71         output(pre[n+1]);
72         cout<<1<<endl;
73     }
74     return 0;
75 }
View Code

 

 
   
   
 
 
 
 
 
 
 
posted on 2015-04-22 16:51  _fukua  阅读(284)  评论(0编辑  收藏  举报