Zepto Code Rush 2014——Dungeons and Candies
- 题意:
k个点,每一个点都是一个n * m的char型矩阵。对与每一个点,权值为n * m或者找到一个之前的点,取两个矩阵相应位置不同的字符个数乘以w。找到一个序列,使得全部点的权值和最小 - 分析:
首先,这个图是一个无向图。求权值和最小,每一个权值相应的是一条边,且每一个点仅仅能有一个权值即一条边,一个k个边,和生成树非常像,可是须要证明不能有环形。最好还是如果如今有三个点,每一个点的最小边成环,这时候是不能找到一个序列使得每一个点都取到它的最小边值的,所以,k个点k个边不能有环且边值和最小,就是最小生成树。
const int maxn = 1100; char ipt[maxn][11][11];
int dist[maxn][maxn];
int d[maxn], p[maxn];
bool vis[maxn];
int n, m, k, w; int main()
{
// freopen("in.txt", "r", stdin);
while (~RIV(n, m, k, w))
{
CLR(dist, 0);
REP(i, k)
{
vis[i] = false;
d[i] = n * m;
p[i] = -1;
} REP(i, k) REP(j, n)
RS(ipt[i][j]);
REP(i, k) REP(j, k) REP(ii, n) REP(jj, m)
dist[i][j] += (ipt[i][ii][jj] != ipt[j][ii][jj]) * w;
d[0] = 0;
int sum = n * m;
VI ans;
REP(i, k)
{
int M = INF, ind;
REP(j, k)
if (!vis[j] && d[j] < M)
{
ind = j;
M = d[j];
}
vis[ind] = true;
sum += M;
ans.push_back(ind);
REP(j, k)
{
if (!vis[j] && dist[ind][j] < d[j])
{
d[j] = dist[ind][j];
p[j] = ind;
}
}
}
WI(sum);
REP(i, ans.size())
{
cout << ans[i] + 1 << ' ' << p[ans[i]] + 1 << endl;
}
}
return 0;
}
kruskal:
const int maxn = 1100; struct Edge
{
int from, to, dist;
int operator< (const Edge& rhs) const
{
return dist < rhs.dist;
}
Edge (int from = 0, int to = 0, int dist = 0)
{
this->from = from;
this->to = to;
this->dist = dist;
}
}; vector<Edge> G[maxn];
int in[maxn];
vector<Edge> edges;
int fa[maxn];
char ipt[maxn][105];
int diff[maxn][maxn];
int n, m, k, w;
int find(int n)
{
return (n == fa[n]) ? n : (fa[n] = find(fa[n]));
}
void init(int n)
{
REP(i, n)
{
fa[i] = i;
G[i].clear();
in[i] = 0;
}
edges.clear();
}
void AddEdge(int u, int v, int dist)
{
edges.push_back(Edge(u, v, dist));
}
void dfs(int u, int fa)
{
REP(i, G[u].size())
{
Edge& e = G[u][i];
if (e.to != fa)
{
if (e.dist == m * n)
cout << e.to + 1 << ' ' << 0 << endl;
else
cout << e.to + 1 << ' ' << u + 1 << endl;
dfs(e.to, u);
}
}
}
void solve()
{
int ret = 0;
sort(all(edges));
REP(i, edges.size())
{
Edge& e = edges[i];
int ru = find(e.from), rv = find(e.to);
if (ru != rv)
{
fa[ru] = rv;
ret += e.dist;
G[e.from].push_back(Edge(e.from, e.to, e.dist));
G[e.to].push_back(Edge(e.to, e.from, e.dist));
in[e.from]++;
in[e.to]++;
}
}
WI(ret + n * m);
REP(i, k)
{
if (in[i] <= 1)
{
cout << i + 1 << ' ' << 0 << endl;
dfs(i, -1);
break;
}
}
} int judge(int a, int b)
{
int cnt = 0;
REP(i, n * m)
cnt += (ipt[a][i] != ipt[b][i]);
return cnt;
} int main()
{
// freopen("in.txt", "r", stdin);
while (~RIV(n, m, k, w))
{
CLR(diff, 0);
REP(i, k)
{
int len = 0;
REP(j, n)
{
RS(ipt[i] + len);
len = strlen(ipt[i]);
}
}
REP(i, k) REP(j, k) REP(t, n * m)
diff[i][j] += (ipt[i][t] != ipt[j][t]);
init(k);
REP(i, k) REP(j, k)
{
if (i == j)
continue;
AddEdge(i, j, min(diff[i][j] * w, n * m));
}
solve();
}
return 0;
}
Zepto Code Rush 2014——Dungeons and Candies的更多相关文章
- Codeforces Zepto Code Rush 2014 -C - Dungeons and Candies
这题给的一个教训:Codeforces没有超时这个概念.本来以为1000*(1000+1)/2*10*10要超时的.结果我想多了. 这题由于k层都可能有关系,所以建一个图,每两个点之间连边,边权为n* ...
- CF Zepto Code Rush 2014 B. Om Nom and Spiders
Om Nom and Spiders time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- Zepto Code Rush 2014 A. Feed with Candy
此题用贪心求解, 首先将caramel drop类别的糖果按照高度从小到大排序,如果高度相同,按照重量从小到大排序 将fruit drop类别的糖果按照高度从小到大排序,如果高度相同,按照重量从小到大 ...
- Zepto Code Rush 2014 B - Om Nom and Spiders
注意题目给的是一个nxm的park,设元素为aij,元素aij 有4种可能U(上移),D(下移),L(左移),R(右移) 假设第i行第j列元素aij(注意元素的索引是从0开始的) 当aij为D时,此时 ...
- Zepto Code Rush 2014-A. Feed with Candy(HACK)
A. Feed with Candy time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Dungeons and Candies
Zepto Code Rush 2014:http://codeforces.com/problemset/problem/436/C 题意:k个点,每个点都是一个n * m的char型矩阵.对与每个 ...
- Code Rush插件
code rush 是微软推出的一款VS2008上的插件.他有强大的文件和代码导航功能,易于访问的重构和代码创建功能.一组编辑器.选择.剪贴板工具等. 教程链接 http://www.devexpre ...
- ZeptoLab Code Rush 2015 C. Om Nom and Candies 暴力
C. Om Nom and Candies Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/526 ...
- ZeptoLab Code Rush 2015 C. Om Nom and Candies [ 数学 ]
传送门 C. Om Nom and Candies time limit per test 1 second memory limit per test 256 megabytes input sta ...
随机推荐
- 学校oj平台上不去
学校oj平台上不去,我的作业咋办啊
- HDU 3126 Nova [2009 Asia Wuhan Regional Contest Online]
标题效果 有着n巫妖.m精灵.k木.他们都有自己的位置坐标表示.冷却时间,树有覆盖范围. 假设某个巫妖攻击精灵的路线(他俩之间的连线)经过树的覆盖范围,表示精灵被树挡住巫妖攻击不到.求巫妖杀死所有精灵 ...
- OpenCV——Delaunay三角 [转载]
从这个博客转载 http://blog.csdn.net/raby_gyl/article/details/17409717 请其它同学转载时注明原始文章的出处! Delaunay三角剖分是1934年 ...
- 【Android基础】AndroidManifest常用权限permission整理
android.permission.ACCESS_COARSE_LOCATION 通过WiFi或移动基站的方式获取用户错略的经纬度信息,定位精度大概误差在30~1500米 android.permi ...
- War文件部署(转)
其实,开始要求将源码压缩成War文件时,一头雾水! 公司项目要求做CAS SSO单点登录 也就是这玩意.... 其实war文件就是Java中web应用程序的打包.借用一个老兄的话,“当你一个web应用 ...
- UVa10986_Sending email(最短)(白皮书图论的话题)
解决报告 思路: 裸裸的最短路. #include <iostream> #include <cstring> #include <cstdio> #include ...
- jquery php 百度搜索框智能提示效果
这个程序是利用php+ajax+jquery 实现的一个仿baidu智能提示的效果,有须要的朋友能够下载測试哦. 代码例如以下 index.html文件,保保存成index.htm <!DOCT ...
- MapXtreme DJ最短路径算法 全路径搜索算法
包括最短路径,全路径搜索算法演示程序请在http://pan.baidu.com/s/1jG9gKMM#dir/path=%2F%E4%BA%A7%E5%93%81%2FDemos 找 ShortWa ...
- 例如找出令人信服的权威C++中间malloc与new
例如找出令人信服的权威C++中间malloc与new 问题: 非常多人都知道malloc与new都是用来申请空间用的,开辟空间来源于堆中. 可是在C++中却非常少用malloc去申请空间,为什么? 以 ...
- WIN8 、WIN7 下IIS7.5、IIS8 的rewrite 伪静态功能设置方法
原文 WIN8 .WIN7 下IIS7.5.IIS8 的rewrite 伪静态功能设置方法 win7和win8系统都自带有iis的功能.关于IIS的安装,上一篇已经讲述,这里就不重复了. 下面说下在w ...