Free DIY Tour

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 6882    Accepted Submission(s): 2241

  

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 andN+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

解题心得:
1、这个题的大意很多点,每个点有一定的数值,部分点之间有边,要求从1点走到n+1点求最大的和。其实就是一个dp加了一些图论的问题,先按照起点拍一个序,然后标记一下终点是否可以走到,边走边标记,只有能够从1点走出达到的点才能加上。题意还要求记录一下路径,可以将dp数组开成一个结构体,在状态转移的时候顺便记录一下转移前的点的位置。最后再从n+1点倒过来找一下就可以了(实现方法看代码)。
2、还有一种做法就是将这个问题弄成纯dp,看成求最大上升子序列的和。
dp+图论代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 110;
struct DP
{
int sum;
int pre_city;
}dp[maxn];
struct
{
bool flag;
int Num;
}
num[maxn];
struct node
{
int start;
int End;
}maps[maxn*10000]; bool cmp(node a,node b)
{
return a.start < b.start;
} //这个函数就是拿来找路径的
void get_path(int n)
{
printf("circuit : ");
stack <int> s;
int End,pre;
End = n;
while(dp[End].pre_city != 0)//层层返回,从终点找路径找到起点
{
s.push(End);
End = dp[End].pre_city;
}
s.push(1);//将起点手动压入 printf("%d",s.top());
s.pop();
while(!s.empty())
{
if(s.top() == n)//到达的其实是n+1点,但是n+1点其实就是起点,这里处理一下
{
s.pop();
s.push(1);
}
printf("->");
printf("%d",s.top());
s.pop();
}
printf("\n");
} int main()
{
int t;
scanf("%d",&t);
int Test = 1;
while(t--)
{
int n,m,T;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&num[i].Num);
num[i].flag = false;
}
num[1].flag = true;
scanf("%d",&m);
T = 0;
while(m--)
{
scanf("%d%d",&maps[T].start,&maps[T].End);
if(maps[T].start >= maps[T].End)//起点比终点更大,不计入
T--;
T++;
} sort(maps,maps+T,cmp);//按照起点排一个序
for(int i=0;i<T;i++)
{
if(num[maps[i].start].flag)//要能从1走到该点才行
num[maps[i].End].flag = true;
if(dp[maps[i].End].sum < dp[maps[i].start].sum + num[maps[i].End].Num && num[maps[i].start].flag)
{
dp[maps[i].End].sum = dp[maps[i].start].sum + num[maps[i].End].Num;
dp[maps[i].End].pre_city = maps[i].start;//记录一下转移过来的路径
}
}
printf("CASE %d#\n",Test++);
printf("points : %d\n",dp[n+1]); get_path(n+1); //每次消除上次计算的,不然WA很惨
if(t != 0)
printf("\n");
for(int i=0;i<=T;i++)
maps[i].End = maps[i].start = 0;
for(int i=0;i<=n+1;i++)
{
dp[i].pre_city = 0;
dp[i].sum = 0;
num[i].flag = false;
num[i].Num = 0;
}
}
}



纯dp代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 110;
bool maps[maxn][maxn];
int dp[maxn];
int num[maxn],pre[maxn]; void get_path(int n)
{
printf("circuit : ");
stack <int> s;
int N = n; while(dp[N] != 0)//倒过来找到路径
{
s.push(N);
N = pre[N];
}
s.push(1);
printf("%d",s.top());
s.pop();
while(!s.empty())//再正过来输出
{
printf("->");
if(s.top() == n)
{
s.pop();
s.push(1);
}
printf("%d",s.top());
s.pop();
}
printf("\n");
} int main()
{
int T = 1;
int t;
scanf("%d",&t);
while(t--)
{
memset(dp,0,sizeof(dp));
memset(maps,0,sizeof(maps));
memset(num,0,sizeof(num));
memset(pre,0,sizeof(pre));
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&num[i]);
int m;
scanf("%d",&m);
for(int i=0;i<m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
if(a < b)
maps[a][b] = true;
} for(int i=1;i<=n+1;i++)
for(int j=1;j<i;j++)
{
if(maps[j][i])//有这条路
{
if(dp[i] < dp[j] + num[i])//状态转移
{
dp[i] = dp[j] + num[i];
pre[i] = j;//记录转移的位置
}
}
} printf("CASE %d#\n",T++);
printf("points : %d\n",dp[n+1]);
get_path(n+1);
if(t != 0)
printf("\n");
}
}



