题意:给出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】的更多相关文章

  1. codeforces 400D Dima and Bacteria 并查集+floyd

    题目链接:http://codeforces.com/problemset/problem/400/D 题目大意: 给定n个集合,m步操作,k个种类的细菌, 第二行给出k个数表示连续的xi个数属于i集 ...

  2. Codeforces Round #376 (Div. 2) C. Socks---并查集+贪心

    题目链接:http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,每只都有一个颜色,现在他的妈妈要去出差m天,然后让他每天穿第 L 和第 R 只 ...

  3. Codeforces 766D. Mahmoud and a Dictionary 并查集 二元敌对关系 点拆分

    D. Mahmoud and a Dictionary time limit per test:4 seconds memory limit per test:256 megabytes input: ...

  4. Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序

    https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[] ...

  5. CodeForces Roads not only in Berland(并查集)

    H - Roads not only in Berland Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d ...

  6. codeforces div2 603 D. Secret Passwords(并查集)

    题目链接:https://codeforces.com/contest/1263/problem/D 题意:有n个小写字符串代表n个密码,加入存在两个密码有共同的字母,那么说这两个密码可以认为是同一个 ...

  7. CodeForces 698B Fix a Tree (并查集应用)

    当时也是想到了并查集,但是有几个地方没有想清楚,所以就不知道怎么写了,比如说如何确定最优的问题.赛后看了一下别人的思路,才知道自己确实经验不足,思维也没跟上. 其实没有那么复杂,这个题目我们的操作只有 ...

  8. Codeforces 977E:Cyclic Components(并查集)

    题意 给出nnn个顶点和mmm条边,求这个图中环的个数 思路 利用并查集的性质,环上的顶点都在同一个集合中 在输入的时候记录下来每个顶点的度数,查找两个点相连,且度数均为222的点,如果这两个点的父节 ...

  9. codeforces #541 D. Gourmet choice(拓扑+并查集)

    Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the wo ...

随机推荐

  1. Educational Codeforces Round 6 B. Grandfather Dovlet’s calculator 暴力

    B. Grandfather Dovlet’s calculator   Once Max found an electronic calculator from his grandfather Do ...

  2. apiCloud中openFrameGroup传参

    apiCloud中openFrameGroup传参 1.无效的 api.openFrameGroup({ // 打开 frame 组 name: 'group', scrollEnabled: fal ...

  3. Win10使用VMware虚拟机安装ubuntu

    Win10专业版自带有虚拟机安装工具Hyper-V,也可以使用其他如VMware工具安装,也挺方便. 所需工具: 1.  VMware-workstation  下载链接: http://rj.bai ...

  4. Linux下iscsi的使用

    查看是否已安装了iscsi-initiator:  [root@test\ ~]# rpm -qa |grep iscsi iscsi-initiator-utils-6.2.0.868-0.18.e ...

  5. ios各个屏幕

    func currentDeviceScreenMeasurement() -> CGFloat { var deviceScree: CGFloat = 3.5 if ((568 == SCR ...

  6. (转载)Android项目实战(二十八):使用Zxing实现二维码及优化实例

    Android项目实战(二十八):使用Zxing实现二维码及优化实例 作者:听着music睡 字体:[增加 减小] 类型:转载 时间:2016-11-21我要评论 这篇文章主要介绍了Android项目 ...

  7. windows下安装mycat,并简单使用

    使用mycat需要先安装jdk1.7以上 参考:http://www.cnblogs.com/llhhll/p/9257764.html 1从官网下载解压后目录如下(1.6版本) 下载地址:https ...

  8. 利用canvas做一个简单个gif停止和播放

    var ImagePlayer = function(options) { this.control = options.control; this.image = options.image; th ...

  9. 适配器模式(Adapter):类适配器、对象适配器

    适配器模式(Adapter):将一个类的接口转换成客户希望的另外一个接口.A d a p t e r 模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 适用场景: 1.已经存在的类的接口 ...

  10. PostgreSQL 事务管理的MVCC

    PostgreSQL的并发控制机制同时实现了多版本控制MVCC协议和两阶段封锁协议.实际采用哪种协议取决于所执行的语句类型. DML语句的并发控制将使用MVCC协议: DDL语句的并发控制基于标准的两 ...