zoukankan      html  css  js  c++  java
  • HDU1224 DP

    Free DIY Tour

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 6227    Accepted Submission(s): 2013


    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 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
    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
     
    Author
    JGShining(极光炫影)
     
    Source
     
    题意:
    有n个城市,每次只能从1城市出发最后回到1 城市,给出几个相连的城市并且只能从数字大的城市到数字小的城市即路是单向的,每个城市有一个价值,问怎么走一圈能够得到最多的价值。
    代码:
     1 /*
     2 到达每一个城市的最大价值取决于能够到达他的城市中价值较大的那个再加上它本身,记录路径load[i]存入能够到达i
     3 的最优的那个城市最后输出load[i],load[load[i]]........
     4 */
     5 #include<iostream>
     6 #include<string>
     7 #include<cstdio>
     8 #include<cmath>
     9 #include<cstring>
    10 #include<algorithm>
    11 #include<vector>
    12 #include<iomanip>
    13 #include<queue>
    14 #include<stack>
    15 using namespace std;
    16 int t,n,m;
    17 int dp[110];
    18 int val[110];
    19 int main()
    20 {
    21     int a,b;
    22     scanf("%d",&t);
    23     for(int h=1;h<=t;h++)
    24     {
    25         stack<int>q[110];
    26         scanf("%d",&n);
    27         for(int i=1;i<=n;i++)
    28         scanf("%d",&val[i]);
    29         val[n+1]=0;
    30         scanf("%d",&m);
    31         while(m--)
    32         {
    33             scanf("%d%d",&a,&b);
    34             q[b].push(a);
    35         }
    36         memset(dp,0,sizeof(dp));
    37         int load[110];
    38         for(int i=1;i<=n+1;i++)
    39         {
    40             if(q[i].empty())
    41             continue;
    42             while(!q[i].empty())
    43             {
    44                 int father=q[i].top();
    45                 q[i].pop();
    46                 if(dp[i]<dp[father]+val[i])
    47                 {
    48                     dp[i]=dp[father]+val[i];
    49                     load[i]=father;
    50                 }
    51             }
    52 
    53         }
    54         if(h!=1)
    55         printf("
    ");
    56         printf("CASE %d#
    ",h);
    57         printf("points : %d
    circuit : ",dp[n+1]);
    58         int k=0,kk=n+1,lload[110];
    59         while(kk!=1)
    60         {
    61             lload[k++]=load[kk];
    62             kk=load[kk];
    63         }
    64         for(int i=k-1;i>=0;i--)
    65         {
    66             printf("%d->",lload[i]);
    67         }
    68         printf("1
    ");
    69     }
    70     return 0;
    71 }
     
  • 相关阅读:
    2018.8.5 复习笔记
    C#抽象类与接口的区别【转】
    double转整数问题
    C++学习笔记
    BCG使用
    C++设计模式之工厂方法模式
    静态成员函数
    CTreeCtrl 控件使用总结
    WinAPI: ShellExecute
    C++ STL map使用
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/5802801.html
Copyright © 2011-2022 走看看