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的更多相关文章

  1. TopCoder kawigiEdit插件配置

    kawigiEdit插件可以提高 TopCoder编译,提交效率,可以管理保存每次SRM的代码. kawigiEdit下载地址:http://code.google.com/p/kawigiedit/ ...

  2. 记第一次TopCoder, 练习SRM 583 div2 250

    今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...

  3. TopCoder比赛总结表

    TopCoder                        250                              500                                 ...

  4. Topcoder几例C++字符串应用

    本文写于9月初,是利用Topcoder准备应聘时的机试环节临时补习的C++的一部分内容.签约之后,没有再进行练习,此文暂告一段落. 换句话说,就是本文太监了,一直做草稿看着别扭,删掉又觉得可惜,索性发 ...

  5. TopCoder

    在TopCoder下载好luncher,网址:https://www.topcoder.com/community/competitive%20programming/ 选择launch web ar ...

  6. TopCoder SRM 596 DIV 1 250

    body { font-family: Monospaced; font-size: 12pt } pre { font-family: Monospaced; font-size: 12pt } P ...

  7. 求拓扑排序的数量,例题 topcoder srm 654 div2 500

    周赛时遇到的一道比较有意思的题目: Problem Statement      There are N rooms in Maki's new house. The rooms are number ...

  8. TopCoder SRM 590

     第一次做TC,不太习惯,各种调试,只做了一题...... Problem Statement     Fox Ciel is going to play Gomoku with her friend ...

  9. Topcoder Arena插件配置和训练指南

    一. Arena插件配置 1. 下载Arena 指针:http://community.topcoder.com/tc?module=MyHome 左边Competitions->Algorit ...

随机推荐

  1. Oracle--常见Exception

    1.  错 误 名 称 错误代码    错 误 含 义 2.  CURSOR_ALREADY_OPEN ORA_06511   试图打开已经打开的游标 3.  INVALID_CURSOR  ORA_ ...

  2. C#Combobox显示名称保存代码

    private void Form1_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add ...

  3. android 中在CMD中查看sqlite

    今天第一次学习Sqlite,在代码中执行了sql,但是不知道在上面地方才能直观的查看sqlite中的数据,下面是查看资料后的找到的查看方法: 通过上述可以从cmd进入数据库查看原文地址:http:// ...

  4. php 5.3 配置mssql笔记

    参考URL  https://docs.moodle.org/29/en/Installing_MSSQL_for_PHP#Using_FreeTDS_on_Debian_Lenny 第一步,下载相应 ...

  5. ios6-7以后用户开热点后的屏幕适配

    // 排版时,注意logical coordinate space和device coordinate space的区别,注意frame和bounds的区别! - (void)loadView { / ...

  6. win8.1企业版更新到win10解决方案

    最近想把自己的win8.1更新成win10,发现月底就要免费更新了,由于我的电脑是企业版,官方不提供企业版的免费升级,所以用电脑管家或者360老是提示不对.我就百度了在注册表里面改成了专业版的,接着继 ...

  7. Inline functions

    Problems: (Page 372) There are two problems with the use of proprocessor macros in C++. The first is ...

  8. .net闭包的应用

    这里体现出闭包的数据共享 , , , , , , , , , }; ; ; values.ToList().ForEach(s => result1 += s); values.ToList() ...

  9. css3动画使用技巧之—border旋转时的应用。

    <html> <head> <title>css3动画border旋转时的应用.</title> <meta charset="UTF- ...

  10. obj-c 坑

    BOOL,使用8位存储空间,具有YES和NO值,如果赋值微长于8位的变量,那么只有低位字节会用作BOOL值,例如8960=0x2300,低8位为0,BOOL为NO.