POJ-2570 Fiber Network

12 篇文章 0 订阅
Fiber Network
Time Limit: 1000MS Memory Limit: 65536K
   

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
-

————————————————————集训15.3的分割线————————————————————
思路:蛮好的一道水题。
尽管不是求最短路,但是要用到Floyd的更新思想。用26位2进制数来表示26个公司,在建图的时候,读入起点和终点以及拥有线路的公司,这些公司按位或在一起。更新的时候,如果某个公司从i可达k,从k可达j,则该位是1,否则为0。(按位与)最后查询,枚举26个公司。
代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define INF 0x3f3f3f3f
using namespace std;
/****************************************/
const int N = 205;
int n, mat[N][N];


void Floyd()
{
	for(int k = 1; k <=n; k++) {
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= n; j++) if(k != i&&k != j) { 
				mat[i][j] |= mat[i][k] & mat[k][j];
			}
		}
	}
}

int main()
{	
#ifdef J_Sure
//	freopen("000.in", "r", stdin);
//	freopen(".out", "w", stdout);
#endif
	while(~scanf("%d", &n)&&n) {
		int u, v;
		char com[30];
		memset(mat, 0, sizeof(mat));
		while(scanf("%d%d", &u, &v)) {
			if(!u && !v) break;
			scanf("%s", com);
			int len = strlen(com);
			for(int i = 0; i < len; i++) {
				mat[u][v] |= 1 << (com[i]-'a');
			}
		}
		Floyd();
		int a, b;
		while(scanf("%d%d", &a, &b)) {
			if(!a && !b) break;
			for(int i = 0; i < 26; i++) {
				if(mat[a][b] & (1<<i)) putchar(i+'a');
			}
			if(!mat[a][b]) putchar('-');
			puts("");
		}
		puts("");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值