TTTTTTTTTTT 400D Dima and Bacteria 细菌 最短路
题意: 题目大意:给出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 细菌 最短路的更多相关文章
- codeforces 400D Dima and Bacteria 并查集+floyd
题目链接:http://codeforces.com/problemset/problem/400/D 题目大意: 给定n个集合,m步操作,k个种类的细菌, 第二行给出k个数表示连续的xi个数属于i集 ...
- Codeforces400D Dima and Bacteria
题意:给你一个无向有权的图,图上的点被分成了几类,对于同类的点你需要判断它们之间相互的最短距离是不是0.满足这个条件之后要输出的是类与类之间的最短距离的矩阵.点给到10^5这么多,判断同类的点显然不能 ...
- codeforces Dima and Bacteria
题意:给出n,m和k,表示有n个细菌,m种仪器和k种细菌,给出k种细菌的数量ci,然后每个细菌按照种类排成一排(所以有第i种细菌的序号从∑(1≤j≤i-1)cj + 1 到∑(1≤j≤i)cj):接下 ...
- codeforces 400 D Dima and Bacteria【并查集 Floyd】
题意:给出n个点,分别属于k个集合,判断每个集合里面的点的距离都为0,为0的话输出yes,并输出任意两个集合之间的最短路 这道题目有两个地方不会处理, 先是n个点,分别属于k个集合,该怎么记录下来这里 ...
- SDKD 2017 Summer Single Training #03
今天的题目有 6 个. 第一题: CodeForces - 400D Dima and Bacteria 这个题实际是不难的,难的可能在题意的理解上还有题干有点长,这个题很考察题意上面,知识点很熟悉 ...
- CodeForces 400
A - Inna and Choose Options Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d &a ...
- Codeforces Round #234 (Div. 2)
A. Inna and Choose Options time limit per test 1 second memory limit per test 256 megabytes input st ...
- cf div2 234 D
D. Dima and Bacteria time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codefroces 366 D Dima and Trap Graph (最短路)
Dima and Trap Graph 题意:Dima和Inna越来越喜欢对方,但是Dima的室友缺很没有热情出去玩,然后Dima就把他室友Seryozha骗进了陷阱里.现在Seryozha想要从陷阱 ...
随机推荐
- 【转帖】2018全球公有云IaaS榜单出炉:阿里、腾讯、中国电信、金山云列前十
2018全球公有云IaaS榜单出炉:阿里.腾讯.中国电信.金山云列前十 https://news.cnblogs.com/n/628391/ 中国电信貌似就是用的华为的技术 阿里 腾讯 华为 金山 百 ...
- AC自动机模版
我以前一直觉得AC自动机就是我打一个代码,然后可以帮我自动AC题目,现在才知道原来是处理字符串的一个有意思的东西,然后我们现在就来看一下这个东西 1464: [视频][AC自动机]统计单词出现个数 时 ...
- java检测是不是移动端访问
request可以用别的代替 private static boolean isMobile(){ HttpServletRequest request = ThreadContextHolder.g ...
- Sentinel基本使用--基于QPS流量控制(二), 采用Warm Up预热/冷启动方式控制突增流量
Sentinel基本使用--基于QPS流量控制(二), 采用Warm Up预热/冷启动方式控制突增流量 2019年02月18日 23:52:37 xiongxianze 阅读数 398更多 分类专栏: ...
- 机器学习-SVM-核函数
SVM-核函数 在研究了一天的SVM核函数后,我顿悟了一个道理: 研究和使用核函数的人,从一开始的目的就是把data分开而已.高维和映射,都是原来解释操作合理性的,但根本不是进行这一操作的原因 我为什 ...
- qt webengineview 加载本地资源方式
一.如果把资源添加到本地资源qrc库里了,请使用 ui->preview->setUrl(QUrl("qrc:/HelloWorld2.html")): 二.如果没有现 ...
- node.js学习之路(1)
node.js 属于后台语言,后台语言还有php,java等. 优势:1.性能好 node.js VS php 86倍 2.跟前台JS配合方便 3.node.js便于前端学习 https:// ...
- new angular 项目的工作区配置文件和应用源文件
1.工作区配置文件 每个工作空间中的所有项目共享同一个 CLI 配置环境 .该工作空间的顶层包含着全工作空间级的配置文件.根应用的配置文件以及一些包含根应用的源文件和测试文件的子文件夹. 工作空间配置 ...
- linux使用VNC服务轻松远程安装oracle
VNC服务在远程服务器上安装oracle,新手安装oracle时总会遇到这样或者那样的问题,下面我就详细解说一下安装过程,其实oracle安装很简单,并不要把他相像的特别复杂. 本环境用:centos ...
- emwin之窗口ID的唯一性
@2019-04-30 [小记] emwin窗口ID是唯一的 emwin多次创建同一窗口,则窗口句柄不同,多次删除窗口采取LIFO机制,即最新创建的窗口被首先删除 获取多次创建同一窗口的ID,准确位置 ...