简单dp

#include <stdio.h>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
typedef unsigned long long ull;
const int N = 105;
int n, m, a[N];
int s[N][N], dp[2][N];
void pt(int x ){
for(int i = 1; i <= m; i++)printf("%d ", dp[x][i]); puts("");
}
int solve(){
int cur = 0;
if(a[1] < 0)
memset(dp[cur], 0, sizeof dp[cur]);
else
{
memset(dp[cur], -1, sizeof dp[cur]);
dp[cur][a[1]] = 0;
}
for(int i = 2; i <= n; i++)
{
cur ^= 1;
memset(dp[cur], -1, sizeof dp[cur]);
if(a[i]>0)
{
for(int j = 1; j <= m; j++)
if(dp[cur^1][j] != -1)
dp[cur][a[i]] = max(dp[cur][a[i]], dp[cur^1][j] + s[j][a[i]]);
}
else
{
for(int j = 1; j <= m; j++)
if(dp[cur^1][j]!=-1)
for(int k = 1; k <= m; k++)
dp[cur][k] = max(dp[cur][k], dp[cur^1][j]+s[j][k]);
}
// cout<<i<<":"; pt(cur);
}
int ans = 0;
for(int i = 1; i <= m; i++)
ans = max(ans, dp[cur][i]);
return ans;
}
void input(){
cin>>n >> m;
for(int i = 1; i <= m; i++)
for(int j = 1; j <= m; j++)
scanf("%d", &s[i][j]);
for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
}
int main(){
int T; cin>>T;
while(T--){
input();
cout<<solve()<<endl;
}
return 0;
}
/*
9
3 3
1 1 5
5 1 100
1 5 1
-1 -1 -1 */

HDU 5074 Hatsune Miku 2014 Asia AnShan Regional Contest dp(水的更多相关文章

  1. dp --- 2014 Asia AnShan Regional Contest --- HDU 5074 Hatsune Miku

    Hatsune Miku Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=5074 Mean: 有m种音符(note),现在要从 ...

  2. HDU 5074 Hatsune Miku(2014鞍山赛区现场赛E题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5074 解题报告:给出一个长度为n的序列,例如a1,a2,a3,a4......an,然后这个序列的美丽 ...

  3. 2014 Asia AnShan Regional Contest --- HDU 5073 Galaxy

    Galaxy Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=5073 Mean: 在一条数轴上,有n颗卫星,现在你可以改变k颗 ...

  4. 2014 Asia AnShan Regional Contest --- HDU 5078 Osu!

    Osu! Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=5078 Mean: 略. analyse: 签到题,直接扫一遍就得答 ...

  5. HDU 5073 Galaxy 2014 Asia AnShan Regional Contest 规律题

    推公式 #include <cstdio> #include <cmath> #include <iomanip> #include <iostream> ...

  6. hdu5071 2014 Asia AnShan Regional Contest B Chat

    模拟题: add的时候出现过的则不再添加 close的时候会影响到top rotate(Prior.Choose)的时候会影响到top /*============================== ...

  7. hdu5072 2014 Asia AnShan Regional Contest C Coprime

    最后一次参加亚洲区…… 题意:给出n(3 ≤ n ≤ 105)个数字,每个数ai满足1 ≤ ai ≤ 105,求有多少对(a,b,c)满足[(a, b) = (b, c) = (a, c) = 1] ...

  8. hdu oj 3127 WHUgirls(2009 Asia Wuhan Regional Contest Online)

    WHUgirls Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total ...

  9. 2014 ACM-ICPC Asia Anshan Regional Contest(Online Version)

    题目I - Osu! - HDU 5078 题目分析:最水的一道题吧,求两点间的距离和时间差值的最大比值 #include<stdio.h> #include<math.h> ...

随机推荐

  1. jboss中JVM监控

    1)打开 http://server-name-or-ip/jmx-console/HtmlAdaptor2)在 jboss.system 节点找到 type=ServerInfo ,点击进入3)找到 ...

  2. bat获取注册表值

    @echo off Setlocal enabledelayedexpansion for /f "skip=2 delims=: tokens=1,*" %%i in ('reg ...

  3. photoshop cs6安装和破解步骤

    http://tieba.baidu.com/p/4791130877 http://www.frontopen.com/1181.html

  4. ubuntu18.04 python版本切换

    update-alternatives是ubuntu系统中专门维护系统命令链接符的工具,通过它可以很方便的设置系统默认使用哪个命令.哪个软件版本,比如,我们在系统中同时安装了python2.7和pyt ...

  5. Android(java)学习笔记195:ContentProvider使用之添加数据到联系人(掌握)

    1.添加联系人逻辑思路 (1)首先在raw_contacts创建一个新的id (2)在data表里面添加这个id对应的数据 2.下面通过一个案例,说明一下如何添加一条数据到联系人: (1)首先我们关注 ...

  6. C# 获取文件编码

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  7. Matrix (二分套二分

    Given a N × N matrix A, whose element in the i-th row and j-th column Aij is an number that equals i ...

  8. C 语言指针 5 分钟教程

    指针.引用和取值 什么是指针?什么是内存地址?什么叫做指针的取值?指针是一个存储计算机内存地址的变量.在这份教程里“引用”表示计算机内存地址.从指针指向的内存读取数据称作指针的取值.指针可以指向某些具 ...

  9. BZOJ 4823 Luogu P3756 老C的方块 染色+最小割

    题面太长了请各位自行品尝—>老C的方块 分析: 我们要解决掉所有使人弃疗的组合,还要保证花费最小,容易想到最小割(当然你要是想费用流的话,我们就没办法定义流量了) 我们来分析一下那些令人弃疗的组 ...

  10. [Python3网络爬虫开发实战] 1.4.1-MySQL的安装

    MySQL是一个轻量级的关系型数据库,本节中我们来了解下它的安装方式. 1. 相关链接 官方网站:https://www.mysql.com/cn 下载地址:https://www.mysql.com ...