codeforces 400 D Dima and Bacteria【并查集 Floyd】
题意:给出n个点,分别属于k个集合,判断每个集合里面的点的距离都为0,为0的话输出yes,并输出任意两个集合之间的最短路
这道题目有两个地方不会处理,
先是n个点,分别属于k个集合,该怎么记录下来这里,
然后就是判断每个集合里面的点的距离是否为1,这里可以用并查集来做,如果在输入点的时候,距离为0,就将这两点合并
最后判断每个点,如果他们同属于一个集合,判断它俩的根是否一样就可以了
最后用floyd求最短路
#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
using namespace std; #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i) typedef long long LL;
const int INF = (<<)-;
const int mod=;
const int maxn=; int in[maxn*maxn],d[maxn][maxn],p[maxn*maxn]; int find(int x){ return x==p[x]? x:p[x]=find(p[x]);} int main(){
int n,m,k;
scanf("%d %d %d",&n,&m,&k);
int sum=;
for(int i=;i<=k;i++){
int c;
cin>>c;
for(int j=sum+;j<=sum+c;j++) in[j]=i;
sum+=c;
} for(int i=;i<=k;i++){//��ʼ��floyed����
for(int j=;j<=k;j++){
if(i==j) d[i][j]=;
else d[i][j]=INF;
}
}
for(int i=;i<=n;i++) p[i]=i; while(m--){
int u,v,w;
cin>>u>>v>>w;
if(in[u]!=in[v]&&d[in[u]][in[v]]>w) d[in[u]][in[v]]=d[in[v]][in[u]]=w; if(w==){
int x=find(u);
int y=find(v);
if(x!=y) p[x]=y;
}
} for(int i=;i<=n;i++){
if(in[i]==in[i-]){
int x=find(i);
int y=find(i-);
if(x!=y){ //�������ͬһ�����ϵ��Ǹ���ͬ��˵������֮��ķ��ò���0�����NO
printf("No\n");
return ;
}
}
}
printf("Yes\n"); for(int p=;p<=k;p++)
for(int i=;i<=k;i++)
for(int j=;j<=k;j++)
d[i][j]=min(d[i][j],d[i][p]+d[p][j]); for(int i=;i<=k;i++){
for(int j=;j<=k;j++){
if(d[i][j]==INF) printf("-1 ");
else printf("%d ",d[i][j]);
}
printf("\n");
} return ;
}
codeforces 400 D Dima and Bacteria【并查集 Floyd】的更多相关文章
- codeforces 400D Dima and Bacteria 并查集+floyd
题目链接:http://codeforces.com/problemset/problem/400/D 题目大意: 给定n个集合,m步操作,k个种类的细菌, 第二行给出k个数表示连续的xi个数属于i集 ...
- Codeforces Round #376 (Div. 2) C. Socks---并查集+贪心
题目链接:http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,每只都有一个颜色,现在他的妈妈要去出差m天,然后让他每天穿第 L 和第 R 只 ...
- Codeforces 766D. Mahmoud and a Dictionary 并查集 二元敌对关系 点拆分
D. Mahmoud and a Dictionary time limit per test:4 seconds memory limit per test:256 megabytes input: ...
- Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序
https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[] ...
- CodeForces Roads not only in Berland(并查集)
H - Roads not only in Berland Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
- codeforces div2 603 D. Secret Passwords(并查集)
题目链接:https://codeforces.com/contest/1263/problem/D 题意:有n个小写字符串代表n个密码,加入存在两个密码有共同的字母,那么说这两个密码可以认为是同一个 ...
- CodeForces 698B Fix a Tree (并查集应用)
当时也是想到了并查集,但是有几个地方没有想清楚,所以就不知道怎么写了,比如说如何确定最优的问题.赛后看了一下别人的思路,才知道自己确实经验不足,思维也没跟上. 其实没有那么复杂,这个题目我们的操作只有 ...
- Codeforces 977E:Cyclic Components(并查集)
题意 给出nnn个顶点和mmm条边,求这个图中环的个数 思路 利用并查集的性质,环上的顶点都在同一个集合中 在输入的时候记录下来每个顶点的度数,查找两个点相连,且度数均为222的点,如果这两个点的父节 ...
- codeforces #541 D. Gourmet choice(拓扑+并查集)
Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the wo ...
随机推荐
- Hadoop实战:使用Combiner提高Map/Reduce程序效率
好不easy算法搞定了.小数据測试也得到了非常好的结果,但是扔到进群上.挂上大数据就挂了.无休止的reduce不会结束了. .. .. .... .. ... .. ================= ...
- ThinkPHP5.0框架开发--第5章 TP5.0 控制器
ThinkPHP5.0框架开发--第5章 TP5.0 控制器 第5章 TP5.0 控制器 ============================================== 上次复习 1.路 ...
- Oracle 10g RAC (linux) ASM 共享存储的管理详解
---------ASM 的管理(共享磁盘的管理)1.以 instance 的方式管理 ASM,启动 database 之前必须先启动 ASM instance,ASM instance 启动后,挂载 ...
- angular4(2-2)angular脚手架引入第三方类库(swiper)
试了好多方法,npm install 方法失败了,下载到本地是可以使用的: 将swiper文件放到assets文件下: 项目目录下:(命令行) 因为ts并不能准确识别js语法,所以需要用ts中的int ...
- Redis-3-string类型
Redis-3-string类型 标签(空格分隔): redis set key value [ex 秒数] / [px 毫秒数] [nx] /[xx] mset key value key valu ...
- BZOJ 4636 (动态开节点)线段树
思路: 偷懒 懒得离散化 搞了个动态开节点的线段树 (其实是一样的--..) 注意会有a=b的情况 要判掉 //By SiriusRen #include <cstdio> #includ ...
- 模仿百度首页“元宵节汤圆”动图,并实现360°不停旋转(CSS3的animation动画效果)
模仿百度首页“元宵节汤圆”动图,并实现360°不停旋转(CSS3的animation动画效果) 效果图: 切图地址: https://ss1.bdstatic.com/5eN1bjq8AAUYm2zg ...
- 51nod 1179 最大的最大公约数 (打表计数法)
题目: 考虑清楚就简单了,我们把每个数的因子计数. 两个数的公约数就是计数超过2的数,然后找到最大的那个就好了. 计算每个数的素因子,记得sqrt(),不然会超时. 打表计数法时间复杂度O(n*sqr ...
- Linux 清空缓存
sync echo 1 > /proc/sys/vm/drop_caches echo 2 > /proc/sys/vm/drop_caches echo 3 > /proc/sys ...
- hiho 1617 - 方格取数 - dp
题目链接 描述 给定一个NxN的方格矩阵,每个格子中都有一个整数Aij.小Hi和小Ho各自选择一条从左上角格子到右下角格子的路径,要求路径中每一步只能向右或向下移动,并且两条路径不能相交(除了左上右下 ...