题目大意:在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. android textview 跑马灯

    <TextView android:layout_width="match_parent" android:layout_height="48dp" an ...

  2. ie浏览器兼容性快速处理小招

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. 关于 IOS 发布的点点滴滴记录(一)

    今天又是发布 APP 审核的时候,哎,说来也悲催. 我们产品连这次好像是第四次被苹果公司拒绝了,想想都有点伤感.其实对于里面的内容我到是不是很关心.我关心的是在这过程中我所碰到的奇怪的事情.  (这次 ...

  4. Android之View.onMeasure方法

    View在屏幕上显示出来要先经过measure(计算)和layout(布局). 1.什么时候调用onMeasure方法? 当控件的父元素正要放置该控件时调用.父元素会问子控件一个问题,“你想要用多大地 ...

  5. Navicat提示Access violation at address 004E9844 in module ‘navicat.exe’

    今天在联系MySQL 数据库表的练习时,出现了一下问题: 内存越界问题,最好重新注册下Windows的动态链接库 首先“开始”—“运行”—“cmd” 在打开的dos窗口中运行“for %1 in (% ...

  6. hdu 2052

    PS:竟然一次AC....惊喜...注意每个实例后跟一个空行.. 学到一个快速清空数组的用法...memst函数.  memst(void *s,char a,sizeof n)  把 s里面的前n个 ...

  7. 解决:未找到setenv命令

    在Ubuntu12.04中配置python环境变量:setenv PATH "$PATH:/usr/local/bin/python",提示未找到setenv命令. 为什么呢?这是 ...

  8. R函数是对A方法的封装

    $user = new UserController;  ===      $user=A("User"); $user = new UserController; $user-& ...

  9. Python 的property的实现 .

    描述符.就是 将某种特殊类型①的类的实例指派给另一个类的属性 ①只要实现一下三种方法的其中一个就是特殊类型. __get__(self,instance,owner) -用于访问属性,他返回属性的值. ...

  10. Android Manifest.xml详解

    一.关于AndroidManifest.xml AndroidManifest.xml 是每个android程序中必须的文件.它位于整个项目的根目录,描述了package中暴露的组件(activiti ...