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. QWebView使用

      问题: 开始编译的时候在pro文件中要加上 QT += core gui webkitwidgets 文件使用部分加上 #include<QtWebKitWidgets/QWebView&g ...

  2. C语言两种方式实现矩阵的转置

    #include"stdio.h" typedef struct{ int i,j; int v; }Triple; typedef struct{ Triple date[]; ...

  3. Oracle ASM 常用命令

    01, 查看磁盘路径 select name,path,group_number from v$asm_disk_stat; 02, 查看磁盘组信息 select state,name,type,to ...

  4. Linux私房菜阅读笔记

    在线man:http://www.linux.com/linux-man-pages 计算机硬件五大单元: 1.输入 2.输出 3.控制单元 4.算数逻辑单元 5.内存   CPU种类 1.RISC( ...

  5. ife task0003学习笔记(一):JavaScript作用域

    在学习JavaScript作用域概念之前,首先要明白几个概念:执行环境.变量对象.作用域链. 一.JavaScript执行环境(execution context): 在<Professiona ...

  6. (转)vs2010 vs2013等vs中如何统计整个项目的代码行数

    在一个大工程中有很多的源文件和头文件,我如何快速统计总行数? ------解决方案-------------------- b*[^:b#/]+.*$ ^b*[^:b#/]+.*$ ctrl + sh ...

  7. 【学习笔记】2017年7月18日MySQL测试:模拟QQ数据库

    模拟测试: QQ数据库管理 一.创建数据库并添加关系和测试数据 ##创建QQ数据库,完成简单的测试 #创建数据库 DROP DATABASE IF EXISTS MyQQ; CREATE DATABA ...

  8. MySQL出现时区错误的解决方法

    目录 环境 问题 分析 解决方法 环境 windows10 MySQL 8.0.13 IDEA 问题 The server time zone value 'Öйú±ê׼ʱ¼ä' is unre ...

  9. TCP/IP五层模型详解

    将应用层,表示层,会话层并作应用层,从tcp/ip五层协议的角度来阐述每层的由来与功能,搞清楚了每层的主要协议就理解了整个互联网通信的原理. 首先,用户感知到的只是最上面一层应用层,自上而下每层都依赖 ...

  10. 基于CSS3的3D旋转效果

    自从有了html5和css3,好多以前只能想想的华丽效果都可以上手实现了.3D 转换(个人认为3D变换更贴切^)就是其中之一.关于3D转换,可以阅读CSS3 3D transform变换,不过如此,文 ...