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

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 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
 
还是坑人的格式问题...dp[i] = max(dp[i],dp[j]+v[i])
#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上的最长路)的更多相关文章

  1. NYOJ_矩形嵌套(DAG上的最长路 + 经典dp)

    本题大意:给定多个矩形的长和宽,让你判断最多能有几个矩形可以嵌套在一起,嵌套的条件为长和宽分别都小于另一个矩形的长和宽. 本题思路:其实这道题和之前做过的一道模版题数字三角形很相似,大体思路都一致,这 ...

  2. UVa 10285 最长的滑雪路径(DAG上的最长路)

    https://vjudge.net/problem/UVA-10285 题意: 在一个R*C的整数矩阵上找一条高度严格递减的最长路.起点任意,但每次只能沿着上下左右4个方向之一走一格,并且不能走出矩 ...

  3. Vulnerable Kerbals CodeForces - 772C【拓展欧几里得建图+DAG上求最长路】

    根据拓展欧几里得对于同余方程 $ax+by=c$ ,有解的条件是 $(a,b)|c$. 那么对于构造的序列的数,前一个数 $a$  和后一个数 $b$ ,应该满足 $a*x=b(mod m)$ 即 $ ...

  4. HDU 4109 Instrction Arrangement(DAG上的最长路)

    把点编号改成1-N,加一点0,从0点到之前任意入度为0的点之间连一条边权为0的边,求0点到所有点的最长路. SPFA模板留底用 #include <cstdio> #include < ...

  5. uva103(最长递增序列,dag上的最长路)

    题目的意思是给定k个盒子,每个盒子的维度有n dimension 问最多有多少个盒子能够依次嵌套 但是这个嵌套的规则有点特殊,两个盒子,D = (d1,d2,...dn) ,E = (e1,e2... ...

  6. POJ 1949 Chores(DAG上的最长路 , DP)

    题意: 给定n项任务, 每项任务的完成用时t和完成每项任务前需要的k项任务, 求把所有任务完成的最短时间,有当前时间多项任务都可完成, 那么可以同时进行. 分析: 这题关键就是每项任务都会有先决条件, ...

  7. HDU 1224 无环有向最长路

    用bellman_ford的方法,将中间不断取较小值,修改为取较大值就可以了 #include <cstdio> #include <cstring> #include < ...

  8. HDU 3249 Test for job (有向无环图上的最长路,DP)

     解题思路: 求有向无环图上的最长路.简单的动态规划 #include <iostream> #include <cstring> #include <cstdlib ...

  9. hdu 1534(差分约束+spfa求最长路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1534 思路:设s[i]表示工作i的开始时间,v[i]表示需要工作的时间,则完成时间为s[i]+v[i] ...

随机推荐

  1. 【翻译】介绍 ASP.NET Core 中的 Razor Pages

    介绍 ASP.NET Core 中的 Razor Pages 原文地址:Introduction to Razor Pages in ASP.NET Core         译文地址:介绍 asp. ...

  2. HashMap和Hashtable的区别(转载)

    转载声明:转载自原文http://www.importnew.com/7010.html HashMap和Hashtable的比较是Java面试中的常见问题,用来考验程序员是否能够正确使用集合类以及是 ...

  3. Python创建目录文件夹

    Python对文件的操作还算是方便的,只需要包含os模块进来,使用相关函数即可实现目录的创建. 主要涉及到三个函数 1.os.path.exists(path) 判断一个目录是否存在 2.os.mak ...

  4. 时间戳转换成日期的js

    在项目开发过程中,我们常常需要把时间戳转换成日期.下面这个是我一直使用的js方法,希望能帮助到有需要的朋友.大家如果有更好的方法,请多多指教! js代码如下: //时间戳转换成日期 function ...

  5. el-checkbox根据是否被选中执行不同的操作

    直接给el-checkbox绑定点击事件是没有效果的,因为它会被解析成其他形式的html,el-checkbox只是一个类名,因此,使用ts和jquery动态绑定事件: mounted() { $(& ...

  6. Div+Css中transparent制作奥运五环

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. visio应用程序相关设置-选项-视图

    1.是否显示"新建"选项卡,可读/写 ApplicationSettings.ShowChooseDrawingTypePane m_Visio.Window.Applicatio ...

  8. GDI+小例子

    原文链接地址:http://www.cnblogs.com/chuanzifan/archive/2011/11/26/2264507.html 1.在stdafx.h中 #include <G ...

  9. [Leetcode] Reverse linked list ii 反转链表

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given1->2 ...

  10. POJ 1050 To the Max 二维最大子段和

    To the MaxTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 52281 Accepted: 27633Description ...