并查集 + Floyd
来源:互联网 发布:老张博客源码 编辑:程序博客网 时间:2024/05/21 06:21
题目:Codeforces Round #234 D. Dima and Bacteria
题目链接:http://codeforces.com/contest/400/problem/D
题目意思:有n个病菌分为k个种类,n个病菌编号为1-n,k个种类编号为1-K。把能量从一个细菌运送到另一个细菌需要消耗代价x。问题有两问:1、同一种类任意两个细菌运送能量消耗是否为零。2、不同种类之间最小消耗。
解题思路:英语是硬伤啊!一开始没读懂,看了好久也没明白要干什么。看了题解,发现是并查集 + Floyd,然后就一目了然了……哎~~太弱啊……
程序代码:比大神的长了好多……慢了好多……不多自己看着清晰……
//发现了一个大神并查集封装成类的代码 感觉挺好的也封起来class DJset {int *f;public : DJset(int n){f = new int[n];for(int i = 0; i <= n; i ++) f[i] = i;}void insert(int i, int j){int a = getFather(i);int b = getFather(j);a > b ? f[a] = b : f[b] = a;}int getFather(int i){return f[i] == i ? i : getFather(f[i]);}}; DJset *ds;void build(){int N = 0;repf(i, 1, k) {int num = a[i];if(a[i] == 1) dis[i][i] = 0;while( num --){chg[++N] = i;}}}//图论好弱啊…… void floyd(){int a, b, c;repf(a, 1, k)repf(b, 1, k)repf(c, 1, k)dis[b][c] = min( dis[b][c], dis[b][a] + dis[a][c]);repf(a, 1, k) {repf(b, 1, k){if(b != 1) cout << ' ';if(dis[a][b] < 100000) cout << dis[a][b];else cout << -1 ;}cout << endl;}}int solve(){int u, v, x, N = 1;int ans = 1;repf(i, 1, m) {cin >> u >> v >> x;if(x == 0 ) ds->insert(u, v);dis[chg[u]][chg[v]] = dis[chg[v]][chg[u]] = min(dis[chg[v]][chg[u]], x);}repf(i, 1, k){int w = a[i];int temp = ds->getFather(N);while( w --){//cout << temp << ' ' << ds->getFather(N) << endl ;if(temp != ds->getFather(N++)) ans = 0;}}if(ans) cout << "Yes" << endl, floyd();else cout << "No" << endl;return 0;}int main(){clr(dis, 63);ios::sync_with_stdio(false);cin >> n >> m >> k;ds = new DJset(n);repf(i, 1, k) cin >> a[i];build();//repf(i, 1, n) cout << chg[i] << ' ' ; cout << endl;solve();//system("pause");return 0;}
0 0
- 并查集 + Floyd
- hdu1233(floyd+并查集)
- 并查集或图floyd-2
- codeforces#234_div2_D Dima and Bacteria floyd+并查集
- Pilot Work Experience (URAL 1888 并查集+floyd)
- USACO Section 2.4 Cow Tours (Floyd+并查集)
- New Year Permutation(Floyd+并查集)
- poj 1797 并查集 / floyd(超时)
- 51nod 1366 贫富差距【并查集+Floyd】
- pat L2-010. 排座位 floyd 或 并查集
- floyd闭包或者加一点并查集uva247
- CF400D Dima and Bacteria(并查集+Floyd)
- hdoj 3081 Marriage Match II 【二分查找+ 最大流 + 并查集 or floyd】【二分图求最大匹配 + 并查集 or floyd】
- Codeforces 400D Dima and Bacteria(Floyd+并查集)
- 武大预选赛F题-(裸并查集+下标离散化+floyd最短路)
- WOJ校赛 Problem 1542 - F - Countries 并查集+离散化+Floyd
- Good Bye 2014 B. New Year Permutation(并查集 ||Floyd )
- Floyd,传递闭包,并查集(电话圈,uva 247)
- 项目笔记--图像处理
- 设计模式小记
- C# 编码转换 UTF8转GB2312 GB2312转UTF8
- 在google官网中下载的Android SDK的配置问题
- 物联网学习笔记——京东云擎初体验 部署REST服务
- 并查集 + Floyd
- C++点滴(4)
- 好受伤啊
- A20红外遥控器与Android功能的适配
- 周二作业第1题输出我的班级、姓名和学号程序
- 批处理部分命令解析
- C++点滴(5)
- jQuery中的.bind()、.live()和.delegate()之间区别分析
- 《算法艺术与程序设计竞赛》 第四章习题-D题