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
-

Source

传递闭包,不过在TOJ超时了。应该有种更加牛X的做法。

 #include <stdio.h>
#include <string.h>
#define MAXN 220 int n;
int f[MAXN][MAXN][]; void floyd(){
for(int k=; k<=n; k++){
for(int i=; i<=n; i++){
for(int j=; j<=n; j++){
for(int c=; c<; c++){
if( f[i][k][c] && f[k][j][c])
f[i][j][c]=;
}
}
}
}
}
int main()
{
while( scanf("%d" ,&n)!=EOF && n){
memset(f,,sizeof(f));
int u,v;
char ch[];
while( scanf("%d %d",&u ,&v) ){
if(u== && v==)break;
scanf("%s",ch);
for(int i=; ch[i]!='\0'; i++){
f[u][v][ch[i]-'a']=;
}
}
floyd();
while( scanf("%d %d",&u ,&v) ){
if(u== && v==)break;
int flag=;
for(int i=; i<; i++){
if( f[u][v][i] ){
printf("%c",i+'a');
flag=;
}
}
if(!flag){
puts("-");
}else{
puts("");
}
}
printf("\n");
}
return ;
}

大牛的解法,有状态压缩的思想。

f[u][v]:存放是是二进制的状态。

假如u-v之间有a,g,m。那么可以写成 f[u][v]=1000001000001。

它是由以下二进制数通过 |运算得到的。

0000000000001

0000001000000

1000000000000

传递闭包的时候,只要跟当前要取得的位进行&运算就可以了。如果返回是1表示u-v之间的路有当前位所对应的公司参与建造。

 #include <stdio.h>
#include <string.h>
#define MAXN 220 int n;
int f[MAXN][MAXN]; void floyd(){
for(int k=; k<=n; k++){
for(int i=; i<=n; i++){
for(int j=; j<=n; j++){
f[i][j]=f[i][j]|(f[i][k]&f[k][j]);
}
}
}
} int main()
{
while( scanf("%d",&n)!=EOF && n ){
int u,v;
char ch[];
memset(f , ,sizeof(f));
while( scanf("%d %d" ,&u ,&v)!=EOF ){
if(u== && v==)break;
scanf("%s",ch);
for(int i=; ch[i]!='\0'; i++){
f[u][v]=f[u][v]|(<<(ch[i]-'a'));
}
}
floyd();
while( scanf("%d %d" ,&u ,&v)!=EOF ){
if(u== && v==)break;
int flag=;
for(int i=; i<; i++){
if(f[u][v]&(<<i)){
flag=;
printf("%c",i+'a');
}
}
if(!flag)
printf("-");
puts("");
}
puts("");
}
return ;
}

POJ 2570 Fiber Network的更多相关文章

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

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

  2. ZOJ 1967 POJ 2570 Fiber Network

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

  3. poj 2570 Fiber Network(floyd)

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

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

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

  5. POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Network / FZU 1161 (网络流,最大流)

    POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Networ ...

  6. 【POJ 3694】 Network(割边&lt;桥&gt;+LCA)

    [POJ 3694] Network(割边+LCA) Network Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7971 ...

  7. POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集

    POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...

  8. zoj 1967 Fiber Network/poj 2570

    题意就是 给你 n个点 m条边 每条边有些公司支持 问 a点到b点的路径有哪些公司可以支持 这里是一条路径中要每段路上都要有该公司支持 才算合格的一个公司// floyd 加 位运算// 将每个字符当 ...

  9. [并查集] POJ 2236 Wireless Network

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 25022   Accepted: 103 ...

随机推荐

  1. Oracle累计函数

    今天遇到一个客户的报表需求,在shipment的报表中要查看该shipment中的每个PO的采购数量,当前shipment的出货数量以及累计的所有出货数量. 要有累计的出货数,并且是要有顺序的累计出货 ...

  2. Django工程中使用echarts怎么循环遍历显示数据

    前言: 后面要开发测试管理平台,需要用到数据可视化,所以研究了一下 先看下最后的图吧,单击最上方的按钮可以控制柱状图显隐 views.py # -*- coding: utf-8 -*- from _ ...

  3. Socket网络通讯

    网络编程 使用C#进行网络编程时,通常都需要用到System.Net命名空间.System.Net.Sockets命名空间和System.Net.Mail命名空间: 1. System.Net命名空间 ...

  4. C#多线程编程实战1.4终止线程

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  5. 关于pip无法安装scrapy的问题

    安装scrapy时如果出现下列问题: building ' twisted. test. raiser' extension error: Microsoft Visual C++ 14.0 is r ...

  6. 蓝牙4.0BLE抓包(二) – 广播包解析

    版权声明:本文为博主原创文章,转载请注明作者和出处.    作者:强光手电[艾克姆科技-无线事业部] 在使用EN-Dongle捕获和解析广播包之前,我们先了解一下BLE报文的结构,之后,再对捕获的广播 ...

  7. 构建docker镜像

    一.通过docker commit命令构建镜像 docker commit 构建镜像可以想象为是将运行的镜像进行重命名另存一份.我们先创建一个容器,并在容器里做出修改,就像修改代码一样,最后再将修改提 ...

  8. java基础_01

    一.java中的数据类型 1.基本数据类型:四类八种 byte(1),boolean(1),short(2),char(2),int(4),float(4),long(8),double(8); 2. ...

  9. Spring学习笔记(一)—— Spring介绍及入门案例

    一.Spring概述 1.1 Spring是什么 Spring是一个开源框架,是于2003年兴起的一个轻量级的Java开发框架, 由Rod Johnson 在其著作<Expert one on ...

  10. Foremost恢复Linux中已删除的文件

    Foremost 我们只能在Linux中恢复已删除的文件,只要这些扇区在硬盘上没有被覆盖. 首先安装 要在CentOS上安装Foremost,我们将从官方网页下载并安装最前面的rpm.打开终端并执行以 ...