POJ 2570 Fiber Network

Fiber Network
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2660   Accepted: 1221

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的路径.每个公司由一个小写字母表示。

思路:因为只有26个字母 所以可能用二进制去表示每个公司 1表示该路径上有该公司 比如说 1-->3 这条路径上有 abc这三个公司 则mat[1][3]  二进制表示为00..0111; 然后就是用floyd传递闭包。
 
#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

const int N=250;

int g[N][N];

void Print(int num){
    char res[30];
    int k=0,cnt=0;
    while(num){
        if(num&1)
            res[cnt++]='a'+k;
        k++;
        num>>=1;
    }
    res[cnt]='\0';
    printf("%s\n",res);
}

int main(){

    //freopen("input.txt","r",stdin);

    char str[30];
    int n;
    while(~scanf("%d",&n) && n){
        memset(g,0,sizeof(g));
        int u,v;
        while(scanf("%d%d",&u,&v)){
            if(u==0 && v==0)
                break;
            scanf("%s",str);
            for(int i=0;str[i]!='\0';i++)
                g[u][v]=g[u][v]|(1<<(str[i]-'a'));  //转换成二进制表示
        }

        int i,j,k;
        for(k=1;k<=n;k++)
            for(i=1;i<=n;i++)
                for(j=1;j<=n;j++)
                    if(g[i][k]&g[k][j])
                        g[i][j]=g[i][j] | (g[i][k] & g[k][j]);

        while(scanf("%d%d",&u,&v)){
            if(u==0 && v==0)
                break;
            if(g[u][v])
                Print(g[u][v]);
            else
                printf("-\n");
        }
        printf("\n");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/jackge/p/3053285.html