您的位置:首页 > 其它

poj 2570 Fiber Network(传递闭包,floyd+位运算)

2013-06-29 17:29 330 查看
[align=center]FiberNetwork[/align]

TimeLimit:1000MSMemoryLimit:65536K
TotalSubmissions:2703Accepted:1242
Description
SeveralstartupcompanieshavedecidedtobuildabetterInternet,calledthe"FiberNet".Theyhavealreadyinstalledmanynodesthatactasroutersallaroundtheworld.Unfortunately,theystartedtoquarrelabouttheconnecting
lines,andendedupwitheverycompanylayingitsownsetofcablesbetweensomeofthenodes.

Now,serviceproviders,whowanttosenddatafromnodeAtonodeBarecurious,whichcompanyisabletoprovidethenecessaryconnections.Helptheprovidersbyansweringtheirqueries.
Input
Theinputcontainsseveraltestcases.Eachtestcasestartswiththenumberofnodesofthenetworkn.Inputisterminatedbyn=0.Otherwise,1<=n<=200.Nodeshavethenumbers1,...,n.Thenfollowsalistofconnections.Every
connectionstartswithtwonumbersA,B.ThelistofconnectionsisterminatedbyA=B=0.Otherwise,1<=A,B<=n,andtheydenotethestartandtheendpointoftheunidirectionalconnection,respectively.Foreveryconnection,thetwonodesarefollowedbythe
companiesthathaveaconnectionfromnodeAtonodeB.Acompanyisidentifiedbyalower-caseletter.Thesetofcompanieshavingaconnectionisjustawordcomposedoflower-caseletters.

Afterthelistofconnections,eachtestcaseiscompletedbyalistofqueries.EachqueryconsistsoftwonumbersA,B.Thelist(andwithitthetestcase)isterminatedbyA=B=0.Otherwise,1<=A,B<=n,andtheydenotethestartandtheendpointofthequery.
Youmayassumethatnoconnectionandnoquerycontainsidenticalstartandendnodes.
Output
Foreachqueryineverytestcasegeneratealinecontainingtheidentifiersofallthecompanies,thatcanroutedatapackagesontheirownconnectionsfromthestartnodetotheendnodeofthequery.Iftherearenocompanies,
output"-"instead.Outputablanklineaftereachtestcase.


SampleInput
3
12abc
23ad
13b
31de
00
13
21
32
00
2
12z
00
12
21
00
0

SampleOutput
ab
d
-

z
-

题意:给出n个结点,给出若干条连接结点的路径,路径是单向的,路径上有小写字母。例如:当结点1和结点2之间的路径有a、b,结点2和结点3之间的路径有b、c,那么结点1和结点3之间的路径就会出现b(即使结点1和结点3之间没有直接相连的路径或路径上没有b)。每次询问结点a和b之间的路径有哪个小写字母。

思路:传递闭包,用floyd。若n^3的floyd再加上查找相同字母,则显然TLE。有什么方法不用查找相同的字母?用位运算!G[a][b]保存a与b路径上的小写字母的集合,通过并(|)运算即可合并结合


AC代码:

#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<iostream>

usingnamespacestd;

intmain()
{
intn;
inta,b;
intG[205][205];
charcom[30];
while(cin>>n,n)
{
memset(G,0,sizeof(G));
while(cin>>a>>b)
{
if(!a&&!b)break;
cin>>com;
inti=0;
while(com[i])
G[a][b]|=1<<(com[i++]-'a');
}
for(intk=1;k<=n;k++)
for(inti=1;i<=n;i++)
for(intj=1;j<=n;j++)
if(G[i][k]&G[k][j])
G[i][j]|=G[i][k]&G[k][j];

while(cin>>a>>b)
{
if(!a&&!b)break;
if(G[a][b]!=0)
{
inti=0,temp=G[a][b];
while(temp)
{
if(temp&1)
putchar(i+'a');
temp>>=1;
i++;
}
}
else
putchar('-');
putchar('\n');
}
putchar('\n');
}
return0;
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: