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

1 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 }

   

   

时间: 2024-08-24 09:54:00

HDU 1224 Free DIY Tour 简单DP的相关文章

hdu 1224 Free DIY Tour(最长路/dp)

http://acm.hdu.edu.cn/showproblem.php?pid=1224 基础的求最长路以及记录路径.感觉dijstra不及spfa好用,wa了两次. #include <stdio.h> #include <algorithm> #include <set> #include <map> #include <vector> #include <math.h> #include <string.h> #

HDU 1224 Free DIY Tour(DP)

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

hdu 1224 Free DIY Tour(最长的公路/dp)

http://acm.hdu.edu.cn/showproblem.php? pid=1224 基础的求最长路以及记录路径. 感觉dijstra不及spfa好用,wa了两次. #include <stdio.h> #include <algorithm> #include <set> #include <map> #include <vector> #include <math.h> #include <string.h>

hdu 1224 Free DIY Tour

这题真是被弄的欲仙欲死啊,开始写的代码死活的a不掉,也不知道哪里错了,先附上,求大牛指点啊..... #include<iostream> #include<algorithm> #include<cstdio> using namespace std; int n,m; int point[100+5]; int re; int ree[105]; int l; struct stu { int a,b; }; stu mapp[100000+5]; void dfs

HDU 4960 Another OCD Patient 简单DP

思路: 因为是对称的,所以如果两段是对称的,那么一段的前缀和一定等于另一段的后缀和.根据这个性质,我们可以预处理出这个数列的对称点对.然后最后一个对称段是从哪里开始的,做n^2的DP就可以了. 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath> 6 #include <algori

HDU 1160 FatMouse&#39;s Speed 简单DP

FatMouse's Speed Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the

HDU 5375 Gray code (简单dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5375 题面: Gray code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 626    Accepted Submission(s): 369 Problem Description The reflected binary cod

hdu 2084 数塔(简单dp)

题目 简单dp //简单的dp #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int dp[110][110];//dp[i][j] di i ceng di j ge zui da he int a[110][110]; int main() { int t; scanf("%d",&t); while(t--) { int n;

[ACM] HDU 5074 Hatsune Miku (简单DP)

Hatsune Miku Problem Description Hatsune Miku is a popular virtual singer. It is very popular in both Japan and China. Basically it is a computer software that allows you to compose a song on your own using the vocal package. Today you want to compos