POJ 2570 Fiber Network
2014-03-03 22:36
323 查看
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
Sample Output
Source
Ulm Local 2001
传递闭包,不过在TOJ超时了。应该有种更加牛X的做法。
大牛的解法,有状态压缩的思想。
f[u][v]:存放是是二进制的状态。
假如u-v之间有a,g,m。那么可以写成 f[u][v]=1000001000001。
它是由以下二进制数通过 |运算得到的。
0000000000001
0000001000000
1000000000000
传递闭包的时候,只要跟当前要取得的位进行&运算就可以了。如果返回是1表示u-v之间的路有当前位所对应的公司参与建造。
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
Ulm Local 2001
传递闭包,不过在TOJ超时了。应该有种更加牛X的做法。
#include <stdio.h> #include <string.h> #define MAXN 220 int n; int f[MAXN][MAXN][26]; void floyd(){ for(int k=1; k<=n; k++){ for(int i=1; i<=n; i++){ for(int j=1; j<=n; j++){ for(int c=0; c<26; c++){ if( f[i][k][c] && f[k][j][c]) f[i][j][c]=1; } } } } } int main() { while( scanf("%d" ,&n)!=EOF && n){ memset(f,0,sizeof(f)); int u,v; char ch[30]; while( scanf("%d %d",&u ,&v) ){ if(u==0 && v==0)break; scanf("%s",ch); for(int i=0; ch[i]!='\0'; i++){ f[u][v][ch[i]-'a']=1; } } floyd(); while( scanf("%d %d",&u ,&v) ){ if(u==0 && v==0)break; int flag=0; for(int i=0; i<26; i++){ if( f[u][v][i] ){ printf("%c",i+'a'); flag=1; } } if(!flag){ puts("-"); }else{ puts(""); } } printf("\n"); } return 0; }
大牛的解法,有状态压缩的思想。
f[u][v]:存放是是二进制的状态。
假如u-v之间有a,g,m。那么可以写成 f[u][v]=1000001000001。
它是由以下二进制数通过 |运算得到的。
0000000000001
0000001000000
1000000000000
传递闭包的时候,只要跟当前要取得的位进行&运算就可以了。如果返回是1表示u-v之间的路有当前位所对应的公司参与建造。
#include <stdio.h> #include <string.h> #define MAXN 220 int n; int f[MAXN][MAXN]; void floyd(){ for(int k=1; k<=n; k++){ for(int i=1; i<=n; i++){ for(int j=1; j<=n; j++){ f[i][j]=f[i][j]|(f[i][k]&f[k][j]); } } } } int main() { while( scanf("%d",&n)!=EOF && n ){ int u,v; char ch[30]; memset(f ,0 ,sizeof(f)); while( scanf("%d %d" ,&u ,&v)!=EOF ){ if(u==0 && v==0)break; scanf("%s",ch); for(int i=0; ch[i]!='\0'; i++){ f[u][v]=f[u][v]|(1<<(ch[i]-'a')); } } floyd(); while( scanf("%d %d" ,&u ,&v)!=EOF ){ if(u==0 && v==0)break; int flag=0; for(int i=0; i<26; i++){ if(f[u][v]&(1<<i)){ flag=1; printf("%c",i+'a'); } } if(!flag) printf("-"); puts(""); } puts(""); } return 0; }
相关文章推荐
- POJ-2570-Fiber Network
- POJ 2570 Fiber Network
- poj 2570 Fiber Network(floyd)
- POJ --2570 Fiber Network
- poj2570 Fiber Network 最短路floyd
- poj 2570 Fiber Network(最短路+Floyd)
- POJ 2570 Fiber Network
- poj 2570 Fiber Network(最短路+Floyd)
- POJ 2570 Fiber Network(Floyd+位运算)
- POJ 2570 Fiber Network(最短路 二进制处理)
- POJ--2570--Fiber Network【floyd+位运算】
- poj 2570 Fiber Network (Floyd)
- POJ 2570 Fiber Network
- poj 2570 Fiber Network(floyd)
- POJ 2570 Fiber Network(Floyd传递闭包+状态压缩)
- POJ-2570 Fiber Network 状态压缩+floyd
- poj 2570 Fiber Network(floyd)
- POJ 2570 Fiber Network(floyd闭包)
- poj 2570 Fiber Network --floyd求传递闭包加位运算
- POJ 2570 Fiber Network