URAL(timus)1709 Penguin-Avia(并查集)
Penguin-Avia
Memory limit: 64 MB
- Using one or more Penguin-Avia flights, one can get from any Antarctic airport to any other.
- 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.
Input
Output
Sample
input | output |
---|---|
6 |
7 |
#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(并查集)的更多相关文章
- 1709. Penguin-Avia(并查集)
1709 简单题 并查集找下就行 #include <iostream> #include<cstdio> #include<cstring> #include&l ...
- URAL 1682 Crazy Professor (并查集)
[题目链接] http://acm.timus.ru/problem.aspx?space=1&num=1682 [题目大意] 给出k,从1开始不断地加一并把这个数写在黑板上,如果写上的数字和 ...
- URAL 1320 Graph Decomposition(并查集)
1320. Graph Decomposition Time limit: 0.5 secondMemory limit: 64 MB There is a simple graph with an ...
- URAL 1671 Anansi's Cobweb (并查集)
题意:给一个无向图.每次查询破坏一条边,每次输出查询后连通图的个数. 思路:并查集.逆向思维,删边变成加边. #include<cstdio> #include<cstring> ...
- URAL - 1003:Parity (带权并查集&2-sat)
Now and then you play the following game with your friend. Your friend writes down a sequence consis ...
- 51nod 1204 Parity(并查集应用)
1204 Parity 题目来源: Ural 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 你的朋友写下一串包含1和0的串让你猜,你可以从中选择一个连续的子串 ...
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
- 关押罪犯 and 食物链(并查集)
题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...
随机推荐
- 火狐和IE的window.event对象详解(转载)
FF的FIREBUG,不仅能测试JS还能检查CSS错误,是一般常用的. 但它主要检查FF方面的错误,对IE就无能为力了. 要测试IE,就用ieTester,它可以测试IE几乎所有版本(1.0恐怕也用不 ...
- 傅里叶变换:MP3、JPEG和Siri背后的数学
九年前,当我还坐在学校的物理数学课的课堂里时,我的老师为我们讲授了一种新方法,给我留下了深刻映像.我认为,毫不夸张地说,这是对数学理论发现最广泛的应用.应用的领域包括:量子物理.射电天文学.MP3和J ...
- PowerShell工具脚本---按行数切割大文本文件
我编写的PowerShell工具脚本,[按行数切割大(文本)文件],生成n个小文件. 主要目的是为了能够让excel快速处理.或用脚本并发处理文本. 注意: 1 如果有必要,你可以先用其他工具,把大文 ...
- SQL Server 语句整理
1. 创建数据库 create database dbName 2. 删除数据库 drop database dbName 3. 备份sql server --- 创建 备份数据的 device US ...
- Python计算文件MD5值
import hashlib def fileMD5(filename): m = hashlib.md5() #md5计算 #m = hashlib.sha1() #sha1计算 #m = hash ...
- hdu 2051
ps:这道题是题目坑爹了...题目说不考虑n=0的...但其实要考虑...醉了 中文意思:输入一个10进制的数,输出他的二进制数 代码: #include "stdio.h"int ...
- Chapter 3: Connector(连接器)
一.概述 Tomcat或者称之为Catalina(开发名称),可以简化为两个主要的模块,如下图: 多个Connector关联一个Container.之所以需要多个Connector,是为了处理多种协议 ...
- BZOJ 1853 幸运数字
需要优化一波常数. 以及刚才那个版本是错的. #include<iostream> #include<cstdio> #include<cstring> #incl ...
- 解决MindManager缺少mfc100u.dll无法启动的难题-转载
很多应用软件的运行环境离不开组件,MindManager也不例外.很多用户在成功安装MindManager之后,却显示无法找到组件mfc100u.dll,mfc100u.dll是MindManager ...
- LCA(RMQ)
; xh=; ..lx*] of longint; lt,dfn,fr,dep:..lx] of longint; f:..lx*,..xh] of longint; vis:..lx] of boo ...