poj 2570 Fiber Network(传递闭包,floyd+位运算)
2013-06-29 17:29
330 查看
[align=center]FiberNetwork[/align]
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
SampleOutput
TimeLimit:1000MS | MemoryLimit:65536K | |
TotalSubmissions:2703 | Accepted:1242 |
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;
}
相关文章推荐
- poj 2570 Fiber Network --floyd求传递闭包加位运算
- POJ 2570 Fiber Network(Floyd+位运算)
- POJ--2570--Fiber Network【floyd+位运算】
- POJ 2570 传递闭包 Floyd
- POJ 2570 Fiber Network(Floyd传递闭包+状态压缩)
- poj 2570 Fiber Network(floyd)
- poj 2570 floyd 二进制+位运算
- poj 2570 Fiber Network(floyd)
- poj2570 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 (传递闭包)
- POJ2570 二进制,位运算,Floyd
- poj 2570 Fiber Network 一直每条边可以通过某些公司过去,问从a到b可以通过那几个公司(同一个)过去
- Cow Contest POJ - 3660(传递闭包,floyd)
- POJ 3660 Cow Contest (Floyd 传递闭包)
- POJ - 3660 - Cow Contest (floyd求传递闭包)
- POJ2594(二分匹配+Floyd求传递闭包)