动态规划:HDU1224-Free DIY Tour的更多相关文章

  1. HDU ACM 1224 Free DIY Tour (SPFA)

    Free DIY Tour Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  2. HDU 1224 Free DIY Tour(spfa求最长路+路径输出)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1224 Free DIY Tour Time Limit: 2000/1000 MS (Java/Oth ...

  3. F - Free DIY Tour(动态规划,搜索也行)

    这道题可用动态规划也可以用搜索,下面都写一下 Description Weiwei is a software engineer of ShiningSoft. He has just excelle ...

  4. 【dfs or 最短路】【HDU1224】【Free DIY Tour】

    路径只能由小序号到大序号..(起点可以视为最小的序号和最大的序号) 问怎么走 happy值最大.. DFS N=100 且只能小序号到大序号 显然dfs可以过.. 但是存路径的时候sb了.....应该 ...

  5. Free DIY Tour HDU1224

    一道很好的dfs加储存路径的题目  :路径保存:每次dfs都存i 当大于max时 将临时数组保存到答案数组 并不是当 当前值大于最大值时更新路径 还要加上一个条件:能回去 #include<bi ...

  6. HDU1224-Free DIY Tour(SPFA+路径还原)

    Weiwei is a software engineer of ShiningSoft. He has just excellently fulfilled a software project w ...

  7. HDU 1224 Free DIY Tour

    题意:给出每个城市interesting的值,和城市之间的飞行路线,求一条闭合路线(从原点出发又回到原点) 使得路线上的interesting的值之和最大 因为要输出路径,所以用pre数组来保存前驱 ...

  8. 【HDOJ】1224 Free DIY Tour

    DP. #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm ...

  9. hdu Free DIY Tour

    http://acm.hdu.edu.cn/showproblem.php?pid=1224 #include <cstdio> #include <cstring> #inc ...

随机推荐

  1. Java 在使用@Select遇到的问题:拼接字符串将数组拼为了字符串

    Java再用@Select拼接sql语句时候, #{参数名}:是加引号的 ${参数名}:是不加引号的 例如: userIds为List或者数组,值为1,2,3,4,5 1.@Select(" ...

  2. 14.JAVA-jar命令使用

    介绍 jar命令用来对*.class文件进行压缩,从而生成jar(archive)归档文件,避免文件过多. 定义一个文件: package common.demo ; public class Tes ...

  3. 学习《CSS选择器Level-4》不完全版

    1 概述 1.1 前言 选择器是CSS的核心组件.本文依据W3C的Selectors Level 4规范,概括总结了Level1-Level4中绝大多数的选择器,并做了简单的语法说明及示例演示.希望对 ...

  4. 七、SSR(服务端渲染)

    使用框架的问题 下载Vue.js 执行Vue.js 生成HTML页面(首屏显示,依赖于vue.js的加载) 以前没有前端框架时,用jsp/php在服务器端进行数据的填充,发送给客户端就是已经填充好的数 ...

  5. jquery 的extend的方法

    用flot.js  用到了jquery的extend 方法 关于extend方法 我就照手册打一遍,加深一下理解,说实话其实我理解的也不透 extend  用一个或多个其他对象来扩展一个对象,返回被扩 ...

  6. 零基础逆向工程30_Win32_04_资源文件_消息断点

    1 资源文件,创建对话框 详细步骤: 1.创建一个空的Win32应用程序 2.在VC6中新增资源 File -> New -> Resource Script 创建成功后会新增2个文件:x ...

  7. JAVA方法定义和调用

    类的方法代表的是实例的某种行为或功能 定义类的方法 访问修饰 类型 方法名(参数列表){ //方法体 } 1.把方法当作一个模块,是个“黑匣子”,完成某个特定的功能,并返回处理结果 2.方法分类“ 返 ...

  8. iOS JS 交互之利用系统JSContext实现 JS调用OC方法以及Objective-C调用JavaScript方法

    ios js 交互分为两块: 1.oc调用js 这一块实现起来比较简单, 我的项目中加载的是本地的html,js,css,需要注意的是当你向工程中拖入这些文件时,选择拷贝到工程中,(拖入的文件夹是蓝色 ...

  9. ConcurrentHashMap源码刨析(基于jdk1.7)

    看源码前我们必须先知道一下ConcurrentHashMap的基本结构.ConcurrentHashMap是采用分段锁来进行并发控制的. 其中有一个内部类为Segment类用来表示锁.而Segment ...

  10. LeetCode Remove Duplicates from Sorted List 删除有序链表中的重复结点

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...