[topcoder]KingdomReorganization
http://community.topcoder.com/stat?c=problem_statement&pm=11282&rd=14724
这道题是最小生成树,但怎么转化是关键。首先是把所有的路都destroy掉,得到基本的MassiveCost,然后在选MST的过程中,遇上这些边相当于还回去,它们的cost就是-destroy[i][j]。这样转化完毕。
用Kruskal来做,注意生成Edge的过程,第二层循环j要从i+1开始,主要是避免把i到i的路也放进去。
此题算是比较经典的K算法的题了。(没有用并查集)
import java.util.*;
public class KingdomReorganization
{
public int getCost(String[] kingdom, String[] build, String[] destroy)
{
ArrayList<Edge> edges = new ArrayList<Edge>();
int len = kingdom.length;
int basicCost = 0;
// create edges with cost
for (int i = 0; i < len; i++)
{
for (int j = i+1; j < len; j++)
{
Edge edge = new Edge();
edge.a = i; edge.b = j;
if (kingdom[i].charAt(j) == '0')
{
edge.cost = getValue(build, i, j);
}
else
{
int tmp = getValue(destroy, i, j);
basicCost += tmp;
edge.cost = -tmp;
}
edges.add(edge);
}
}
// Kruskal algo
Collections.sort(edges);
int[] color = new int[len];
for (int i = 0; i < len; i++)
{
color[i] = i;
}
int cost = basicCost;
for (int i = 0; i < edges.size(); i++)
{
Edge e = edges.get(i);
if (color[e.a] == color[e.b]) continue;
cost += e.cost;
int oldColor = color[e.a];
for (int k = 0; k < len; k++)
{
if (color[k] == oldColor)
{
color[k] = color[e.b];
}
}
}
return cost;
} private int getValue(String[] costs, int i, int j)
{
char c = costs[i].charAt(j);
if (c >= 'A' && c <= 'Z')
{
return c - 'A';
}
else
return c - 'a' + 26;
}
} class Edge implements Comparable<Edge>
{
public int a;
public int b;
public int cost;
public int compareTo(Edge rhs)
{
return this.cost - rhs.cost;
}
}
[topcoder]KingdomReorganization的更多相关文章
- TopCoder kawigiEdit插件配置
kawigiEdit插件可以提高 TopCoder编译,提交效率,可以管理保存每次SRM的代码. kawigiEdit下载地址:http://code.google.com/p/kawigiedit/ ...
- 记第一次TopCoder, 练习SRM 583 div2 250
今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...
- TopCoder比赛总结表
TopCoder 250 500 ...
- Topcoder几例C++字符串应用
本文写于9月初,是利用Topcoder准备应聘时的机试环节临时补习的C++的一部分内容.签约之后,没有再进行练习,此文暂告一段落. 换句话说,就是本文太监了,一直做草稿看着别扭,删掉又觉得可惜,索性发 ...
- TopCoder
在TopCoder下载好luncher,网址:https://www.topcoder.com/community/competitive%20programming/ 选择launch web ar ...
- TopCoder SRM 596 DIV 1 250
body { font-family: Monospaced; font-size: 12pt } pre { font-family: Monospaced; font-size: 12pt } P ...
- 求拓扑排序的数量,例题 topcoder srm 654 div2 500
周赛时遇到的一道比较有意思的题目: Problem Statement There are N rooms in Maki's new house. The rooms are number ...
- TopCoder SRM 590
第一次做TC,不太习惯,各种调试,只做了一题...... Problem Statement Fox Ciel is going to play Gomoku with her friend ...
- Topcoder Arena插件配置和训练指南
一. Arena插件配置 1. 下载Arena 指针:http://community.topcoder.com/tc?module=MyHome 左边Competitions->Algorit ...
随机推荐
- Redis操作Set工具类封装,Java Redis Set命令封装
Redis操作Set工具类封装,Java Redis Set命令封装 >>>>>>>>>>>>>>>>& ...
- web.config详解(配置文件节点说明)
转载:http://www.zzzj.com/html/20081110/67614.html web.config文件是一个XML文件,它的根结点是<configuration>,在&l ...
- 20151223jquery学习笔记--Ajax表单提交
传统的表单提交, 需要多次跳转页面, 极大的消耗资源也缺乏良好的用户体验. 而这款form.js 表单的 Ajax 提交插件将解决这个问题.一. 核心方法官方网站: http://malsup.com ...
- Candence下对“跨页连接器(off-page connector)”进行批量重命名的方法
parts.ports.alias等等均可以在“属性编辑器(Property Editor)”中进行查看编辑,并通过复制到Excel等表格软件来进行批量修改.之后再粘贴回去的方法进行批量编辑.但是“跨 ...
- oracle级联删除 触发器
CREATE TABLE STUDENT( --创建学生表 ID NUMBER(10) PRIMARY KEY, --主键ID SNAME VARCHAR2(20), CLASSNAME V ...
- iOS Crash文件的解析
iOS Crash文件的解析 开发程序的过程中不管我们已经如何小心,总是会在不经意间遇到程序闪退.脑补一下当你在一群人面前自信的拿着你的App做功能预演的时候,流畅的操作被无情地Crash打断.联想起 ...
- mac最常用终端命令
1分钟,快速复习下: pwd (显示当前所在路径) ls -l (列出文件的详细信息,如创建者,创建时间,文件的读写权限列表等等) touch `filename`(创建文件) open `file ...
- java连接远程服务器之manyged-ssh2 (windows和linux)
一.所需要的jar包 需要借助Ganymed SSH的jar包: ganymed-ssh2-262.jar 下载地址: http://www.ganymed.ethz.ch/ssh2/ API详情: ...
- HDU 2809 God of War(DP + 状态压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2809 题目大意:给出战神吕布的初始攻击力ATI.防御力DEF.生命值HP.每升一级增加的攻击力In_A ...
- What is the Xcopy Command?:
Quote from: http://pcsupport.about.com/od/commandlinereference/p/xcopy-command.htm The xcopy command ...