首页 > ACM题库 > HDU-杭电 > HDU 1224 Free DIY Tour-动态规划-[解题报告] C++
2013
12-04

HDU 1224 Free DIY Tour-动态规划-[解题报告] C++

Free DIY Tour

问题描述 :

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?

输入:

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.

输出:

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.

样例输入:

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

样例输出:

CASE 1#
points : 90
circuit : 1->3->1

CASE 2#
points : 90
circuit : 1->2->1

http://acm.hdu.edu.cn/showproblem.php?pid=1224

题目大意:给出一些城市的point,从起点出发经过一些城市最终回到起点,在这个过程中经过的下一个城市的point值要求不低于现在城市的point值(最后回到城市1的情况不算),问最后能够达到的最大point值之和,并输出路径。

直接动态规划;转台转移方程 for i <- 1 to n for j <- 1 to n if point[i]<point[j] and f[i]+point[i]>f[j] then f[j]=f[i]+point[i],pre[i]=i end if.其间用一个数组pre[]保留路径,方便之后输出结果。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 111;
int f[N], point[N] , pre[N], go[N][N];
void putans(int u) {
    if(u == -1) return;
    putans(pre[u]);
    printf("%d->",u);
}
int main() {
    int T , n , cas = 1;
    scanf("%d",&T);
    while(T--) {
        if(cas > 1) puts("");
        printf("CASE %d#\n",cas++);
        scanf("%d",&n);
        n += 1;
        for(int i=1;i<n;i++) scanf("%d",&point[i]);
        point[n] = 0;
        for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) {
            go[i][j] = 0;
        }
        for(int i=1;i<=n;i++) f[i] = 0 , pre[i] = -1;
        int m;
        scanf("%d",&m);
        while(m--) {
            int u , v;
            scanf("%d%d",&u,&v);
            go[u][v] = 1;
        }
        for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++) {
            if(go[i][j] && f[i]+point[i] > f[j]) {
                f[j] = f[i] + point[i];
                pre[j] = i;
            }
        }
        printf("points : %d\n",f[n]);
        printf("circuit : 1->");
        putans(pre[n]);
        printf("1\n");
    }
    return 0;
}

 

 


  1. [email protected]

  2. I like your publish. It is great to see you verbalize from the coronary heart and clarity on this essential subject matter can be easily noticed.

  3. 我还有个问题想请教一下,就是感觉对于新手来说,递归理解起来有些困难,不知有没有什么好的方法或者什么好的建议?