poj 2570 Fiber Network (两种解法 1.floyd+位操作 2.传递闭包)

来源:互联网 发布:仿卷皮淘宝客源码 编辑:程序博客网 时间:2024/04/29 10:59
Fiber Network
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 2741 Accepted: 1262

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

31 2 abc2 3 ad1 3 b3 1 de0 01 32 13 20 021 2 z0 01 22 10 00

Sample Output

abd-z-

Source

Ulm Local 2001


思路1:

floyd()解法,要有一点状态压缩的思想。用int的前26位分别表示公司a~z。


代码:

#include <iostream>#include <cstdio>#include <cstring>#define maxn 205using namespace std;int n,m,ans;int city[maxn][maxn];char s[maxn];void floyd(){    int i,j,k;    for(k=1;k<=n;k++)    {        for(i=1;i<=n;i++)        {            for(j=1;j<=n;j++)            {                city[i][j]=city[i][j]|(city[i][k]&city[k][j]);            }        }    }}int main(){    int i,j,t,l,r,flag;    while(scanf("%d",&n),n)    {        memset(city,0,sizeof(city));        while(scanf("%d%d",&l,&r),l||r)        {            scanf("%s",s);            for(i=0; s[i]!='\0'; i++)            {                t=s[i]-'a';                city[l][r]=city[l][r]|(1<<t);            }        }        floyd();        while(scanf("%d%d",&l,&r),l||r)        {            flag=0;            for(i=0; i<26; i++)            {                if(city[l][r]&(1<<i))                {                    flag=1;                    printf("%c",'a'+i);                }            }            if(!flag) printf("-");            printf("\n");        }        printf("\n");    }    return 0;}

思路2:

传递闭包思想,用26次floyd()。

ps:这样不会TLE的,因为传递闭包中的floyd()很多情况下都只进行两种循环,不过时间比第一种方法慢一些,内存更不用说了。


代码:

#include <iostream>#include <cstdio>#include <cstring>#define maxn 205using namespace std;const int INF=0x3f3f3f3f;int n,m,ans;int city[26][maxn][maxn];bool vis[30];char s[maxn];void init(){    int i,j,k;    memset(vis,0,sizeof(vis));    memset(city,0x3f,sizeof(city));    for(k=0; k<26; k++)    {        for(i=1; i<=n; i++)        {            city[k][i][i]=0;        }    }}void floyd(int x){    int i,j,k;    for(i=1; i<=n; i++)    {        for(j=1; j<=n; j++)        {            if(city[x][j][i]==1)     // 如果j,i 连通  如果这里变为city[i][j] 你会发现会WA            {                for(k=1; k<=n; k++)                {                    if(city[x][i][k]==1) city[x][j][k]=1;   // 如果i,k连通 则j,k连通                }            }        }    }}int main(){    int i,j,l,r,flag;    while(scanf("%d",&n),n)    {        init();        while(scanf("%d%d",&l,&r),l||r)        {            scanf("%s",s);            for(i=0; s[i]!='\0'; i++)            {                vis[s[i]-'a']=1;                city[s[i]-'a'][l][r]=1;            }        }        for(i=0; i<26; i++)        {            if(vis[i]) floyd(i);        }        while(scanf("%d%d",&l,&r),l||r)        {            flag=0;            for(i=0; i<26; i++)            {                if(vis[i]&&city[i][l][r]==1)                {                    flag=1;                    printf("%c",'a'+i);                }            }            if(!flag) printf("-");            printf("\n");        }        printf("\n");    }    return 0;}