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想要从陷阱 ...
随机推荐
- Spring MVC的异步模式(ResponseBodyEmitter、SseEmitter、StreamingResponseBody) 高级使用篇
DeferredResult高级使用 上篇博文介绍的它的基本使用,那么本文主要结合一些特殊的使用场景,来介绍下它的高级使用,让能更深刻的理解DeferredResult的强大之处. 它的优点也是非常明 ...
- Hive怎么使用远程连接
HIVE的连接模式== 本地连接模式 直接启动hive命令 HIVE的远程连接 这里要启动HIVE的服务 thirft进行编写 hiveserver2 —- > 前台启动 后台启动 前台启动 h ...
- java多线程的优先性问题
多线程的优先级问题 重点:理解线程优先级的继承性.规则性.随机性 线程的优先级 在操作系统中,线程可以划分优先级,.尽可能多的给优先级高的线程分配更多的CPU资源. 线程的优先级分为1~10,有三个预 ...
- [LGP2000] 拯救世界
6的倍数 1/(1-x^6) 最多9块 (1-x^10)/(1-x) 最多5块 (1-x^6)/(1-x) 4的倍数 1/(1-x^4) 最多7块 (1-x^8)/(1-x) 2的倍数 1/(1-x^ ...
- python-day39(正式学习)
目录 线程锁 死锁问题及递归锁 死锁 递归锁 信号量 GIL全局解释器锁 线程锁 from threading import Thread,Lock x=0 lock=Lock() def test( ...
- Ruby Rails正式学习:Ruby on Rails 做个演示项目吧,逐渐完善
项目开始 一. 新建Rails项目 1. 修改一下Gemfile文件(简单修改一下) source 'https://rubygems.org' git_source(:github) { |repo ...
- Collection接口的子接口——Queue接口
https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html public interface Queue<E> exten ...
- linux系统内核优化参数
1. 系统连接数优化 # vim /etc/security/limits.conf * soft nofile 65535 * hard nofile 65535 * soft noproc 655 ...
- Docker:Swarm + Stack 一站式部署容器集群
参考1 参考2 1.注意docker的版本,yum默认安装的版本比较低,可能出现 unsupported Compose file version: 3.7 docker版本升级 2.docker-c ...
- Ubuntu分区挂载
创建主分区: 25G 主分区 空间起始位置 Ext4日志文件系统 / (ps:安装主要放这了,原因不明) 创建swap分区: 8192MB 逻辑分区 空间起 ...