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. query聚类技术

    query聚类 目的 query聚类主要有以下两个目的 解决query空间稀疏问题(长尾query) 挖掘用户意图(一条行为包含的意图是稀疏的,当有一簇行为时,意图更明确) 可以说聚类是构建内容模型的 ...

  2. string Format转义大括号

    String.Format("{0} world!","hello") //将输出 hello world!,没有问题,但是只要在第一个参数的任意位置加上一个大 ...

  3. EasyUI控件combobox重复请求后台,dialog窗口数据异常

    最近在用Easy UI+Dapper+MVC4 开发一个财务收款系统,其中就发现一些小问题,供有需要的人参考. 1.EasyUI控件combobox 数据绑定 出现重复请求后台 上代码: <td ...

  4. Invoke()的使用

    (最近在看协程) Invoke()方法是一种委托机制 Invoke ( "SendMsg", 3 ), 意思是3秒之后调用 SendMsg() 方法 使用时应该注意以下几点: 1. ...

  5. rsync实时备份备份服务搭建和使用指南

    一.Rsync企业工作场景说明: 1.利用定时任务+rsync方式实现数据同步 对于网站内部技术人员创建的数据,可以采取定时任务的方式 2.利用实时任务+rsync方式实现数据同步 对于网站外部访问用 ...

  6. tomcat访问日志关闭

    在tomcat(实例)路径下[conf/server.xml]中修改,以下节点(注释掉该节点): tomcat catalina.out日志关闭 在tomcat(主目录)路径下[bin/catalin ...

  7. virtueBox实现虚拟机的复制和粘贴

    1.在设备--共享粘贴板--勾选双向. 2.在设备--拖放--勾选双向. 3.在设备--安装增强功能,然后进入虚拟机安装增强功能即可.

  8. 1221: Fibonacci数列 [数学]

    1221: Fibonacci数列 [数学] 时间限制: 1 Sec 内存限制: 128 MB 提交: 116 解决: 36 统计 题目描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn- ...

  9. Web渗透测试(xss漏洞)

    Xss介绍—— XSS (cross-site script) 跨站脚本自1996年诞生以来,一直被OWASP(open web application security project) 评为十大安 ...

  10. Shell等,不等......

    -eq           //等于 -ne           //不等于 -gt            //大于 (greater ) -lt            //小于  (less) -g ...