Fiber Network 2010.3.6
Fiber Network
【关键字】
最短路 floyd
【摘要】
一些顶点可以通过很多种方式到达,求两点间最多有哪些方式可以到达。
【正文】
1、题目描述
Fiber Network
Time Limit:2000MS Memory Limit:65536K
Total Submit:29 Accepted:8
Description
Several startup companies have decided tobuild a better Internet, called the "FiberNet". They have alreadyinstalled many nodes that act as routers all around the world. Unfortunately,they started to quarrel about the connecting lines, and ended up with everycompany laying its own set of cables between some of the nodes.
Now, service providers, who want to senddata from node A to node B are curious, which company is able to provide thenecessary connections. Help the providers by answering their queries.
Input
The input contains several test cases. Eachtest case starts with the number of nodes of the network n. Input is terminatedby n=0. Otherwise, 1<=n<=200. Nodes have the numbers 1, ..., n. Thenfollows 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, andthey denote the start and the endpoint of the unidirectional connection,respectively. For every connection, the two nodes are followed by the companiesthat have a connection from node A to node B. A company is identified by alower-case letter. The set of companies having a connection is just a wordcomposed of lower-case letters.
After the list of connections, each testcase 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. Youmay assume that no connection and no query contains identical start and endnodes.
Output
For each query in every test case generatea line containing the identifiers of all the companies, that can route datapackages on their own connections from the start node to the end node of thequery. If there are no companies, output "-" instead. Output a blankline after each test case.
SampleInput
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
SampleOutput
ab
d
-
z
-
Source
UML 2001
2、算法分析
这道题,虽说是赤果果的floyd,但是还是有一些小小的的东西,如果想不到的话,就一定想不到了,比如,位运算。
其实,用的是26位的二进制数来表示每2个点之间都可以由哪些方式到达。因为是26个字母来表示方式,所以选择了26位的二进制数,方便进行位运算。每次floyd的时候,先按位与f[i][k]和f[k][j]这样如果某种方式在f[i][k]和f[k][j]里都存在的话,就会被保留下来。在和自己f[i][j]或,就把可行的方式都存下来了。就刚好可以求出来~~
输出结果的时候,按照二进制在减回去就可以知道是哪些字母了。
3、源码
#include <stdio.h>
#include <string.h>
#define MAXN 200+10
int num[27]={0,1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432};
int f[MAXN][MAXN],n;
int ans[27];
int max(int x,int y)
{
if (x>y) return x;
else return y;
}
void main()
{
int a,b,ls,i,j,k,tt,lans;
char s[100];
while (scanf("%d",&n),n)
{
memset(f,0,sizeof(f));
while(scanf("%d %d",&a,&b),a,b)
{
scanf("%s",s);
ls=strlen(s);
for(i=0;i<ls;i++)
f[a][b]+=num[s[i]-'a'+1];
}
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
if (k!=i)
for(j=1;j<=n;j++)
if ((k!=j)&&(i!=j))
f[i][j]=(f[i][k]&f[k][j])|(f[i][j]);
while (scanf("%d %d",&a,&b),a,b)
{
tt=f[a][b];
lans=0;
for(i=26;i>=1;i--)
if (tt>=num[i])
{
lans++;
ans[lans]=i;
tt-=num[i];
}
if (lans==0)
printf("-\n");
else
{
for(i=lans;i>=1;i--)
printf("%c",ans[i]+'a'-1);
printf("\n");
}
}
printf("\n");
}
}