POJ2570 Fiber Network

该博客介绍了一个关于FiberNet网络中不同公司之间连接的查询解决方案,通过输入网络节点和连接信息,输出能够提供从起点到终点路径的公司名称。每个测试案例包括节点数、连接列表和查询列表,最后输出相关公司的标识符或提示无连接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                                                                          Fiber Network
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2495 Accepted: 1146

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

 
 
思路:题目首先给出点A,B和拥有这两点之间连线的公司代号(如a),然后给出一系列的起始点,问拥有联通这些起始点之间的线路的公司有哪些。如果没有输出-。
       本题可以使用FLOYD,然后如果某个公司拥有相应的起始点的连线则用1表示,否则用0表示。而这些公司是从a~z所以可以使用int类型来按位表示。
 
 
  1 #include <cstdlib>
  2 #include <iostream>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <cstdio>
  6 #include <cctype>
  7 
  8 using namespace std;
  9 
 10 
 11 int data[201][201];
 12 
 13 
 14 int main(int argc, char *argv[])
 15 {
 16     
 17     
 18 int n;
 19 int i,j,k;
 20 int t;
 21 
 22 while(scanf("%d",&n)!=EOF)
 23 {
 24                           if(n==0)
 25                           break;
 26                           
 27                           for(i=1;i<=n;i++)
 28                           for(j=1;j<=n;j++)
 29                           {
 30                                            data[i][j]=0;
 31                           }
 32                           
 33                           
 34                           
 35                           
 36                           int a,b;
 37                           while(scanf("%d %d",&a,&b)!=EOF)
 38                           {
 39                                           if((a==0)&&(b==0))
 40                                           break;
 41                                           
 42                                           
 43                                           getchar();
 44                                           char str[30];
 45                                           
 46                                           scanf("%s",str);
 47                                           getchar();
 48                                           
 49                                           
 50                                           int len=strlen(str);
 51                                           for(i=0;i<len;i++)
 52                                           {
 53                                                             data[a][b]|=(0x40000000>>(str[i]-'a'));
 54                                           }
 55                                           
 56                                           
 57                           }
 58                           
 59                           
 60                           for(k=1;k<=n;k++)
 61                           for(i=1;i<=n;i++)
 62                           for(j=1;j<=n;j++)
 63                           {
 64                                            data[i][j]|=data[i][k]&data[k][j];
 65                           }
 66                           
 67                           
 68                           
 69                           
 70                           while(scanf("%d%d",&a,&b)!=EOF)
 71                           {
 72                                                          if((a==0)&&(b==0))
 73                                                          break;
 74                                                          
 75                                                          int tag=0;
 76                                                          
 77                                                          for(i=0;i<26;i++)
 78                                                          {
 79                                                                           
 80                                                                           if((data[a][b]<<(i+1))<0)
 81                                                                           {tag=1;putchar('a'+i);//printf("%c",i+'a');
 82                                                                           }
 83                                                          }
 84                                                          
 85                                                          if(tag==0)
 86                                                          {printf("-");}
 87                                                          
 88                                                          
 89                                                          printf("\n");
 90                           }
 91                           
 92                           puts("");
 93 }
 94                                                          
 95                                                          
 96                                                          
 97                                                          
 98                                           
 99                                           
100                                           
101                                           
102 
103     
104     
105     //system("PAUSE");
106     return EXIT_SUCCESS;
107 }

 

转载于:https://www.cnblogs.com/zjushuiping/archive/2012/08/25/2656924.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值