Description

Several startup companies have decided to build a better Internet, called the "FiberNet". They have already installed many nodes that act as routers all around the world. Unfortunately, they started to quarrel about the connecting lines, and ended up with every company laying its own set of cables between some of the nodes. 
Now, service providers, who want to send data from node A to node B are curious, which company is able to provide the necessary connections. Help the providers by answering their queries.

Input

The input contains several test cases. Each test case starts with the number of nodes of the network n. Input is terminated by n=0. Otherwise, 1<=n<=200. Nodes have the numbers 1, ..., n. Then follows a list of connections. Every connection starts with two numbers A, B. The list of connections is terminated by A=B=0. Otherwise, 1<=A,B<=n, and they denote the start and the endpoint of the unidirectional connection, respectively. For every connection, the two nodes are followed by the companies that have a connection from node A to node B. A company is identified by a lower-case letter. The set of companies having a connection is just a word composed of lower-case letters. 
After the list of connections, each test case is completed by a list of queries. Each query consists of two numbers A, B. The list (and with it the test case) is terminated by A=B=0. Otherwise, 1<=A,B<=n, and they denote the start and the endpoint of the query. You may assume that no connection and no query contains identical start and end nodes.

Output

For each query in every test case generate a line containing the identifiers of all the companies, that can route data packages on their own connections from the start node to the end node of the query. If there are no companies, output "-" instead. Output a blank line after each test case.

Sample Input

3
1 2 abc
2 3 ad
1 3 b
3 1 de
0 0
1 3
2 1
3 2
0 0
2
1 2 z
0 0
1 2
2 1
0 0
0

Sample Output

ab
d
- z
-

题目意思:n个路由器,编号1-n,26个公司,编号a-z,路由器之间有一些有向边,边权为一个字符串,字符串由小写字母组成,表示字符串对应的公司能使这条边连通。现在给若干个查询,查询能使任意2个路由器连通的公司。输出公司号,若不存在公司则输出‘-’。

解题思路:本题并不是求最短路,但是却要用到Floyd算法求解最短路的思想。题目就是要求能使任意2个路由器连通的公司的集合,所以可以使用Floyd传递闭包建立联系。另外,在本题中需要很巧妙地处理公司集合,公司是一个小写字母标识,最多只有26个公司,这样可以使用整数二进制位代表每个公司实现集合的状态压缩。

例如,站点1到站点2连接的公司有集合{ ‘a’ , 'b' ,  'c' },可以用“00000000000000000000000111”表示,

站点2到站点3连接的公司有集合{ ‘a’ , 'd' } ,可以用“00000000000000000000001001”表示,

这两个整数进行二进制与运算后得到“00000000000000000000000001”表示通过中间站点2,站点1到站点3的连接的公司是集合{ ‘ a ’ }。

floyd的本质是枚举中间节点k,使节点i到j的距离最大或最小。针对本题,是要求一个集合,使从i到j连通的公司,那么枚举k的时候,就要求保证i->k和k->j同时连通的公司,状态压缩的话,直接将dis[i][k]和dis[k][j]相与便是结果,这个结果要加到dis[i][j]上去,所以再和dis[i][j]相或。所以总的方程就是:

dis[i][j] = dis[i][j] | (dis[i][k]&dis[k][j])。

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int dis[][];
int n,m;
void Floyd()
{
int i,j,k;
for(k=; k<=n; k++)
{
for(i=; i<=n; i++)
{
for(j=; j<=n; j++)
{
dis[i][j]|=(dis[i][k]&dis[k][j]);
}
}
}
}
int main()
{
int a,b,i;
int len;
char s[];
while(scanf("%d",&n)!=EOF)
{
if(n==)
{
break;
}
memset(dis,,sizeof(dis));
while(scanf("%d%d",&a,&b)!=EOF)
{
if(a==&&b==)
{
break;
}
scanf("%s",s);
len=strlen(s);
for(i=; i<len; i++)
{
dis[a][b]|=(<<(s[i]-'a'));///逻辑左移s[i]-'a'位
}
}
Floyd();
while(scanf("%d%d",&a,&b)!=EOF)
{
if(a==&&b==)
{
break;
}
if(dis[a][b])
{
for(i=; i<; i++)
{
if(dis[a][b]&(<<i))///如果dis[a][b]&(1<<i)!=0,说明dis[a][b]所代表的集合中包含公司'a'+i
{
putchar('a'+i);
}
}
}
else
{
putchar('-');
}
putchar('\n');
}
putchar('\n');
}
return ;
}

