码迷,mamicode.com
首页 > 其他好文 > 详细

F - Free DIY Tour(动态规划)

时间:2015-10-19 23:57:30      阅读:534      评论:0      收藏:0      [点我收藏+]

标签:

这道题也可以用深搜做,可以深搜本来就不熟,好久没做早忘了,明天看看咋做的

 

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
 
 
问题:输入测试案例数t
   输入城市数n
   依次输入1到n号城市中每个城市的有趣程度
   输入道路的数目m
   输入m组数据(a, b)表示a和b城市相连
   已知:出发的城市标号1和n+1,因为它不仅是起点,同时也是终点,它的有趣度为0(初始化的时候一定要记得,不然会很惨啊啊啊啊……)
      只能由标号小的城市出发到达标号大的城市
 
   wrong answer原因:初始化的时候忽略了intrest[n+1] = 0;
             输出路径的时候注意并非经过所有的城市,所以输出的城市数目不等于总城市数
   易错点:注意每个案例之间要有一行空行,最后一个案例后面没有空行
       
    分析:用Link[i][j]存储i城市和j城市是否相连,intrest[i]存储城市i的有趣度dp[i]存储到达i城市的时候的最大有趣度
      dp[i] = max(dp[j]+intrest[i], dp[i])
      到达i城市之前可能在除了i城市和起点之外的任意一个城市,遍历经过i城市之前的城市为任意城市,把到达i城市的时候的有趣度更新为其中最大的一个数代码中具体讲
 
#include <stdio.h>
#include <string.h>
bool link[105][105];
int intrest[105], dp[105], last[105], path[105];
int main()
{
    int t, case_num = 1;
    //freopen("input.txt", "r", stdin);
    scanf("%d", &t);
    while(t--)
    {
        int n, m, i, j;
        memset(link, 0, sizeof(link));
        memset(dp, 0, sizeof(dp));
        last[1] = 0;
        scanf("%d", &n);
        if(case_num != 1)
            printf("\n");
        for(i = 1; i <= n; i++)
        {
            scanf("%d", &intrest[i]);
        }
        intrest[i] = 0; //注意i城市有趣度为0!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        scanf("%d", &m);
        for(int i = 0; i < m; i++)
        {
            int a, b;
            scanf("%d%d", &a, &b);
            link[a][b] = link[b][a] = 1;
        }
        for(i = 2; i <= n+1; i++) //假设目标城市标号为i(注意到达它之前经过的城市的标号都小于它)
        {
            for(j = 1; j < i; j++)
                //遍历到达i城市之前所在的城市的标号的所有可能性,
                //更新到达i城的时候的有趣度之和为所有情况中最大的
            {
                if(dp[j]+intrest[i] > dp[i] && link[i][j])
                {
                    dp[i] = dp[j]+intrest[i];
                   last[i] = j;
                }
            }
        }
        j = 0;
        i = n+1;
        while(last[i])
        {
            path[j++] = last[i];
            i = last[i];
        }
        printf("CASE %d#\n", case_num++);
        printf("points : %d\n", dp[n+1]);
        printf("circuit : ");
        for(i = j-1; i >= 0; i--)//注意输出的个数并非为城市数目!!!!!!!!!!!!!
        {
            printf("%d->", path[i]);
        }
        printf("1\n");
    }
    return 0;
}

 

F - Free DIY Tour(动态规划)

标签:

原文地址:http://www.cnblogs.com/rain-1/p/4893271.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!