题目大意:在nxm的方格中,每一个1x1的小方格中都有一堵沿对角线的墙,并且每堵墙都有一个坚固程度,这些墙将nxm的方格分割成了若干个区域。现在要拆除一些墙,使其变成一个区域。

题目分析:将区域视作点,将墙视作边,这样问题就变成了求最小生成树。

代码如下:

# include<iostream>
# include<cstdio>
# include<cmath>
# include<vector>
# include<list>
# include<queue>
# include<cstring>
# include<algorithm>
using namespace std;
# define LL long long const int INF=1000000000;
const int N=1000;
const double eps=1e-10;
const double inf=1e20; int n,m;
char p[105][105];
int a[105][105];
int vis[105][105][2]; struct Edge
{
int fr,to,w;
bool operator < (const Edge &a) const{
return w<a.w;
}
};
Edge e[20000];
int fa[20000]; bool ok(int x,int y)
{
return x>=0&&x<n&&y>=0&&y<m;
} void dfs(int x,int y,int f,int cnt)
{
vis[x][y][f]=cnt;
if(p[x][y]=='\\'){
if(f==1){
if(ok(x,y-1)){
if(p[x][y-1]=='\\'&&vis[x][y-1][0]==-1) dfs(x,y-1,0,cnt);
if(p[x][y-1]=='/'&&vis[x][y-1][1]==-1) dfs(x,y-1,1,cnt);
}
if(ok(x+1,y)&&vis[x+1][y][0]==-1)
dfs(x+1,y,0,cnt);
}else{
if(ok(x-1,y)&&vis[x-1][y][1]==-1)
dfs(x-1,y,1,cnt);
if(ok(x,y+1)){
if(p[x][y+1]=='\\'&&vis[x][y+1][1]==-1) dfs(x,y+1,1,cnt);
if(p[x][y+1]=='/'&&vis[x][y+1][0]==-1) dfs(x,y+1,0,cnt);
}
}
}else{
if(f==1){
if(ok(x,y+1)){
if(p[x][y+1]=='\\'&&vis[x][y+1][1]==-1) dfs(x,y+1,1,cnt);
if(p[x][y+1]=='/'&&vis[x][y+1][0]==-1) dfs(x,y+1,0,cnt);
}
if(ok(x+1,y)&&vis[x+1][y][0]==-1)
dfs(x+1,y,0,cnt);
}else{
if(ok(x-1,y)&&vis[x-1][y][1]==-1)
dfs(x-1,y,1,cnt);
if(ok(x,y-1)){
if(p[x][y-1]=='\\'&&vis[x][y-1][0]==-1) dfs(x,y-1,0,cnt);
if(p[x][y-1]=='/'&&vis[x][y-1][1]==-1) dfs(x,y-1,1,cnt);
}
}
}
} int find_fa(int x)
{
int u=x;
while(fa[u]!=u)
u=fa[u];
while(fa[x]!=u){
int t=x;
x=fa[x];
fa[t]=u;
}
return u;
} int main()
{
int T;
scanf("%d",&T);
int cas=0;
while(T--)
{
scanf("%d%d",&n,&m);
memset(vis,-1,sizeof(vis));
for(int i=0;i<n;++i)
scanf("%s",p[i]);
for(int i=0;i<n;++i)
for(int j=0;j<m;++j)
scanf("%d",&a[i][j]);
int cnt=0;
for(int i=0;i<n;++i){
for(int j=0;j<m;++j){
if(vis[i][j][0]==-1){
dfs(i,j,0,cnt);
++cnt;
}
if(vis[i][j][1]==-1){
dfs(i,j,1,cnt);
++cnt;
}
}
}
int k=0;
for(int i=0;i<n;++i){
for(int j=0;j<m;++j){
if(vis[i][j][0]==vis[i][j][1]) continue;
e[k].fr=vis[i][j][0];
e[k].to=vis[i][j][1];
e[k].w=a[i][j];
++k;
}
}
sort(e,e+k);
for(int i=0;i<cnt;++i)
fa[i]=i;
int ans=0;
for(int i=0;i<k;++i){
int f1=find_fa(e[i].fr);
int f2=find_fa(e[i].to);
if(f1!=f2){
ans+=e[i].w;
fa[f1]=f2;
}
}
printf("Case %d: %d\n",++cas,ans);
}
return 0;
}

  

