poj 2570 Fiber Network(传递闭包,floyd+位运算)
来源:互联网 发布:域名的价值 编辑:程序博客网 时间:2024/04/29 07:27
Fiber Network
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 2703 Accepted: 1242
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.
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.
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
31 2 abc2 3 ad1 3 b3 1 de0 01 32 13 20 021 2 z0 01 22 10 00
Sample Output
abd-z-
题意:给出n个结点,给出若干条连接结点的路径,路径是单向的,路径上有小写字母。例如:当结点1和结点2之间的路径有a、b,结点2和结点3之间的路径有b、c,那么结点1和结点3之间的路径就会出现b(即使结点1和结点3之间没有直接相连的路径或路径上没有b)。每次询问结点a和b之间的路径有哪个小写字母。
思路:传递闭包,用floyd。若n^3的floyd再加上查找相同字母,则显然TLE。有什么方法不用查找相同的字母?用位运算!G[a][b]保存a与b路径上的小写字母的集合,通过并(|)运算即可合并结合
AC代码:
#include <cstring>#include <string>#include <cstdio>#include <algorithm>#include <queue>#include <cmath>#include <vector>#include <cstdlib>#include <iostream>using namespace std;int main(){ int n; int a,b; int G[205][205]; char com[30]; while(cin>>n,n) { memset(G,0,sizeof(G)); while(cin>>a>>b) { if(!a&&!b) break; cin>>com; int i=0; while(com[i]) G[a][b]|=1<<(com[i++]-'a'); } for(int k=1; k<=n; k++) for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) if(G[i][k]&G[k][j]) G[i][j]|=G[i][k]&G[k][j]; while(cin>>a>>b) { if(!a&&!b) break; if(G[a][b]!=0) { int i=0,temp=G[a][b]; while(temp) { if(temp&1) putchar(i+'a'); temp>>=1; i++; } } else putchar('-'); putchar('\n'); } putchar('\n'); } return 0;}
- poj 2570 Fiber Network(传递闭包,floyd+位运算)
- POJ--2570--Fiber Network【floyd+位运算】
- POJ 2570 Fiber Network(Floyd传递闭包+状态压缩)
- poj 2570 Fiber Network (两种解法 1.floyd+位操作 2.传递闭包)
- [floyd]poj 2570 fiber network #floyd+位运算
- POJ 2570 Fiber Network(floyd闭包)
- poj 2570 Fiber Network --floyd求传递闭包加位运算
- POJ 2570Fiber Network(Floyd+位运算)
- POJ2570 Fiber Network floyd传递闭包
- poj 2570 Fiber Network Floyd
- poj 2570 Fiber Network(floyd)
- zoj 1967 Fiber Network(位运算&&FLOYD)
- poj 2570 Fiber Network(最短路+Floyd)
- poj 2570 Fiber Network (Floyd)
- poj 2570 Fiber Network floyd算法
- Poj 2570 Fiber Network【Floyd+状态压缩】
- poj 2570 Fiber Network(floyd)
- poj 2570 Fiber Network Floyd思想+二进制处理
- ffmpeg 解码h264数据
- 让XCode的 Stack Trace信息可读
- 使用Eclipse和maven构建springmvc项目
- Linq to DataSet
- C 专家编程笔记(三) 数组和指针
- poj 2570 Fiber Network(传递闭包,floyd+位运算)
- fedora安装flashplayer
- 解读TCP/UDP数据包(三):UDP数据包结构
- spring注解详解
- 使用IDA对MFC寻找按钮处理事件
- dede:channelartlist dede:channel 无子栏目时取同级栏目解决方案
- 解读TCP/UDP数据包(四):TCP数据包实例解析
- Android_ListViewDemo
- object-c math.h里的数学计算公式介绍