题意: 题目大意:给出n,m和k,表示有n个细菌,m种仪器和k种细菌,给出k种细菌的数量ci,然后每个细菌按照种类排成一排(所以有第i种细菌的序号从∑(1≤j≤i-1)cj + 1 到∑(1≤j≤i)cj);接下来给出m种仪器,有u,v,x三个值,表示说从可以在第u,v号细菌之间移动能量,代价为x。请帮助博士判断说这些细菌是否正确,正确的前提条件是说同种细菌之间移动能量为0,如果正确的话,给出两两细菌(种类)间移动能量的最小值。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
const double pi=acos(-1);
const int mod=100000000;
struct edge{
int to,c;
};
vector<edge> G[100600];
int c[100600],f[100600],be[100600],dist[600][600],n,m,k;
void add_edge(int u,int v,int c)
{
G[u].push_back((edge){v,c});
G[v].push_back((edge){u,c});
} int findr(int u)
{
if(f[u]!=u)
f[u]=findr(f[u]);
return f[u];
} bool legal()
{
for(int i=1;i<=k;i++)
{
int r=findr(c[i-1]+1);
for(int j=c[i-1]+2;j<=c[i];j++)
if(r!=findr(j)) return false;
}
return true;
} int main()
{
while(~scanf("%d %d %d",&n,&m,&k))
{
for(int i=1;i<=n;i++) {G[i].clear();f[i]=i;}
for(int i=1;i<=k;i++)
{
scanf("%d",&c[i]);
c[i]+=c[i-1];
for(int j=c[i-1]+1;j<=c[i];j++)
be[j]=i;
}
MM(dist,inf);
for(int i=1;i<=m;i++)
{
int u,v,c;
scanf("%d %d %d",&u,&v,&c);
add_edge(u,v,c);
if(!c)
{
int ru=findr(u);
int rv=findr(v);
if(ru!=rv) f[rv]=ru;
}
}
if(!legal()) {printf("No\n");continue;};
for(int i=1;i<=n;i++)
for(int j=0;j<G[i].size();j++)
{
int u=i,v=G[i][j].to;
if(dist[be[u]][be[v]]>G[i][j].c)
dist[be[v]][be[u]]=dist[be[u]][be[v]]=G[i][j].c;
}
for(int w=1;w<=k;w++)
for(int i=1;i<=k;i++)
for(int j=1;j<=k;j++)
dist[i][j]=min(dist[i][j],dist[i][w]+dist[w][j]);
printf("Yes\n");
for(int i=1;i<=k;i++)
for(int j=1;j<=k;j++)
{
if(i==j)
printf("0 ");
else if(dist[i][j]==inf)
printf("-1 ");
else
printf("%d ",dist[i][j]);
if(j==k)
printf("\n");
}
}
return 0;
}

  并查集+floyd

wa代码,好好查错:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
const double pi=acos(-1);
const int mod=100000000;
struct edge{
int to,c;
};
vector<edge> G[100005];
int c[100005],f[100005],n,m,k;
void add_edge(int u,int v,int c)
{
G[u].push_back((edge){v,c});
G[v].push_back((edge){u,c});
} int findr(int u)
{
if(f[u]!=u) return findr(f[u]);
} bool legal()
{
for(int i=1;i<=k;i++)
{
int r=findr(c[i-1]+1);
for(int j=c[i-1]+2;j<=c[i];j++)
if(r!=findr(j)) return false;
}
return true;
} int main()
{
while(~scanf("%d %d %d",&n,&m,&k))
{
for(int i=1;i<=n;i++) {G[i].clear();f[i]=i;}
for(int i=1;i<=k;i++) {scanf("%d",&c[i]);c[i]+=c[i-1];}
for(int i=1;i<=m;i++)
{
int u,v,c;
scanf("%d %d %d",&u,&v,&c);
add_edge(u,v,c);
if(!c)
{
int ru=findr(u);
int rv=findr(v);
if(ru!=rv) f[rv]=ru;
}
}
if(!legal()) {printf("No\n");continue;}; }
return 0;
}

  