UVALive-7303 Aquarium (最小生成树)的更多相关文章

  1. 训练指南 UVALive - 5713(最小生成树 + 次小生成树)

    layout: post title: 训练指南 UVALive - 5713(最小生成树 + 次小生成树) author: "luowentaoaa" catalog: true ...

  2. UVALive - 2515 (最小生成树 kruskal)

    You are assigned to design network connections between certain points in a wide area. You are given ...

  3. 最小生成树求最大比率 UVALive - 5713

    题目链接:https://vjudge.net/problem/UVALive-5713 题意:给出t组数据,每组数据第一行给出一个n,表示点的数量,接下来n行,每行有三个数字,分别是点的坐标x,y和 ...

  4. 最小生成树 prime算法 UVALive - 6437

    题目链接:https://vjudge.net/contest/241341#problem/D 这里有多个发电站,需要求出所有点都和发电站直接或间接相连的最小代价,那么就是求出最小生成树的问题了,有 ...

  5. UVALive - 5713 最小生成树

    题意: 秦始皇修路,已知n个城市的坐标以及该城市的人口数,修路的费用是两个城市之间的欧几里得距离,其中可以有一条路不用花费代价但是要求这条路连接的两个城市的人口之和A/B尽量大,其中B是修路的总费用. ...

  6. UvaLive 4872 Underground Cables (最小生成树)

    题意: 就是裸的最小生成树(MST), 完全图, 边长是实数. 分析: 算是复习一下MST把 方法一: prim 复杂度(n^2) #include <bits/stdc++.h> usi ...

  7. UVALive 4872 Underground Cables 最小生成树

    题目链接: 题目 Underground Cables Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %ll ...

  8. POJ 1502 MPI Maelstrom / UVA 432 MPI Maelstrom / SCU 1068 MPI Maelstrom / UVALive 5398 MPI Maelstrom /ZOJ 1291 MPI Maelstrom (最短路径)

    POJ 1502 MPI Maelstrom / UVA 432 MPI Maelstrom / SCU 1068 MPI Maelstrom / UVALive 5398 MPI Maelstrom ...

  9. 最小生成树的kruskal、prim算法

    kruskal算法和prim算法 都说 kruskal是加边法,prim是加点法 这篇解释也不错:这篇 1.kruskal算法 因为是加边法,所以这个方法比较合适稀疏图.要码这个需要先懂并查集.因为我 ...

随机推荐

  1. Portlet和servlet的区别

    相同之处 l 都是java技术开发的web组件 l 都是由特定的容器在管理 l 都可以动态产生各种内容 l 生命周期都是由容器管理 l 和客户端的交互通过request/response机制 不同之处 ...

  2. Android 动画特效

    一.渐变动画 AlphaAnimation aa = new AlphaAnimation(0.3f, 1.0f); // fromAlpha , toAlpha aa.setDuration(200 ...

  3. checkbox的全选、反选、删除(MainActivity)

    package com.example.ay; import java.util.ArrayList;import java.util.List; import com.example.adapter ...

  4. 避免ajax中get方法产生缓存的解决办法

    在参数中传一个随机数,就会避免浏览器对get方法异步修改数据缓存,导致不能及时看到最新效果 $.get("<?php echo U('Vip/VipHandouts/change_gr ...

  5. jquery的is用法

    JQuery 中 is(':visible') 解析及用法 javascript代码$(document).ready(function() {           $('#faq').find('d ...

  6. javascript 变量声明有var与无var 的区别

    1.在函数作用域内 加var定义的变量是局部变量,不加var定义的就成了全局变量.使用var定义var a = 'hello World';function bb(){var a = 'hello B ...

  7. WPF 基础到企业应用系列索引

    转自:http://www.cnblogs.com/zenghongliang/archive/2010/07/09/1774141.html WPF 基础到企业应用系列索引 WPF 基础到企业应用系 ...

  8. VM虚拟机中Ubuntu无法连接网络

    之前Ubuntu是可以上网的,但是今天打开后发现上不了网了,经过一番折腾,弄好了,记录下. 方案一:将网络连接设置为自定义NAT VM ->设置-> 硬件->网络适配器 这么已修改就 ...

  9. CodeForces 56E-Domino Principle

    E - Domino Principle Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I6 ...

  10. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...