Description

You are a boss who has N employers. One day, you have a message and you want to tell it to M employers of yours. As you are a funny man, you want to choose some of the M employers and tell them the message, and then let them tell other employers so that all the M know the message (you don't care whether the other N − M employers know the message or not). Employers who know the message can tell it to anyone else. The ith employer telling the jth employer causes a cost of Cij and you want to know what the minimal total cost is.

Input

The input data consists of several cases. The first line contains one integer, T, indicating the number of test cases. For each test case, the first line contains two integers, N (10 ≤ N ≤ 50) and M (5 ≤ M ≤ 10), described above. The employers are numbered from 1. In the next N lines, each line contains N integers. The jth integer in the ith line is Cij (1 ≤ Cij ≤ 1000, Cii = 0, Cij = Cji). In the next line, there are 2 × M integers. The 2 × i − 1th integer is the index of the ith employer who you want to know the message. The 2 × ith is the cost for you to tell him the message. All cost values are between 1 and 1000.

Output

For each test case, print one integer in one line, W, indicating the mininal total cost.

Sample Input

1
10 5
0 42 468 501 479 706 492 605 719 668
42 0 335 170 359 146 996 903 896 300
468 335 0 725 963 282 943 154 448 36
501 170 725 0 465 828 828 293 727 895
479 359 963 465 0 962 437 383 772 704
706 146 282 828 962 0 392 422 539 812
492 996 943 828 437 392 0 717 870 323
605 903 154 293 383 422 717 0 913 334
719 896 448 727 772 539 870 913 0 674
668 300 36 895 704 812 323 334 674 0
5 142 2 54 9 148 3 158 8 60

Sample Output

558

Hint

现场只有一个队做出来了,想了半天没做出来。

首先是费用流,保证每个点都被访问,那么会使得所有点都有一个连到源点,汇点,那么他们的权值一定至少全被加起来了。

最短路,枚举选哪些点,跑到其他所有点,sum + dist[v],会有重复路径,还要把所有重复路径的点删掉。太难搞咯~~~

csu 1965的更多相关文章

  1. csu 1812: 三角形和矩形 凸包

    传送门:csu 1812: 三角形和矩形 思路:首先,求出三角形的在矩形区域的顶点,矩形在三角形区域的顶点.然后求出所有的交点.这些点构成一个凸包,求凸包面积就OK了. /************** ...

  2. CSU 1503 点到圆弧的距离(2014湖南省程序设计竞赛A题)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1503 解题报告:分两种情况就可以了,第一种是那个点跟圆心的连线在那段扇形的圆弧范围内,这 ...

  3. CSU 1120 病毒(DP)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1120 解题报告:dp,用一个串去更新另一个串,递推方程是: if(b[i] > a ...

  4. CSU 1116 Kingdoms(枚举最小生成树)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1116 解题报告:一个国家有n个城市,有m条路可以修,修每条路要一定的金币,现在这个国家只 ...

  5. CSU 1113 Updating a Dictionary(map容器应用)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1113 解题报告:输入两个字符串,第一个是原来的字典,第二个是新字典,字典中的元素的格式为 ...

  6. CSU 1333 Funny Car Racing (最短路)

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1333 解题报告:一个图里面有n个点和m条单向边,注意是单向边,然后每条路开a秒关闭b秒 ...

  7. CSU 1337 搞笑版费马大定理(2013湖南省程序设计竞赛J题)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1337 解题报告:虽然x和y的范围都是10^8,但是如果a 是大于1000的话,那么a^3 ...

  8. CSU 1328 近似回文词(2013湖南省程序设计竞赛A题)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1328 解题报告:中文题题意就不说了.还好数据不大,只有1000,枚举回文串的中心位置,然 ...

  9. 三分 --- CSU 1548: Design road

    Design road Problem's Link:   http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1548 Mean: 目的:从(0,0)到 ...

随机推荐

  1. Vue如何封装多个全局过滤器到一个文件

    #### 在写vue项目时,所用的过滤器很多时,把所有的过滤器方法封装在一个文件中,然后导出,并绑定在vue实例上 1.在src下创建filters文件夹,并新建index.js文件 2. index ...

  2. 案例48-crm练习利用spring管理service和dao层的对象

    1 导包 2 将 Service 对象以及 Dao 对象配置到 spring 容器 <?xml version="1.0" encoding="UTF-8" ...

  3. java中的输入输出方法

    输入 import java.util.Scanner; public class EnterTest { public static void main(String[] args) { //主方法 ...

  4. flex buider 4.6 打开设计模式(designer)时提示内存不足错误的解决办法

    先申明,此方法只适用于flex builder 4.6及以下版本, flex builder 4.7以后已经完全取消了designer功能,是没有办法补救的. 1. 首先下载APE文件,这个文件好像是 ...

  5. 介绍几种搭建Dojo环境的方法

    Hello World! 的时间到了,在你所学过的众多语言中,哪个不是从此学起的呢?但在此之前,我们要先构建一个开发环境,如同刚开始学习Java的时候,还是需要我们先安装JDK.配置好环境变量等等,H ...

  6. JavaScript的作用域(Scope)和上下文(Context)

    JavaScript对于作用域(Scope)和上下文(Context)的实现是这门语言的一个非常独到的地方,部分归功于其独特的灵活性. 函数可以接收不同的的上下文和作用域.这些概念为JavaScrip ...

  7. 微信token验证源码分享(c#版)

    在开发时遇到一个问题: 上线后提交申请微信提示"您的服务器没有正确响应token验证...",我查看日志发现根本就没有接收到来自微信的参数. 后来我又记录了微信请求方式和请求的字符 ...

  8. ubuntu下安装MySQL8.0

    为了一劳永逸不每次都到处找资料,花了一下午时间做了这些.其中大部分是根据官方手册来的,后面部分谢谢大佬的帮助,超开心. 一.首先,将MySQL APT存储库添加到系统的软件存储库列表中 1.转到htt ...

  9. MyBatis 中 sqlmapconfig核心标签说明以及配置

    文件介绍 对于 MyBatis 最核心的全局配置文件是 sqlmapConfig.xml 文件,其中包含了数据库的连接配置信息.Mapper 映射文件的加载路径.全局参数.类型别名等. 配置项详解 标 ...

  10. netty笔记(一)--Demo

    Netty是一个Java开源框架,用于传输数据.由server和client组成,封装了Java nio,支持TCP, UDP等协议.这里写了一Demo EchoClientHandler.java ...