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 ...
随机推荐
- window中启动vs后鼠标无法移动
你停止wisptis.exe这个进程,在c:\Windows\System32下删除wispitis.exe就可以了!
- repeater+aspnetpager 组合分页
页面代码 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TF_Product. ...
- Mongo Windows 基本使用入门
1.安装https://www.mongodb.com/download-center#community注意:安装 "install mongoDB compass" 不勾选下载 ...
- C#三层架构搭建
一.简介 主要分为:界面层(User Interface layer),业务逻辑层(Business Logic Layer),数据访问层(Data access layer) 1.作用 界面层(UI ...
- intellij idea 15,webstorm 最新注册破解
http://idea.lanyus.com 在激活地址填写上上面的地址就行了,非常简单有效. 亲测idea,webstorm...都可以激活
- ajax-2
serialize()输出序列化表单值的结果: 如: <html> <head> <script type="text/javascript" src ...
- python3好用的mysql.connector库
python3好用的mysql.connector库 from mysql.connector import connect #建立mysql连接,生成一个mysql.connector对象 conn ...
- 使用Eclipse的几个必须掌握的快捷方式
“工若善其事,必先利其器”,感谢Eclipse,她 使我们阅读一个大工程的代码更加容易,在阅读的过程中,我发现掌握几个Eclipse的快捷键会使阅读体验更加流畅,写出来与诸君分享,欢迎补充. 1. C ...
- Java程序员修炼之路
作者简介:王成委,CSDN知识库特邀编辑,Java高级工程师,熟悉Java编程语言和Oracle数据库.专注于高并发架构设计和大数据存储方向的研究. 我们为什么选择Java 大多数人选择Java可能只 ...
- C语言数据结构-链式队列的实现-初始化、销毁、清空、长度、队列头元素、插入、删除、显示操作
1.数据结构-链式队列的实现-C语言 typedef struct QNode { int data; struct QNode *next; }QNode,*QueuePtr; typedef st ...