题意: 题目大意:给出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. 分层最短路(牛客第四场)-- free

    题意: 给你边权,起点和终点,有k次机会把某条路变为0,问你最短路是多长. 思路: 分层最短路模板题.题目有点坑(卡掉了SPFA,只能用dijkstra跑的算法). #include<iostr ...

  2. 1.bash总体介绍

    1.总体介绍1.1 什么是Bash?Bash(Borune-Again SHell)是一个用于Linux操作系统的shell,即命令解释器Bash与sh兼容,并从ksh和csh引进了一些有用的功能,在 ...

  3. C# 连接 Oracle数据库增删改查,事务

    一. 前情提要 一般.NET环境连接Oracle数据库,是通过 TNS/SQL.NET 配置文件,而 TNS 必须要 Oracle 客户端(如果连接的是服务器的数据库,本地还要装一个 client , ...

  4. Linux6上安装MySQL

    MySQL安装包下载:https://www.mysql.com/downloads/ 然后选择: 把下载好的安装包传到服务器上的指定目录,然后解压: [root@master mysql]# tar ...

  5. 点击切换JS

    $(function(){ var tabnav = $("#tab-nav ul li"); tabnav.click(function(){ $(this).addClass( ...

  6. 运维学习篇之jenkins的安装(CentOS7)

    一. 介绍   Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能二. 作用  1.持续的软件版本 ...

  7. label smooth

    图像分类的一个trick,推导可参考这位博主https://leimao.github.io/blog/Label-Smoothing/ 知乎上的讨论https://www.zhihu.com/que ...

  8. ES数据架构与关系数据库Mysql

    ES数据架构的主要概念(与关系数据库Mysql对比) MySQL ElasticSearch Database Index Table Type Row Document Column Field S ...

  9. Java8使用lambda遍历List、Set、map

    public static void main(String[] args){ Map<String,String> map= new HashMap<>(); map.for ...

  10. 阿里域名 ssl tomcat

    1.首先注册一个域名 2.添加一个信息模板(域名服务里边) 3.域名解析(默认解析127.0.0.1)  可以ping 域名试下看是否解析了(阿里有参考视频) 4.ssl 证书   免费版,网上有教程 ...