Ural1709-Penguin-Avia
来源:互联网 发布:餐饮收银软件排行 编辑:程序博客网 时间:2024/05/29 13:14
用并查集先筛选掉以及连通的边,然后再将未连通边加上即可。
#include <cstdio>#include <cstring>#include <vector>using namespace std;const int maxn = 100 + 5;char mat[maxn][maxn];char ans[maxn][maxn];struct edge { int u, v;};vector<edge> e;int par[maxn];int rk[maxn];void init(int n) { for (int i = 0; i < n; i++) { par[i] = i; rk[i] = 0; }}int find(int x) { if (par[x] == x) { return x; } else { return par[x] = find(par[x]); }}void unite(int x, int y) { x = find(x); y = find(y); if (x == y) { return; } if (rk[x] < rk[y]) { par[x] = y; } else { par[y] = x; if (rk[x] == rk[y]) { rk[x]++; } }}bool same(int u, int v) { return find(u) == find(v);}int main(int argc, char const *argv[]) { int n, d, a; scanf("%d%d%d", &n, &d, &a); for (int i = 0; i < n; i++) { getchar(); for (int j = 0; j < n; j++) { scanf("%c", &mat[i][j]); } } for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { if (mat[i][j] == '1') { e.push_back((edge){i, j}); } } } init(n); long long cost = 0; memset(ans, '0', sizeof(ans)); for (int i = 0; i < e.size(); i++) { int u = e[i].u; int v = e[i].v; if (!same(u, v)) { unite(u, v); } else { ans[u][v] = 'd'; ans[v][u] = 'd'; cost += d; } } for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { if (!same(i, j)) { unite(i, j); ans[i][j] = 'a'; ans[j][i] = 'a'; cost += a; } } } printf("%lld\n", cost); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { putchar(ans[i][j]); } putchar('\n'); } return 0;}
0 0
- Ural1709-Penguin-Avia
- Ural 1709. Penguin-Avia
- ural Penguin-Avia
- URAL 1709 Penguin-Avia
- penguin(二)
- MFC下通过OpenCV打开AVIA文件,有暂停功能
- A. Polo the Penguin and Segments
- B. Polo the Penguin and Matrix
- A. Polo the Penguin and Strings
- CodeForces 288APolo the Penguin and Strings
- CodeForces 289APolo the Penguin and Segments
- CodeForces 289BPolo the Penguin and Matrix
- java继承案例(Pet、Dog、Penguin)
- cf 402bPolo the Penguin and Matrix
- Codeforces 835E:The penguin's game
- codeforces Polo the Penguin and Strings 思维问题代码链接
- codeforces 289 B. Polo the Penguin and Matrix
- CodeForces 288C - Polo the Penguin and XOR operation
- 深度学习UFLDL(一)
- S3C2440开发板LED驱动——ioremap 映射
- yum 常用命令和选项 及 安装的软件的位置
- bit、byte、位、字节、汉字、字符之间的区别
- 自带logo的二维码生成代码
- Ural1709-Penguin-Avia
- 修复RecyclerView嵌套滚动问题
- Android 将自己的应用改为系统应用
- Activiti基础教程--04管理流程定义(流程定义部署ZIP/classpath)、查看(获取流程定义图图片)、删除,不能修改
- 12.3 信息工程学院acm招新宣讲
- oracle pl/sql 程序块,游标,生成测试数据
- Hack the RPLiDAR A1 Laser Scanner
- 关于添加rownum条件count stopkey执行计划对于cost成本估算的影响
- ADB指令之将电脑文本复制到仿真手机中