hdoj 5074
Today you want to compose a song, which is just a sequence of notes. There are only m different notes provided in the package. And you want to make a song with n notes.

Also, you know that there is a system to evaluate the beautifulness of a song. For each two consecutive notes a and b, if b comes after a, then the beautifulness for these two notes is evaluated as score(a, b).
So the total beautifulness for a song consisting of notes a1, a2, . . . , an, is simply the sum of score(ai, ai+1) for 1 ≤ i ≤ n - 1.
Now, you find that at some positions, the notes have to be some specific ones, but at other positions you can decide what notes to use. You want to maximize your song’s beautifulness. What is the maximum beautifulness you can achieve?
For each test case, the first line contains two integers n(1 ≤ n ≤ 100) and m(1 ≤ m ≤ 50) as mentioned above. Then m lines follow, each of them consisting of m space-separated integers, the j-th integer in the i-th line for score(i, j)( 0 ≤ score(i, j) ≤ 100). The next line contains n integers, a1, a2, . . . , an (-1 ≤ ai ≤ m, ai ≠ 0), where positive integers stand for the notes you cannot change, while negative integers are what you can replace with arbitrary notes. The notes are named from 1 to m.
5 3
83 86 77
15 93 35
86 92 49
3 3 3 1 2
10 5
36 11 68 67 29
82 30 62 23 67
35 29 2 22 58
69 67 93 56 11
42 29 73 21 19
-1 -1 5 -1 4 -1 -1 -1 4 -1
625
分析:记dp[i][j]为第i位取第j位的最大值
#include "stdio.h"
#include "string.h"
#define MAX 110
int a[MAX];
int dp[MAX][MAX],g[MAX][MAX];
int max(int a,int b)
{
return a>b?a:b;
}
int main()
{
int t;
int i,j,k,n,m;
int ans;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=; i<=m; i++)
for(j=; j<=m; j++)
scanf("%d",&g[i][j]);
for(i=; i<=n; i++)
scanf("%d",&a[i]);
memset(dp,,sizeof(dp));
for(i=; i<=n; i++)
{
if(a[i]>)
{
if(a[i-]>)
dp[i][a[i]]=max(dp[i][a[i]],dp[i-][a[i-]]+g[a[i-]][a[i]]);
else
{
for(j=; j<=m; j++)
dp[i][a[i]]=max(dp[i][a[i]],dp[i-][j]+g[j][a[i]]);
}
}
else
{
if(a[i-]>)
{
for(j=; j<=m; j++)
dp[i][j]=max(dp[i][j],dp[i-][a[i-]]+g[a[i-]][j]);
}
else
{
for(j=; j<=m; j++)
for(k=; k<=m; k++)
dp[i][k]=max(dp[i][k],dp[i-][j]+g[j][k]);
}
}
}
ans=;
for(i=; i<=m; i++)
ans=max(ans,dp[n][i]);
printf("%d\n",ans);
}
return ;
}
hdoj 5074的更多相关文章
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDOJ 2317. Nasty Hacks 模拟水题
Nasty Hacks Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- HDOJ 1326. Box of Bricks 纯水题
Box of Bricks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDOJ 1004 Let the Balloon Rise
Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...
- hdoj 1385Minimum Transport Cost
卧槽....最近刷的cf上有最短路,本来想拿这题复习一下.... 题意就是在输出最短路的情况下,经过每个节点会增加税收,另外要字典序输出,注意a到b和b到a的权值不同 然后就是处理字典序的问题,当松弛 ...
- HDOJ(2056)&HDOJ(1086)
Rectangles HDOJ(2056) http://acm.hdu.edu.cn/showproblem.php?pid=2056 题目描述:给2条线段,分别构成2个矩形,求2个矩形相交面 ...
- 继续node爬虫 — 百行代码自制自动AC机器人日解千题攻占HDOJ
前言 不说话,先猛戳 Ranklist 看我排名. 这是用 node 自动刷题大概半天的 "战绩",本文就来为大家简单讲解下如何用 node 做一个 "自动AC机&quo ...
- HDU 5074 Hatsune Miku(2014鞍山赛区现场赛E题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5074 解题报告:给出一个长度为n的序列,例如a1,a2,a3,a4......an,然后这个序列的美丽 ...
- 最近点对问题 POJ 3714 Raid && HDOJ 1007 Quoit Design
题意:有n个点,问其中某一对点的距离最小是多少 分析:分治法解决问题:先按照x坐标排序,求解(left, mid)和(mid+1, right)范围的最小值,然后类似区间合并,分离mid左右的点也求最 ...
随机推荐
- SharePoint常用目录介绍
SharePoint常用目录介绍 stsadm命令管理程序目录:C:\Program Files\Common Files\Microsoft Shared\web server extensions ...
- 简介python2.x的编码
python2.x的中文编码真是令人头痛,简单写下自己的一点python编码转换的体会. windows平台用的默认编码格式为gbk >>> s = raw_input() #在wi ...
- Cheatsheet: 2016 06.01 ~ 6.30
Other Swift for the Java guy: Part 1 – Getting Started Building a better code review process Creatin ...
- Cheatsheet: 2016 03.01 ~ 03.31
JAVA Quick Java 8 or Java 7 Dev Environments With Docker Printing arrays by hacking the JVM Mobile H ...
- java简单计算器
写的一个小计算器,留着以后看吧. import java.awt.BorderLayout; import java.awt.Button; import java.awt.Color; import ...
- 简单的canvas时钟
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- understanding ECMAscript 6 ---- block bindings
Traditionally, the way variable declarations work has been one tricky part of programming in javascr ...
- NTFS系统的ADS交换数据流
VC++ 基于NTFS的数据流创建与检测 What are Alternate Streams?(交换数据流) NTFS alternate streams , 或者叫streams,或者叫ADS(w ...
- 获取openid 的步骤
1.引导客户打开 https://open.weixin.qq.com/connect/oauth2/authorize?appid=appid &redirect_uri=https://w ...
- ctags使用细节
在src code目录中运行下面的命令(我自己使用的命令): $ctags --langmap=c++:.h --languages=c++,c,perl,verilog -R 其中,指定cta ...