Penguin-Avia

Time limit: 1.0 second
Memory limit: 64 MB
The Penguin-Avia airline, along with other Antarctic airlines, experiences financial difficulties because of the world's economic crisis. People of Antarctica economize on flights and use trains or prefer to stay at home. The airline's management hopes that the number of their clients will increase in the summer due to the tourists visiting the coastal resorts. In order to hold out till the summer, it was decided to optimize the flight scheme by cancelling some flights and introducing some new flights.
Director of Penguin-Avia assumes that after the optimization the flight scheme must have the following properties:
  1. Using one or more Penguin-Avia flights, one can get from any Antarctic airport to any other.
  2. The scheme must contain the minimal number of flights among all the schemes satisfying the first property.

However, not everything is that easy in Antarctica. For cancelling a flight, the airline must pay a one-time forfeit of d Antarctic dollars. To obtain slots for a new flight, the company must spend a Antarctic dollars to grease the palm of the godfather of the Antarctic mafia nicknamed Walrus.

Help Director of Penguin-Avia transform the existing flight scheme spending as little money as possible. For doing that, you will be presented with a travel card for all flights of the airline.

Input

In the first line you are given the number n of airports in Antarctica, 2 ≤ n ≤ 100. In the second line you are given the integers d and a, 1 ≤ d, a ≤ 106. The following n lines describe the existing scheme of Penguin-Avia flights in the form of an n × n matrix. There is “1” in a cell (i, j) of the matrix if the airline has flights between the airports i and j. Otherwise, there is “0” in the cell. It is guaranteed that the matrix is symmetric and there are only zeros on its diagonal.

Output

In the first line output the minimal amount of money necessary for the optimization of the existing flight scheme. In the next n lines give the plan of changing the scheme in the form of an n × n matrix. A cell (i, j) of this matrix contains the symbol “d” if the flights between the airports i and j should be cancelled. In the case when a new flight should be introduced between these airports, the cell contains the symbol “a”. The remaining cells contain the symbol “0”. The matrix must be symmetric. If there are several optimal schemes, output any one of them.

Sample

input output
6
2 3
011000
101000
110000
000011
000101
000110
7
0d0000
d00000
000a00
00a0d0
000d00
000000
Problem Author: Alexander Ipatov
【分析】给出一个图,双向的,现要你删除,添加一些边,使得所有点联通,删除,添加都需要费用,问你最小费用是多少。就是个简  单的并查集问题。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 0x3f3f3f3f
#define mod 10000
typedef long long ll;
using namespace std;
const int N=;
const int M=;
int power(int a,int b,int c){int ans=;while(b){if(b%==){ans=(ans*a)%c;b--;}b/=;a=a*a%c;}return ans;}
char w[N][N];
int vis[N][N],VIS[N][N];
int n,m,k,c;
ll s=;
int parent[N];
int Find(int x)
{
if(parent[x]!=x)parent[x]=Find(parent[x]);
return parent[x];
}
void Union(int x,int y)
{
x=Find(x);y=Find(y);
if(x==y)return;
parent[y]=x;
}
int main()
{
for(int i=;i<=;i++)parent[i]=i;
int a,d;
scanf("%d%d%d",&n,&a,&d);
for(int i=;i<=n;i++){
scanf("%s",w[i]+);
}
for(int i=;i<=n;i++){
for(int j=i+;j<=n;j++){
if(w[i][j]==''){
int x=Find(i);
int y=Find(j);
if(x==y)vis[i][j]=vis[j][i]=,s+=a;
else Union(i,j);
}
}
}
int ok=Find();
for(int i=;i<=n;i++){
int x=Find(i);
if(x!=ok&&vis[][i]!=)vis[][i]=vis[i][]=,s+=d,Union(,i);
}
printf("%lld\n",s);
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
int x=Find(i);
int y=Find(j);
if(!vis[i][j])printf("");
else if(vis[i][j]==)printf("d");
else if(vis[i][j]==)printf("a");
}
printf("\n");
}
return ;
}

URAL(timus)1709 Penguin-Avia(并查集)的更多相关文章

  1. 1709. Penguin-Avia(并查集)

    1709 简单题 并查集找下就行 #include <iostream> #include<cstdio> #include<cstring> #include&l ...

  2. URAL 1682 Crazy Professor (并查集)

    [题目链接] http://acm.timus.ru/problem.aspx?space=1&num=1682 [题目大意] 给出k,从1开始不断地加一并把这个数写在黑板上,如果写上的数字和 ...

  3. URAL 1320 Graph Decomposition(并查集)

    1320. Graph Decomposition Time limit: 0.5 secondMemory limit: 64 MB There is a simple graph with an ...

  4. URAL 1671 Anansi's Cobweb (并查集)

    题意:给一个无向图.每次查询破坏一条边,每次输出查询后连通图的个数. 思路:并查集.逆向思维,删边变成加边. #include<cstdio> #include<cstring> ...

  5. URAL - 1003:Parity (带权并查集&2-sat)

    Now and then you play the following game with your friend. Your friend writes down a sequence consis ...

  6. 51nod 1204 Parity(并查集应用)

    1204 Parity 题目来源: Ural 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题   你的朋友写下一串包含1和0的串让你猜,你可以从中选择一个连续的子串 ...

  7. BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]

    4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...

  8. 关押罪犯 and 食物链(并查集)

    题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...

  9. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

随机推荐

  1. JSON.parse()和eval()区别

    JSON.parse()只会将标准的Json字符串(key和value都由双引号引起来,最外面用单引号括住)转为JSON对象. eval()在转换字符串的时候是比较松的,即使不是标准的Json字符串也 ...

  2. bjui给出的一个标准应用的首页

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="utf-8&quo ...

  3. swift语言之多线程操作和操作队列(上)———坚持51天吃掉大象

    欢迎有兴趣的朋友,参与我的美女同事发起的活动<51天吃掉大象>,该美女真的很疯狂,希望和大家一起坚持51天做一件事情,我加入这个队伍,希望坚持51天每天写一篇技术文章.关注她的微信公众号: ...

  4. ButterKnife的配置

    1.打开settings选择Plugins 安装 安装完成后会提示重启AS. 2.在build.gradle文件中添加: compile 'com.jakewharton:butterknife:7. ...

  5. 2013年7月份第2周51Aspx源码发布详情

    FineOffice自动化办公(OA)源码  2013-7-12 [ VS2010 ]源码描述:此源码使用fineui开发,作为村居使用的系统,所以命名为fineoffice,其实你在此基础上扩成CR ...

  6. hdu1078 bfs

    //Accepted 468 KB 812 ms //bfs+dp #include <cstdio> #include <cstring> #include <iost ...

  7. poj1458

    //Accepted 4112 KB 16 ms //最长公共子串 #include <cstdio> #include <cstring> #include <iost ...

  8. (转)HTML 5离线存储之Web SQL

    原文:http://developer.51cto.com/art/201106/267357.htm HTML 5离线存储之Web SQL 2011-06-07 15:14 kkun kkun的博客 ...

  9. HDU 圆桌会议 - 数学题

    圆桌   题意就是每分钟可以将相邻的两个人的位置互换一下 , 问你 ,几分钟可以将所有人的位置互换成    原先的  B 在A的右边 C在A的左边 , 换成现在的 C 在A 的右边 , B 在 A 的 ...

  10. ERP仓库管理系统(九)

    需求分析: 1.设计库房表,至少包括两个字段,库房名称,库房所属公司的ID(在客户资质审批表中找到对应公司的ID) 2.设计增.删.改.查一套程序,其中的删除要做限制,只要有库存数据存在则不允许删除对 ...