TTTTTTTTTTT 400D Dima and Bacteria 细菌 最短路的更多相关文章

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

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

  2. Codeforces400D Dima and Bacteria

    题意:给你一个无向有权的图,图上的点被分成了几类,对于同类的点你需要判断它们之间相互的最短距离是不是0.满足这个条件之后要输出的是类与类之间的最短距离的矩阵.点给到10^5这么多,判断同类的点显然不能 ...

  3. codeforces Dima and Bacteria

    题意:给出n,m和k,表示有n个细菌,m种仪器和k种细菌,给出k种细菌的数量ci,然后每个细菌按照种类排成一排(所以有第i种细菌的序号从∑(1≤j≤i-1)cj + 1 到∑(1≤j≤i)cj):接下 ...

  4. codeforces 400 D Dima and Bacteria【并查集 Floyd】

    题意:给出n个点,分别属于k个集合,判断每个集合里面的点的距离都为0,为0的话输出yes,并输出任意两个集合之间的最短路 这道题目有两个地方不会处理, 先是n个点,分别属于k个集合,该怎么记录下来这里 ...

  5. SDKD 2017 Summer Single Training #03

    今天的题目有 6 个. 第一题: CodeForces - 400D  Dima and Bacteria 这个题实际是不难的,难的可能在题意的理解上还有题干有点长,这个题很考察题意上面,知识点很熟悉 ...

  6. CodeForces 400

    A - Inna and Choose Options Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d &a ...

  7. Codeforces Round #234 (Div. 2)

    A. Inna and Choose Options time limit per test 1 second memory limit per test 256 megabytes input st ...

  8. cf div2 234 D

    D. Dima and Bacteria time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  9. Codefroces 366 D Dima and Trap Graph (最短路)

    Dima and Trap Graph 题意:Dima和Inna越来越喜欢对方,但是Dima的室友缺很没有热情出去玩,然后Dima就把他室友Seryozha骗进了陷阱里.现在Seryozha想要从陷阱 ...

随机推荐

  1. 使用idea关联mysql时报错Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezon'

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/liuqiker/article/detai ...

  2. 2019.07.05 纪中_B

    今日膜拜:czj大佬orz%%% 2019.07.05[NOIP提高组]模拟 B 组 今天做题的时候大概能判断出题人的考点,可是就是没学过...特别痛苦 T0:栈的定义,模拟就好了T1:感觉像是找规律 ...

  3. PHP 时间转几分几秒

    public static function timetodate($c){ if($c < 86400){ $time = explode(' ',gmstrftime('%H %M %S', ...

  4. CF 403D Beautiful Pairs of Numbers

    The sequence of integer pairs (a1, b1), (a2, b2), ..., (ak, bk) is beautiful, if the following state ...

  5. RPC框架调用过程详解

    RPC框架调用过程详解 2017年09月16日 21:14:08 荷叶清泉 阅读数 6275   版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. ...

  6. 一、redis学习(基础)

    redis  持久化 rdb aof 

  7. leetcode 1267. Count Servers that Communicate

    You are given a map of a server center, represented as a m * n integer matrix grid, where 1 means th ...

  8. RSA 加密长度计算公式

    The length of data that can be encrypted using RSA is determined primarily by the size of the key yo ...

  9. 111、什么是stack (Swarm18)

    参考https://www.cnblogs.com/CloudMan6/p/8119150.html   什么是 stack ?    在将这个之前先回顾一下前面部署WordPress的过程:     ...

  10. 牛客挑战赛32E 树上逆序对

    nowcoder 口胡一时爽 先从这个逆序对的性质入手,手玩可以发现对于一对具有祖先关系节点的点,只有权值绝对值大的才能对这一对点是否为逆序对造成影响.具体来讲,如果祖先点权值大,并且取正号,那么其后 ...