HDU 1224-Free DIY Tour

Free DIY Tour

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


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


题意:
有一拨人集体出去旅行,旅游公司向他们展示了一种新的旅游线路——DIY电路。每个线路都有一些城市,可以由游客自己选择。根据该公司的统计,每个城市都有自己有趣的地方。例如,巴黎有90个有趣的点,纽约有70个有趣的点。现在他们有一张地图,每个城市都有一个编号,一个数字较高的城市没有直飞到一个数字较低的城市的飞机。
注:这波人总是从杭州开始(在这个问题上,我们假设杭州一直是第一个城市,也是最后一个城市,所以我们把杭州作为1和N + 1),它的有趣点总是0。
现在作为团队的领导你想让旅游尽可能有趣。你应该怎样 选择路线。

输入将包含几个案例。
第一行是一个整数T,它表明了案例的数量。
然后T个案例。
每种情况下将一个整数N(2≤N≤100)这是地图上的一些城市。
然后是N个整数,表示城市的有趣点列表。
然后它是一个整数M紧随其后的是M行(Ai,Bi)。每一对(Ai,Bi)表明飞行可以从城市Ai直飞到城市Bi。

分析:
不知道为什么这题会出现在最短路专题中,个人感觉有点像动态规划,代码注释写的很详细,可以参考代码


#include<iostream>
#include<stdio.h>
#include<string>
#include<string.h>
using namespace std;

struct book{
    int pre,interst;
}city[105];
///interst代表该地有趣的地方个数,
///pre代表到达该城市的上一个城市的编号

int main()
{
    int map1[105][105],dp[105],vis[105];
    ///map1数组存图,dp数组存走到该城市已经走过的有趣的地方的个数,vis数组用于后面统计,便于输出走过的城市的顺序
    int n,m,t;
    int a,b,k=0,c;
    scanf("%d",&t);
    while(t--)
    {
        k++;///记录组数
        scanf("%d",&n);
        memset(map1,0,sizeof(map1));
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&city[i].interst);///存对应地方有区的地方的个数
            city[i].pre=i;///初始化pre都存自身的编号
        }
        scanf("%d",&m);
        for(int i=1;i<=m;i++)
        {
            scanf("%d %d",&a,&b);
            map1[a][b]=1;///存图,不是双向图
        }
        city[n+1].interst=0;///最后一个城市也是起始的城市
        city[n+1].pre=1;
         printf("CASE %d#\n",k);
         for(int i=2;i<=n+1;i++)///更新dp数组
         {
             for(int j=1;j<i;j++)
             {
                 if(map1[j][i])
                 {
                     if(dp[i]<dp[j]+city[i].interst)
                     {
                         dp[i]=dp[j]+city[i].interst;
                         city[i].pre=j;///记录到达i城市的上一个城市编号
                     }
                 }
             }
         }
         printf("points : %d\n",dp[n+1]);
         c=0;
         for(int i=n+1;i>1;i=city[i].pre)
             vis[c++]=city[i].pre;///将选好的路串联起来依次存入VIS数组
         printf("circuit : ");
         for(int i=c-1;i>=0;i--)///因为vis数组里存的是倒序走的路,所以输出需要倒着输出
             printf("%d->",vis[i]);
         printf("1\n");///最后还要单独输出1,因为终点也是起点
         if(t)///这里需特别注意
            printf("\n");
    }
    return 0;
}





 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值