POJ 2570 Fiber Network
Description
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
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
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的更多相关文章
- POJ 2570 Fiber Network(最短路 二进制处理)
题目翻译 一些公司决定搭建一个更快的网络.称为"光纤网". 他们已经在全世界建立了很多网站.这 些网站的作用类似于路由器.不幸的是,这些公司在关于网站之间的接线问题上存在争论,这样 ...
- ZOJ 1967 POJ 2570 Fiber Network
枚举起点和公司,每次用DFS跑一遍图,预处理出所有的答案.询问的时候很快就能得到答案. #include<cstdio> #include<cmath> #include< ...
- poj 2570 Fiber Network(floyd)
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int ...
- POJ 2579 Fiber Network(状态压缩+Floyd)
Fiber Network Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3328 Accepted: 1532 Des ...
- 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 ...
- 【POJ 3694】 Network(割边<桥>+LCA)
[POJ 3694] Network(割边+LCA) Network Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7971 ...
- POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集
POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...
- zoj 1967 Fiber Network/poj 2570
题意就是 给你 n个点 m条边 每条边有些公司支持 问 a点到b点的路径有哪些公司可以支持 这里是一条路径中要每段路上都要有该公司支持 才算合格的一个公司// floyd 加 位运算// 将每个字符当 ...
- [并查集] POJ 2236 Wireless Network
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 25022 Accepted: 103 ...
随机推荐
- 使用Filter对POST和GET方式的请求参数的进行统一解码
一.过滤器 二.自定义request类
- CentOS 进程操作
ps -ef:查看所有进程, ps -ef |grap firewalld 查看与firewalld相关的进程 which :查看进程:which firewalld kill 进程id:杀掉进程 k ...
- .Net Core 项目引用本地类库方式(一)
最近了解到.NET Core 项目,引用本地类库DLL的方式有三种 1.非同解决方案下的引用,直接引用,浏览,找到对应的DLL,然后确定引用. 这种方式有个不好的地方就是,如果引用的DLL文件里面,也 ...
- utp
接口测试大致分为两种:数据驱动和代码驱动 数据驱动:主要处理用例之间没有关联关系的用例集合,一般以(excel.yaml)文件形式存储用例 代码驱动:主要是处理用例之间存在关联关系的用例(如:抽奖,需 ...
- JVM高级特性与实践(一):Java内存区域 与 内存溢出异常
套用<围城>中的一句话,“墙外面的人想进去,墙里面的人想出来”,用此来形容Java与C++之间这堵内存动态分配和垃圾收集技术所围成的“围墙”就再合适不过了. 对于从事C.C++的开发人员而 ...
- J2EE 的体系结构
J2EE 即Java2平台企业版,它提供了基于组件的方式来设计.开发.组装和部署企业应用.J2EE使用多层分布式的应用模型,这个多层通常通过三层或四层来实现: 1.客户层,运行在客户计算机上的组件. ...
- 老男孩Day15作业:商城列表页面(静态)
一. 一.作业需求: 1.完成商城列表静态页面的抒写 二.博客地址:https://www.cnblogs.com/catepython/p/9205636.html 三.运行环境 操作系统:Win1 ...
- Linux硬件信息采集
dmidecode: 简介: dmidecode命令通过读取DMI数据库获取硬件信息并输出.由于DMI信息可以人为修改,因此里面的信息不一定是系统准确的信息 dmidecode遵循SMBIOS/DMI ...
- [NOI2010]能量采集 BZOJ2005 数学(反演)&&欧拉函数,分块除法
题目描述 栋栋有一块长方形的地,他在地上种了一种能量植物,这种植物可以采集太阳光的能量.在这些植物采集能量后,栋栋再使用一个能量汇集机器把这些植物采集到的能量汇集到一起. 栋栋的植物种得非常整齐,一共 ...
- 洛谷 P1149 火柴棒等式
嗯.... 这道题好讨厌啊!!!! 一开始莫名RE,然后发现数组小了,然后发现后面几个点总是WA,原来推的少了.... 并且这道题的思路真的好水啊!! 先看一下题: 题目描述 给你n根 ...