HDU 1224(动态规划)

桃扇骨 2022-06-02 11:39 305阅读 0赞

问题描述:

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?

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

  1. 2
  2. 3
  3. 0 70 90
  4. 4
  5. 1 2
  6. 1 3
  7. 2 4
  8. 3 4
  9. 3
  10. 0 90 70
  11. 4
  12. 1 2
  13. 1 3
  14. 2 4
  15. 3 4

Sample Output

  1. CASE 1#
  2. points : 90
  3. circuit : 1->3->1
  4. CASE 2#
  5. points : 90
  6. circuit : 1->2->1

题目题意:题目给了一个图,每个点都有一定的参观价值,问我们参观一圈(即回到起点1)的最大参观价值。

题目分析:dp[i]表示到节点i的最大价值

代码如下:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<vector>
  6. using namespace std;
  7. const int maxn=150;
  8. bool G[maxn][maxn];
  9. int dp[maxn],a[maxn],pre[maxn];
  10. vector<int> vec;
  11. int main()
  12. {
  13. int t,icase=1;
  14. scanf("%d",&t);
  15. while (t--) {
  16. int n,m;
  17. scanf("%d",&n);
  18. for (int i=1;i<=n;i++) {
  19. scanf("%d",&a[i]);
  20. }
  21. a[++n]=0;
  22. scanf("%d",&m);
  23. memset (G,false,sizeof (G));
  24. for (int i=1;i<=m;i++) {
  25. int a,b;
  26. scanf("%d%d",&a,&b);
  27. G[a][b]=true;
  28. }
  29. memset (dp,0,sizeof (dp));
  30. memset (pre,0,sizeof (pre));
  31. for (int i=1;i<=n;i++) {
  32. for (int j=1;j<i;j++) {
  33. if (G[j][i]) {
  34. if (dp[j]+a[i]>dp[i]) {
  35. dp[i]=dp[j]+a[i];
  36. pre[i]=j;
  37. }
  38. }
  39. }
  40. }
  41. printf("CASE %d#\n",icase++);
  42. printf("points : %d\n",dp[n]);
  43. printf("circuit : ");
  44. m=n;
  45. vec.clear();
  46. while (m!=0) {
  47. vec.push_back(m);
  48. m=pre[m];
  49. }
  50. for (int i=vec.size()-1;i>=0;i--) {
  51. if (i==0) printf("%d\n",1);
  52. else printf("%d->",vec[i]);
  53. }
  54. if (t!=0) printf("\n");
  55. }
  56. return 0;
  57. }

发表评论

表情:
评论列表 (有 0 条评论,305人围观)

还没有评论,来说两句吧...

相关阅读

    相关 HDU 1978(动态规划)

    问题描述 这是一个简单的生存游戏,你控制一个机器人从一个棋盘的起始点(1,1)走到棋盘的终点(n,m)。游戏的规则描述如下:  1.机器人一开始在棋盘的起始点并有起始点所

    相关 HDU 4540(动态规划)

    问题描述: 威威猫最近不务正业,每天沉迷于游戏“打地鼠”。    每当朋友们劝他别太着迷游戏,应该好好工作的时候,他总是说,我是威威猫,猫打老鼠就是我的工作!