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想要从陷阱 ...
随机推荐
- [转帖]Java 8新特性探究 前言
Java 8新特性探究 前言 https://my.oschina.net/benhaile/blog/174136 讲下java的历史 感觉挺好的. 评论 17 jdk8java8javase新特性 ...
- Spread.NET 表格控件 V12.0 Update2 发布更新
Spread.NET表格控件V12.0 Update 2 已经正式发布,本次发布主要针对WinForm平台下客户反馈的产品使用功能进行优化,并修复了已知问题,具体修复情况见下方说明. Spread.N ...
- ABC143F Distinct Numbers
这道题非常好.其思想类似于 $O(n \log n)$ 求最长上升子序列的算法. hint:考虑固定操作次数 $o$,$k$ 最大可取到多少? int n; scan(n); vi a(n); sca ...
- jpa报错 Unable to acquire a connection from driver [null], user [null] and URL [null]
jpa报错 Unable to acquire a connection from driver [null], user [null] and URL [null] 为啥报错 因为你在persist ...
- Abp添加新的Api(不扩展底层方法)
定义新的实体类:FileManage;继承 FullAuditedEntity<Guid> 在XX.Application 中定义IXXservice及实现XXservice public ...
- private修饰的方法可以通过反射访问,那么private的意义是什么?
反射代码: package test; public class Person { private String userName= "Tom"; private void pla ...
- Java中“==”与equals的区别以及equals方法的重写
一.“==”与equals的区别: (1)==代表比较双方是否相同: 基本数据类型表示值相等. 引用数据类型表示地址相等,即同一个对象. (2)Object中的equals()方法:是否为同一个对象的 ...
- C# 过滤字典中的数据 并将过滤后的数据转成新的字典对象
Dictionary<string, object> dic = new Dictionary<string, object>(); dic.Add("); dic. ...
- 微信小程序刮刮乐
<view class="scratch_body"> <image class="scratch_body_bg" mode="w ...
- 19、Firewalld防火墙
安全的考虑方向: 安全框架 OSI七层模型 硬件 机架上锁(机柜) 温度 硬件检查 网络 iptables/firewalld 仅允许公司的IP地址能连接服务器的22端口 公有云使用 安全组 系统 没 ...