Fiber Network
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3328 | Accepted: 1532 |
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.
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.
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
题意:从起点到终点找到所有边权上的相同的地方。
思路: 首先,由于边权是字母又是要求边权上的相同的地方,想到状态压缩。且具有传递性,就用Floyd.
收获:a[u][v] |= 1<<(s[i] - 'a');中的|=是存入状态。a[i][k] & a[k][j] 中的&是来判断有相同的地方。
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF = 0x3f3f3f3f;
const double eps = 1e-;
const double PI = acos(-1.0); const int maxn = ;
int a[maxn][maxn];
int main()
{
int n;
while(~scanf("%d", &n)&&n)
{
int u, v;
memset(a, , sizeof(a));
while(scanf("%d%d", &u, &v)&& (u+v))
{
char s[];
scanf("%s", s);
for(int i = ; i < strlen(s); i++)
a[u][v] |= <<(s[i] - 'a');
} for(int k = ; k <= n; k++)
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
a[i][j] |= a[i][k] & a[k][j]; while(scanf("%d%d", &u, &v) && (u+v))
{
for(int i = 'a'; i <= 'z'; i++)
if(a[u][v] & ( << (i-'a')))
printf("%c",i);
if(!a[u][v])
printf("%c",'-');
puts("");
}
puts("");
}
return ;
}