题意:给出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. Android 多分辨率自适应总结

    这周的工作对Android项目多分辨率自适应进行调整.故对这方面知识进行不断的尝试学习.Android项目刚開始做的时候一定养成编程习惯,全部资源调用放在value中.统一命名以及管理.总结了下面内容 ...

  2. NOIP2017提高组模拟赛 9 (总结)

    NOIP2017提高组模拟赛 9 (总结) 第一题 星星 天空中有N(1≤N≤400)颗星,每颗星有一个唯一的坐标(x,y),(1≤x,y ≤N).请计算可以覆盖至少K(1≤K≤N)颗星的矩形的最小面 ...

  3. web security

    brute force cracking   暴力破解 Brute force (also known as brute force cracking) is a trial and error me ...

  4. sql server执行动态拼接sql(带传参数)和去掉小数点后0的函数

    1 exec sp_executesql N'SELECT 2 [Extent2].[Id] AS [Id], 3 [Extent2].[Name] AS [Name], 4 [Extent2].[D ...

  5. ES线程池设置

    每个Elasticsearch节点内部都维护着多个线程池,如index.search.get.bulk等,用户可以修改线程池的类型和大小,线程池默认大小跟CPU逻辑一致 一.查看当前线程组状态 cur ...

  6. (转载)RecyclerView之ItemDecoration由浅入深

    RecyclerView之ItemDecoration由浅入深 作者 小武站台 关注 2016.09.19 18:20 字数 1155 阅读 10480评论 15喜欢 91赞赏 3 译文的GitHub ...

  7. 51nod 1101 换零钱 完全背包的变型 动态规划

    题目: 思路: ;i < ; i++){ for(int j = a[i];j <= n; j++){ dp[j] = (dp[j] + dp[j-a[i]])%mod; } } a[i] ...

  8. SpringCloud学习笔记(4)----Spring Cloud Netflix之Eureka的配置

    1. Eureka监控配置 Eureka的客户端需要加入依赖 <dependency> <groupId>org.springframework.boot</groupI ...

  9. HDU-5693 D Game 动态规划 两次动规

    题目链接:https://cn.vjudge.net/problem/HDU-5693 题意 中文题 这个游戏是这样的,首先度度熊拥有一个公差集合{D},然后它依次写下N个数字排成一行.游戏规则很简单 ...

  10. Java取得环境变量和系统属性

    取得所有的环境变量 public class GetEnvAndProp { public static void main(String[] args) { Map<String, Strin ...