hdu 1224(动态规划 DAG上的最长路)
Free DIY Tour
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5815 Accepted Submission(s): 1855
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?
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.
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.
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
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#define N 105
using namespace std; int dp[N]; ///dp[i]表示前i个城市能够获得的最大权值
int mp[N][N];
int v[N];
int pre[N];
int path[N];
int main()
{
int tcase;
scanf("%d",&tcase);
int cnt = ;
while(tcase--){
int n,m;
scanf("%d",&n); memset(dp,,sizeof(dp));
memset(mp,,sizeof(mp));
for(int i=;i<=n;i++){
scanf("%d",&v[i]);
}
v[n+]=;///不要忘了,会WA
scanf("%d",&m);
int a,b;
for(int i=;i<=m;i++){
scanf("%d%d",&a,&b);
mp[a][b]=;
}
for(int i=;i<=n+;i++){
for(int j=;j<i;j++){
if(mp[j][i]==){
if(dp[i]<dp[j]+v[i]){
dp[i]=dp[j]+v[i];
pre[i]=j;
}
}
}
}
printf("CASE %d#\npoints : %d\n",cnt++,dp[n+]);
int t = n+;
int k=;
while(t!=){
path[++k] = pre[t];
t=pre[t];
}
printf("circuit : ");
for(int i=k;i>=;i--)printf("%d->",path[i]);
printf("1\n");
if(tcase) printf("\n");
}
return ;
}
hdu 1224(动态规划 DAG上的最长路)的更多相关文章
- NYOJ_矩形嵌套(DAG上的最长路 + 经典dp)
本题大意:给定多个矩形的长和宽,让你判断最多能有几个矩形可以嵌套在一起,嵌套的条件为长和宽分别都小于另一个矩形的长和宽. 本题思路:其实这道题和之前做过的一道模版题数字三角形很相似,大体思路都一致,这 ...
- UVa 10285 最长的滑雪路径(DAG上的最长路)
https://vjudge.net/problem/UVA-10285 题意: 在一个R*C的整数矩阵上找一条高度严格递减的最长路.起点任意,但每次只能沿着上下左右4个方向之一走一格,并且不能走出矩 ...
- Vulnerable Kerbals CodeForces - 772C【拓展欧几里得建图+DAG上求最长路】
根据拓展欧几里得对于同余方程 $ax+by=c$ ,有解的条件是 $(a,b)|c$. 那么对于构造的序列的数,前一个数 $a$ 和后一个数 $b$ ,应该满足 $a*x=b(mod m)$ 即 $ ...
- HDU 4109 Instrction Arrangement(DAG上的最长路)
把点编号改成1-N,加一点0,从0点到之前任意入度为0的点之间连一条边权为0的边,求0点到所有点的最长路. SPFA模板留底用 #include <cstdio> #include < ...
- uva103(最长递增序列,dag上的最长路)
题目的意思是给定k个盒子,每个盒子的维度有n dimension 问最多有多少个盒子能够依次嵌套 但是这个嵌套的规则有点特殊,两个盒子,D = (d1,d2,...dn) ,E = (e1,e2... ...
- POJ 1949 Chores(DAG上的最长路 , DP)
题意: 给定n项任务, 每项任务的完成用时t和完成每项任务前需要的k项任务, 求把所有任务完成的最短时间,有当前时间多项任务都可完成, 那么可以同时进行. 分析: 这题关键就是每项任务都会有先决条件, ...
- HDU 1224 无环有向最长路
用bellman_ford的方法,将中间不断取较小值,修改为取较大值就可以了 #include <cstdio> #include <cstring> #include < ...
- HDU 3249 Test for job (有向无环图上的最长路,DP)
解题思路: 求有向无环图上的最长路.简单的动态规划 #include <iostream> #include <cstring> #include <cstdlib ...
- hdu 1534(差分约束+spfa求最长路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1534 思路:设s[i]表示工作i的开始时间,v[i]表示需要工作的时间,则完成时间为s[i]+v[i] ...
随机推荐
- 【Python】python函数每日一讲 - dir()
最近确实是有些忙,刚过了年,积攒了很多事情需要处理,所以每日一函数只能是每两天更新一篇,在这里和大家致歉. 今天我们来看一个非常重要的函数:dir() 中文说明:不带参数时,返回当前范围内的变量.方法 ...
- Android问题:could not install *smartsocket* listener;Address already in use
今天启动genymotion后,发现没有ip地址,运行项目时报错: 可见,没有连接到模拟器,无法运行,而先前说过没有ip,自然而然连接不上, 解决放法:将资源管理器打开,将adb全部退出 ...
- lintcode-116-跳跃游戏
116-跳跃游戏 给出一个非负整数数组,你最初定位在数组的第一个位置. 数组中的每个元素代表你在那个位置可以跳跃的最大长度. 判断你是否能到达数组的最后一个位置. 注意事项 这个问题有两个方法,一个是 ...
- iOS-缓存
1. 实现数据模型缓存 可以用NSKeyedArchiver类来实现数据模型缓存.为了把模型对象用NSKeyedArchiver归档,模型类需要遵循NSCoding协议. . 应该用哪种缓存技术 在众 ...
- PAT 1055 集体照
https://pintia.cn/problem-sets/994805260223102976/problems/994805272021680128 拍集体照时队形很重要,这里对给定的 N 个人 ...
- 【SSH】——Hibernate实现简单的自动建表
[与ORM] Object Relational Mapping,对象关系映射,将对象和关系联系了起来.面向对象是从耦合.聚合.封装等的基础上发展起来的,而关系数据库则是从数学理论发展而来的,两套理论 ...
- Oracle解决索引碎片功能
我们开始时向一个空的带索引的表中插入大量数据后,是不会产生碎片问题的,但是,数据库经过很长一段时间的增删改查后,难免会出现碎片问题,影响数据库的性能,Oracle对于这一问题有自己的解决方案. 下面介 ...
- Shiro 的 HelloWorld
密码文件 [users] zhang=123 wang=123 测试 package org.zln.hello; import org.apache.log4j.LogManager; import ...
- JSP语法,运行机理等
JSP是几年前就接触了,但是用归用,很多实际的意义含义等还是不太明白,借此机会,梳理一下. 1.JSP运行原理:当浏览器web应用服务器请求一个JSP页面时,Web应用服务器将其转换成一个Servle ...
- [UOJ #48]【UR #3】核聚变反应强度
题目大意:给你一串数$a_i$,求$sgcd(a_1,a_i)$,$sgcd(x,y)$表示$x,y$的次大公约数,若没有,则为$-1$ 题解:即求最大公约数的最大约数,把$a_1$分解质因数,求出最 ...