Fiber Network ZOJ 1967(Floyd+二进制状态压缩)的更多相关文章

  1. POJ 2777.Count Color-线段树(区间染色+区间查询颜色数量二进制状态压缩)-若干年之前的一道题目。。。

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 53312   Accepted: 16050 Des ...

  2. hdu 1429 bfs+二进制状态压缩

    开始时候只用了BFS,显然超时啊,必然在结构体里加一个数组什么的判重啊,开始用的一个BOOL数组,显然还是不行,复杂度高,每次都要遍历数组来判重:后百度之,学习了二进制状态压缩,其实就用一个二进制数来 ...

  3. # 最短Hamilton路径(二进制状态压缩)

    最短Hamilton路径(二进制状态压缩) 题目描述:n个点的带权无向图,从0-n-1,求从起点0到终点n-1的最短Hamilton路径(Hamilton路径:从0-n-1不重不漏的每个点恰好进过一次 ...

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

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

  5. 二进制状态压缩dp(旅行商TSP)POJ3311

    http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Subm ...

  6. POJ-3279.Fliptile(二进制状态压缩 + dfs) 子集生成

    昨天晚上12点刷到的这个题,一开始一位是BFS,但是一直没有思路.后来推了一下发现只需要依次枚举第一行的所有翻转状态然后再对每个情况的其它田地翻转进行暴力dfs就可以,但是由于二进制压缩学的不是很透, ...

  7. HDU_1429——胜利大逃亡续,十位二进制状态压缩,状态判重

    Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)……这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁 ...

  8. BFS+二进制状态压缩 hdu-1429

    好久没写搜索题了,就当练手吧. vis[][][1025]第三个维度用来维护不同key持有状态的访问情况. 对于只有钥匙没有对应门的位置,置为'.',避免不必要的状态分支. // // main.cp ...

  9. ZOJ - 3471 Most Powerful (状态压缩)

    题目大意:有n种原子,两种原子相碰撞的话就会产生能量,当中的一种原子会消失. 问这n种原子能产生的能量最大是多少 解题思路:用0表示该原子还没消失.1表示该原子已经消失.那么就能够得到状态转移方程了 ...

随机推荐

  1. Spring boot Mybatis整合构建Rest服务(超细版)

     Springboot+ Mybatis+MySql整合构建Rest服务(涵盖增.删.改.查) 1.概要 1.1 为什么要使用Spring  boot? 1.1.1 简单方便.配置少.整合了大多数框架 ...

  2. shell习题第9题:sed的常用用法

    [题目要求] 把一个文本文档的前5行中包含字母的行删除掉,同时把6到10行中的全部字母删除掉. [核心要点] sed命令 [脚本] .txt |sed '/[a-zA-Z]/d' .txt |sed ...

  3. 【Linux】YUM源搭建

    YUM是什么? YUM是什么 基于rpm但更胜于rpm的软件管理工具: YUM有服务端和客户端: 如果服务端和客户端在同一台机器,这是本地YUM: 如果服务端和客户端不在同一台机器,这是网络YUM. ...

  4. emlog 百度熊掌号提交插件-基于Emlog6.0.1特别版美化

    插件截图 插件简介 此插件在发布文章的时候自动向百度熊掌号提交,有利于百度熊掌号收录.基于Emlog6.0.1特别版美化的插件. 在百度推送插件的基础上修改制作而成与百度推送共存,解放双手,走向人生巅 ...

  5. git pull 发生冲突解决办法

    冲突原因:远程仓库的同一个文件的代码,和本地的文件代码不一样 解决办法 : 1.git stash (把本地冲突的代码隐藏) 2.git pull 3.git stash pop (将隐藏的和pull ...

  6. 一图看懂hadoop Spark On Yarn工作原理

    hadoop Spark On Yarn工作原理

  7. 一图看懂hadoop分布式文件存储系统HDFS工作原理

    一图看懂hadoop分布式文件存储系统HDFS工作原理

  8. 自学tensorflow——1.框架初步了解以及构建简单的计算图计算

    1.初步了解 tensorflow是谷歌的一款开源深度学习框架.运行前,需要先定义好计算图,最后通过会话启动计算图,这么做的目的是为了防止数据在python和c++(tensorflow底层)传输的时 ...

  9. 浅谈Vue响应式(数组变异方法)

    很多初使用Vue的同学会发现,在改变数组的值的时候,值确实是改变了,但是视图却无动于衷,果然是因为数组太高冷了吗? 查看官方文档才发现,不是女神太高冷,而是你没用对方法. 看来想让女神自己动,关键得用 ...

  10. 在Linux Mint 19 / Linux Mint 18上安装VirtualBox 6.0 / 5.2

    如果你直接可以 sudo apt-get install virtualbox-6.0那就相安无事 否则参考https://www.itzgeek.com/how-tos/linux/linux-mi ...