题意:一些公司决定搭建一些光纤网络。单向的,假设从第一点到第二点,有ab两个公司能够搭建,第二点到第三点有ac两个公司能够搭建,第一点到第三点有d公司能够搭建,则第一点到第三点有a、d两个公司能够搭建。a是通过第二点。d是直接连接两点。如今给你这么一个光纤网络。问某两点之间有哪些公司能够搭建起网络。

首先这题是个多源点的。有点像最短路的思想,假设让我做我肯定硬着头皮找同样的字母,只是我看到图论书里的想法非常好,就写上来了。

由于公司是用一个小写字母来标识的,且每一个公司的标识互不同样,也就是说最多仅仅有26个公司。因此。能够用整数中的二进制位来表示每一个公司,通过二进制位运算来实现集合的“或”运算和“与”运算。

假设第一点到第二点有a、b、c三个公司,则可用“00000000000000000000000000000111”来表示,第二点到第三点有a、d两个公司。则可用“00000000000000000000000000001001“来表示。所以假设用edge数组表示某两点之间的公司,就能够这么更新:

edge[i][j] |= edge[i][k] & edge[k][j]。

然后是输入输出的处理,在读入字符串后,要用逻辑左移存入到edge数组中:edge[a][b] = 1 << (s[i]-'a')

输出的处理。枚举字符ch从‘a’到‘z’,假设edge[a][b]&(1<<ch-'a')为1,则表示edge[a][b]所代表的集合中包括ch公司,则输出

假设edge[a][b]==0,说明a、b之间没有公司了

二进制和位运算真是好东西

#include<cstring>
#include<string>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cctype>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<stack>
#include<ctime>
#include<cstdlib>
#include<functional>
#include<cmath>
using namespace std;
#define PI acos(-1.0)
#define MAXN 510
#define eps 1e-7
#define INF 0x7FFFFFFF
#define seed 131
#define ll long long
#define ull unsigned ll
#define lson l,m,rt<<1
#define rson r,m+1,rt<<1|1 int n;
int edge[MAXN][MAXN];
char s[30];
void floyd(){
int i,j,k;
for(k=1;k<=n;k++){
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
edge[i][j] |= edge[i][k] & edge[k][j];
}
}
}
}
int main(){
int i,j;
int a,b;
while(scanf("%d",&n),n){
memset(edge,0,sizeof(edge));
while(scanf("%d%d",&a,&b),a||b){
scanf("%s",s);
int l = strlen(s);
for(i=0;i<l;i++){
edge[a][b] |= 1 << (s[i]-'a');
}
}
floyd();
while(scanf("%d%d",&a,&b),a||b){
for(i=0;i<=26;i++){
if(edge[a][b]&(1<<i)) printf("%c",'a'+i);
}
if(!edge[a][b]) printf("-");
printf("\n");
}
printf("\n");
}
return 0;
}

POJ--2570--Fiber Network【floyd+位运算】的更多相关文章

  1. poj 2570 Fiber Network(floyd)

    #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int ...

  2. POJ 2570 Fiber Network(最短路 二进制处理)

    题目翻译 一些公司决定搭建一个更快的网络.称为"光纤网". 他们已经在全世界建立了很多网站.这 些网站的作用类似于路由器.不幸的是,这些公司在关于网站之间的接线问题上存在争论,这样 ...

  3. POJ 2570 Fiber Network

    Description Several startup companies have decided to build a better Internet, called the "Fibe ...

  4. ZOJ 1967 POJ 2570 Fiber Network

    枚举起点和公司,每次用DFS跑一遍图,预处理出所有的答案.询问的时候很快就能得到答案. #include<cstdio> #include<cmath> #include< ...

  5. POJ 2531 Network Saboteur 位运算子集枚举

    题目: http://poj.org/problem?id=2531 这个题虽然是个最大割问题,但是分到dfs里了,因为节点数较少.. 我试着位运算枚举了一下,开始超时了,剪了下枝,1079MS过了. ...

  6. POJ 2579 Fiber Network(状态压缩+Floyd)

    Fiber Network Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3328   Accepted: 1532 Des ...

  7. zoj1967 poj2570 Fiber Network (floyd算法)

    虽然不是最短路,但是询问时任意两点之间的信息都要知道才能回答,由此联想到floyd算法,只要都floyd算法的原理理解清楚了就会发现:这道题的思想和求任意两点之间的最短路的一样的,只不过是更新的信息不 ...

  8. [poj]开关类问题 枚举 位运算

    poj 1222  EXTENDED LIGHTS OUT 开关只有两种方案 按和不按,按两次相当于关 只用枚举第一排开关的按法即可,剩下的行为使上一排的灯全部关闭,按法可以确定,并且是唯一的. 最后 ...

  9. POJ 1166 The Clocks [BFS] [位运算]

    1.题意:有一组3*3的只有时针的挂钟阵列,每个时钟只有0,3,6,9三种状态:对时针阵列有9种操作,每种操作只对特点的几个时钟拨一次针,即将时针顺时针波动90度,现在试求从初试状态到阵列全部指向0的 ...

随机推荐

  1. appium+python 【Mac】Android夜神模拟器

    1.官网下载地址:https://www.yeshen.com/ 2.具体的夜神模拟器的介绍请自查 3.下载安装后夜神模拟器后,打开模拟器,进行相应的配置如下: 4. (1).找到android-sd ...

  2. spark集群安装[转]

    [转]http://sofar.blog.51cto.com/353572/1352713 ====================================================== ...

  3. 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)

    Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...

  4. The file will have its original line endings in your working directory.

    在空仓库的情况下,add,出现一下问题 The file will have its original line endings in your working directory. 当报这个警告时是 ...

  5. java jdbc深入理解(connection与threadlocal与数据库连接池和事务实)

    1.jdbc连接数据库,就这样子 Class.forName("com.mysql.jdbc.Driver");java.sql.Connection conn = DriverM ...

  6. ubuntu 16.04.1 LTS postgresql安装配置

    postgresql安装--------------------二进制安装:wget https://get.enterprisedb.com/postgresql/postgresql-9.5.6- ...

  7. Am335x SPI 驱动测试

    内核版本:3.14.65 CPU:Am335x 1.编译内核: make menuconfig Device Drivers -> <*>SPI support -> < ...

  8. 【基础知识】JavaScript基础

    [学习日记]JavaScript基础 1,一般写在<head></head>中(其实可以放到任意位置); 2,弹出对话框 <scripttype="text/j ...

  9. Rsync服务部署使用

    rsync服务搭建过程(daemon模式) 配置服务 在/etc/rsyncd.conf文件中写入相应的配置: uid = root gid = root use chroot = no max co ...

  10. 最短网络Agri-Net

    [问题描述] 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当然,他需要你的帮助.约翰已经给他的农场安排了一条高速的网络线路,他想把这条线路共享给其他农场. ...