POJ 2570

Fiber Network
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2801   Accepted: 1290

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. POJ 2570_第1张图片

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

Ulm Local 2001


不知道为什么,这题一开始用G++编译器一直超时,C++就可以了。


本题有两种方法,一种是先状态压缩,再floyd求传递闭包(位运算正好对应每一个字母)。复杂度是O(n^3)
另一种是先枚举字母,只留下标有这些字母的边,然后枚举起点,看就是否可以到达其他点,这样预处理出所有情况,复杂度是O(26*n^2)


方法一:

#include
#include
#include
#include
#include
#include
#include
#define maxn 209
#define maxm 40009
using namespace std;
int first[maxn];
int next[maxm],u[maxm],v[maxm];
int in[28][maxm];
bool vis[maxn];
int n,m,tot;
int ans[maxn][maxn][30];
int del;
char s[maxn];
void add(int x,int y)
{
    u[tot]=x;v[tot]=y;
    next[tot]=first[x];
    first[x]=tot++;
}
void dfs(int x)
{
    vis[x]=1;
    for(int e=first[x];e!=-1;e=next[e])
    {
        if(!vis[v[e]]&&in[del][e])
            dfs(v[e]);
    }
}
int main()
{
    while(scanf("%d",&n),n)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        memset(in,0,sizeof(in));
        memset(first,-1,sizeof(first));tot=0;
        while(x||y)
        {
            scanf("%s",s);
            for(int i=0;s[i];i++)
                in[s[i]-'a'][tot]=1;
            add(x,y);
            scanf("%d%d",&x,&y);
        }
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
            ans[i][j][0]=0;
        for(int i=0;i<26;i++)
        {
            del=i;
            for(int j=1;j<=n;j++)
            {
                memset(vis,0,sizeof(vis));
                dfs(j);
                for(int k=1;k<=n;k++)
                {
                    if(k!=j&&vis[k])
                    {
                        ans[j][k][0]++;
                        ans[j][k][ans[j][k][0]]=i;
                    }
                }
            }
        }

        scanf("%d%d",&x,&y);
        while(x||y)
        {
            if(!ans[x][y][0])
                printf("-\n");
            else
            {
                for(int i=1;i<=ans[x][y][0];i++)
                    printf("%c",ans[x][y][i]+'a');
                printf("\n");
            }
            scanf("%d%d",&x,&y);
        }
        printf("\n");
    }
}

方法二:

#include
#include
int d[201][201];
char s[30];
int main()
{
    int n;int x,y;
    while(scanf("%d",&n),n)
    {

        memset(d,0,sizeof(d));
        while(scanf("%d%d",&x,&y),x+y)
        {
            scanf("%s",s);
            int k=0;
            for(int i=0;s[i];i++)
                k=k|(1<<(s[i]-'a'));
            d[x][y]=k;
        }
        for(int k=1;k<=n;k++)
            for(int i=1;i<=n;i++)
                for(int j=1;j<=n;j++)
            d[i][j]=d[i][j]|(d[i][k]&d[k][j]);
        while(scanf("%d%d",&x,&y),x+y)
        {
            if(!d[x][y])
                printf("-");
            else
            {
                for(int i=0;i<26;i++)
                    if(d[x][y]&(1<


你可能感兴趣的:(图论)