[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 ...
随机推荐
- JUnit4注解基本介绍
@After If you allocate external resources in a Before method you need to release them after the test ...
- web页面的生命周期
1.先上几张原理图着重理解: 现在针对第四副图原理进行解析: 流程: 1.浏览器发送请求 2.服务器软件(IIS)接收,它最终的目的就是为了向客户输出它请求的动态页面生成的html代码. 3.服务器不 ...
- 华为j2ee面试题
http://blog.csdn.net/chow__zh/article/details/7741312 java基础1.垃圾回收的优点和原理. Java语言中一个显著的特点就是引入了垃圾 ...
- iOS控件——UIView与UIImageView播放动画的实现方法
1.UIView //初始状态 [UIView animateWithDuration:(int) animations:^{ //最终状态 }completion:^(BOOL finished){ ...
- 安装aptana插件报Error opening the editor. java.lang.NullPointerException
Aptana的官方网站下载eclipse的插件: http://update.aptana.com/update/studio/3.2/ ,可以在线安装也可以下载插件后再安装,我是以在线的形式安装的 ...
- Java工具类:获取long型唯一ID
直接上代码: import java.text.SimpleDateFormat; import java.util.Date; /** * 获取long型唯一ID */ public class I ...
- 开始学习requirejs+easyui的使用.
开始学习requirejs+easyui的使用. 目录结构: |-project |-easyui01 |-js |-main.js |-index.html |-libs libs目录下放入的是ea ...
- ZOJ 1025 Wooden Sticks(快排+贪心)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=25 题目大意:机器运送n个木条,每个木条有一个长度和重量.运送第一根木 ...
- 浪潮MegaCli
再总结: 一般,清RAID再做RAID,安装完毕后: ./MegaCli64 -PDList -aALL | egrep 'Slot|Enclosure Device' ...
- Codevs 1066 引水入城 2010年NOIP全国联赛提高组
1066 引水入城 2010年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description 在一个遥远的国度 ...