F - Free DIY Tour(动态规划,搜索也行)
这道题可用动态规划也可以用搜索,下面都写一下
Description
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
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
Output a blank line between two cases.
Sample Input
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
points : 90
circuit : 1->3->1
CASE 2#
points : 90
circuit : 1->2->1
#include <stdio.h>
#include <string.h>
bool link[][];
int intrest[], dp[], last[], path[];
int main()
{
int t, case_num = ;
//freopen("input.txt", "r", stdin);
scanf("%d", &t);
while(t--)
{
int n, m, i, j;
memset(link, , sizeof(link));
memset(dp, , sizeof(dp));
last[] = ; //last记录上一个走的城市的标号,last[1] = 0是为了让追溯到第一个城市之后就不继续追溯了
scanf("%d", &n);
if(case_num != )
printf("\n");
for(i = ; i <= n; i++)
{
scanf("%d", &intrest[i]);
}
intrest[i] = ; //注意i城市有趣度为0!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
scanf("%d", &m);
for(int i = ; i < m; i++)
{
int a, b;
scanf("%d%d", &a, &b);
link[a][b] = link[b][a] = ;
}
for(i = ; i <= n+; i++) //假设目标城市标号为i(注意到达它之前经过的城市的标号都小于它)
{
for(j = ; 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 = ;
i = n+;
while(last[i])
{
path[j++] = last[i];
i = last[i];
}
printf("CASE %d#\n", case_num++);
printf("points : %d\n", dp[n+]);
printf("circuit : ");
for(i = j-; i >= ; i--)//注意输出的个数并非为城市数目!!!!!!!!!!!!!
{
printf("%d->", path[i]);
}
printf("1\n");
}
return ;
}
下面是深搜的代码及讲解
/*
问题:输入测试案例数t
输入城市数目n
接下来n个数字分别代表标号为1-n的城市的风景度
接下来一个数字m,代表道路数目
接下来m对数字(a, b)代表a城市和b城市是相连的
要求只能从标号小的城市走到标号大的城市,最后回到城市1,所以城市1的标号也是n+1
且城市1和城市n+1的风景度都为0
问:按照上述条件从1出发,所能遍历的最大风景度是多少 分析:由于出发城市固定为1,终点城市固定为n+1(也即1)
除了起点之外,任意一步的出发城市可能有多种选择,下一步也有多种选择,
因此可以使用深度搜索来做,遍历所有的可能,当遇到回到起点的时候风景度总和大于上一个风景度总和的时候
更新风景度为其中的最大值,并把记录最大风景度所走的路线也更新一下
深搜的参数为进入深搜之前的风景度、进入深搜的时候所在的城市,记录这是第几步的一个数
*/
#include <stdio.h>
#include <string.h>
#define M 105
int value[M], n, m, maxi, road_len;
bool link[M][M];
int path_tmp[M], ans[M];
void dfs(int step_num, int sum, int now_point)
{
int i, j;
for(i = now_point+; i <= n; i++)
{
/*
注意并非从1到n+1然后值最大的时候更新sum,因为即使所得的值最大,但是回不到起点也不行滴
for循环遍历第step_num步可能走的城市号的所有可能性
如果有路,那就走,并且看一下走这步路之后是否下一步就可以通往n+1城市了
如果可以通往n+1城市,那么更新最大风景值,并记录这个路径
*/
if(link[now_point][i])
{
path_tmp[step_num] = i;
if(link[i][n+])
{
path_tmp[step_num+] = ;
if(sum +value[i]> maxi)
{
maxi = sum + value[i];
road_len = step_num+;
for(j = ; j <= road_len; j++)
{
ans[j] = path_tmp[j];
}
}
}
dfs(step_num+, sum+value[i], i);
}
}
}
void init()
{
ans[] = ;//第一步走城市1,即起点
maxi = ; //把最大风景值初始为0
road_len = ;//初始只经过了城市1,所以路长为1
memset(link, , sizeof(link));//所有城市都没有路
scanf("%d", &n);
for(int i = ; i <= n; i++)
scanf("%d", &value[i]);
value[] = value[n+] = ;//城市1和城市n+1的风景度为0
scanf("%d", &m);
for(int i = ; i < m; i++)
{
int a, b;
scanf("%d%d", &a, &b);
link[a][b] = link[b][a] = ; //因为路是双向的,相对的
}
}
int main()
{
int t, case_num = ;
//freopen("input.txt", "r", stdin);
scanf("%d", &t);
while(t--)
{
init();
if(case_num != )
printf("\n"); // 每个案例之间输出一行空行,但是最后一个输出之后不用输出空行
printf("CASE %d#\n", case_num++);
dfs(, , );//初始时候该走第一步了,这时候风景度为0,当前所在城市1
printf("points : %d\n", maxi); //刚开始竟然把这句话放在dfs前面了,下次表粗心了,wrong了两次
if(road_len != )
{
printf("circuit : ");
for(int i = ; i < road_len; i++)
printf("%d->", ans[i]);
printf("%d\n", ans[road_len]);
}
}
return ;
}
F - Free DIY Tour(动态规划,搜索也行)的更多相关文章
- 动态规划:HDU1224-Free DIY Tour
Free DIY Tour Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 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 ...
- 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 ...
- Liunx搜索命令行
1.grep grep(General Regular Expression Parser,通用规则表达式分析程序)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来. 它的使 ...
- EasyUI实现分页、搜索、行按钮功能
1.html+js代码: <html> <head> <meta name="viewport" content="width=device ...
- HDU1224-Free DIY Tour(SPFA+路径还原)
Weiwei is a software engineer of ShiningSoft. He has just excellently fulfilled a software project w ...
- idea 模糊搜索 ctrl + f(单词不完整搜索不到的解决办法)
1,现象描述,笔者在用 idea 的 ctrl + f 搜索文件的内容时,发现了很神奇的问题,就是字符串必须输入完整才能搜索到,输入一半,哪怕是个字母输入了9个也搜不到 2,可以发现,就差一个字母 h ...
- Atcoder abc187 F Close Group(动态规划)
Atcoder abc187 F Close Group 题目 给出一张n个点,m条边的无向图,问删除任意数量的边后,留下来的最少数量的团的个数(\(n \le 18\) ) 题解 核心:枚举状态+动 ...
- 【dfs or 最短路】【HDU1224】【Free DIY Tour】
路径只能由小序号到大序号..(起点可以视为最小的序号和最大的序号) 问怎么走 happy值最大.. DFS N=100 且只能小序号到大序号 显然dfs可以过.. 但是存路径的时候sb了.....应该 ...
随机推荐
- gcc 的编译过程
通常我们都是使用下面的命令来直接生成可执行文件 gcc demo.c -o demo 对于我们来说十分简单,但是对编译器来说却完成了一系列复杂的工作,概括起来有如下几步: 1. 预处理 gcc -E ...
- Windows下的PHP开发环境搭建——PHP线程安全与非线程安全、Apache版本选择,及详解五种运行模式。
今天为在Windows下建立PHP开发环境,在考虑下载何种PHP版本时,遭遇一些让我困惑的情况,为了解决这些困惑,不出意料地牵扯出更多让我困惑的问题. 为了将这些困惑一网打尽,我花了一下午加一晚上的时 ...
- angulajs 当input使用 bootstrap的email类型时,如果是无效的email格式,则ng-model无效的情况
当使用bootstrap的如下input时 <input type="email" ng-model="userid"> 如果输入的内容是无效的em ...
- zookeeper集群搭建设置
zookeeper 官网:http://zookeeper.apache.org/ 现在最新版本是 3.4.6 ,但是这个版本我没有运行起来,可能是那配置出现问题了,现在我用的是3.4.5 http: ...
- Linux设置高分辨率后无法进入X系统
Vmware9.0中Xubuntu分辨率从800x600变更为1366x768后在用户输入密码登录后会自动退出x系统,出现这种情况时可以切换到命令行登录界面,然后将-/.config/xfce4/xf ...
- Java中volatile的作用以及用法
volatile让变量每次在使用的时候,都从主存中取.而不是从各个线程的“工作内存”. volatile具有synchronized关键字的“可见性”,但是没有synchronized关键字的“并发正 ...
- C标准函数库中获取时间与日期、对时间与日期数据操作及格式化
表示时间的三种数据类型[编辑] 日历时间(calendar time),是从一个标准时间点(epoch)到现在的时间经过的秒数,不包括插入闰秒对时间的调整.开始计时的标准时间点,各种编译器一般使用19 ...
- Cocos2d-x 3.0 使用TinyXml 解析XML文件
在cocos2d-x 3.0中Xml解析已经不用自己找库了,已经为我们集成好了. text.xml <!--?xml version ="1.0" encoding =&qu ...
- UC/0S2之中断
中断是计算机系统处理异步事件的重要机制.当异步事件发生时,事件通常是通过硬件向cpu发出中断请求的.在一般情况下,cpu响应这个请求后会立即运行中断服务程序来处理该事件: 为了处理任务延时.任务调度等 ...
- [学习笔记]jQuery实现一些动画效果的方法
jQuery实现效果的方法 1,隐藏和显示:hide(),show(),toggle() // [ˈtɑ:gl]切换 语法: $(selector).hide(speed,callback); $( ...