简单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. Jquery+ashx实现Ajax

    一 Ajax的实现方式 1.使用一般的webform,在页面用jQuery ajax调用,再从取得的html数据中取得<body>内的内容,写入DOM 优点:不用改变现有的asp.net开 ...

  2. 手机酷派4G5316 5313s 黑砖 求转成功 9008端口 9006端口 少走弯路选对镜像

    首先要有资料 里面有教程  http://pan.baidu.com/s/1bpjxP6n 1.用其他手机 or u 盘往sd卡放进“强制进入下载模式的文件” 2. 驱动 3.刷机工具 下载镜像   ...

  3. OpenMP入门教程(二)

    OpenMP API概述 OpenMP由三部分组成: 编译指令(19) 运行时库程序(32) 环境变量(9) 后来的API包含同样的三个组件,只是三者的数量都有所增加. 编译器指令 OpenMP编译器 ...

  4. AttributeError: 'list' object has no attribute 'extends' && list详解

    拼写错误 是extend  而不是extends 出错demo: In [27]: c = [2,3] In [28]: c.extends([5]) ------------------------ ...

  5. webpack的详细介绍和使用

    // 一个常见的`webpack`配置文件 const webpack = require('webpack'); const HtmlWebpackPlugin = require('html-we ...

  6. 第3节 mapreduce高级:4、倒排索引的建立

    倒排索引建立 需求分析 需求:有大量的文本(文档.网页),需要建立搜索索引 最终实现的结果就是哪个单词在哪个文章当中出现了多少次 思路分析: 首选将文档的内容全部读取出来,加上文档的名字作为key,文 ...

  7. 在 VS2015+EF6.0中使用Mysql 遇到的坑

    1)首先是要在vs2015中安装mysql Database 默认是不存在的 1)下载mysql-connector-net-6.9.9.msi    地址:https://dev.mysql.com ...

  8. python pandas读写excel

    import pandas as pd import numpy as np df = pd.read_csv("result.csv") # csv # df = pd.read ...

  9. react入门(下)

    react生命周期 1. 组件的三个生命周期状态: * Mount:插入真实 DOM * Update:被重新渲染 * Unmount:被移出真实 DOM2. React 为每个状态都提供了两种勾子( ...

  10. 合并多个MP4文件

    把多个MP4文件连接起来的方法与音频文件不太一样,比较有效的方法是: $ cat mylist.txt file '/path/to/file1' file '/path/to/file2' file ...