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. Android Activity的切换动画(overridePendingTransition)

    overridePendingTransition 1.平时Activity的切换是就是从中间弹出来,然后遮盖住之前的Activity.这种效果看到很多后就想给他换成其他的效果,如: 要显示的Acit ...

  2. CentOS 进程操作

    ps -ef:查看所有进程, ps -ef |grap firewalld 查看与firewalld相关的进程 which :查看进程:which firewalld kill 进程id:杀掉进程 k ...

  3. Sql 不确定列 行转列操作

    做项目时,用到了汇总统计的行转列,且 表结构: 具体存储过程脚本如下: -- =============================================-- Author:  -- C ...

  4. 创建Database Diagrams时遇到的问题

    SQL2008 R2中时,Diagrams的问题 Error: ------------------------------ Database diagram support objects cann ...

  5. 十七、创建一个 WEB 服务器(一)

    1.Node.js 创建的第一个应用 var http=require("http") http.createServer(function (req,res) { res.wri ...

  6. conda install 安装太慢怎么办?

    小编我在安装tensorflow和keras的过程中,安装进程太慢,小木棍一直在转圈...抓狂... 如何解决??? 使用清华提供的anaconda镜像,使用以后真的很快! Anaconda 镜像使用 ...

  7. 【离散数学】 SDUT OJ 1.1联结词真值运算

    1.1联结词真值运算 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description 已知命题变元p和 ...

  8. bdd相关整理介绍

    BDD介绍 什么是BDD Behavior-driven development In software engineering, behavior-driven development (BDD) ...

  9. win10进入安全模式的方法

    https://jingyan.baidu.com/article/a3aad71ac5919bb1fa009667.html

  10. Python之freshman01

    列表与元组.字典 1.列表list:["ele1","ele2","ele3","ele0"] 列表是一组任意类型的值, ...