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] ...
随机推荐
- 健康领域今年开始井喷了,养老地产和私人医生这两个领域目测成为下一轮BAT在健康领域布局的竞争方向
医疗行业做了六年多的时间,今年到了井喷的阶段,腾讯先是入股了丁香园,然后又一亿美金融资挂号网,春雨医生获得5000万美元的C轮融资,这是要上市的节奏.. 从互联网战略上,健康网和医疗网都是做资料刚开始 ...
- 【linux】亲测成功_CentOS7.2/rhel7.2 忘记root密码及重置root密码的方法?
本文转自:https://www.jb51.net/article/146320.htm CentOS 7 root密码的重置方式和CentOS 6完全不一样,以进入单用户模式修改root密码为例. ...
- Redis数据类型及操作详解
Redis数据库,是nosql的一种.与传统关系型数据库(如mysql.sqlserver等)相比,他在处理大数据量上相当有优势,扩展性和可用性高,这是传统型数据库所达不到的. Redis是一个key ...
- 安装和配置hadoop集群步骤
hadoop集群的安装步骤和配置 hadoop是由java语言编写的,首先我们肯定要在电脑中安装jdk,配置好jdk的环境,接下来就是安装hadoop集群的步骤了,在安装之前需要创建hadoop用户组 ...
- BZOJ 1101 [POI2007]Zap | 第一道莫比乌斯反(繁)演(衍)
题目: http://www.lydsy.com/JudgeOnline/problem.php?id=1101 题解: http://www.cnblogs.com/mrha/p/8203612.h ...
- BZOJ5343 [Ctsc2018]混合果汁 【二分 + 主席树】
题目链接 BZOJ5343 题解 明显要二分一下美味度,然后用尽量少的价格去购买饮料,看看能否买到\(L\)升,然后看看能否控制价格在\(g\)内 尽量少的价格,就优先先选完便宜的饮料,由于询问的是一 ...
- 【luogu 1439 最长公共子序列】
题目描述 给出1-n的两个排列P1和P2,求它们的最长公共子序列. 输入输出格式 输入格式: 第一行是一个数n, 接下来两行,每行为n个数,为自然数1-n的一个排列. 输出格式: 一个数,即最长公共子 ...
- myisam_sort_buffer_size vs sort_buffer_size
Q: I am MySQL on server with 6GB RAM. I need to know what is the difference between myisam_sort_buff ...
- linux内存条排查
已发现2个内存错误,应用名称(kernel:),日志内容(hangzhou-jishuan-DDS0248 kernel: sbridge: HANDLING MCE MEMORY ERROR han ...
- Windows2008 – Task Scheduler – ‘Action “C:\Windows\SYSTEM32\cmd.exe” with return code 1’
Remediation Edit Task Let us make the necessary changes, which is to specify the Start folder. Here ...