poj 2570 Fiber Network (Floyd)

Fiber Network

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3107   Accepted: 1427

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

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
-

公司使用小写字母表示的,最多26个公司,故可以用二进制的位来表示公司。

#include"stdio.h"
#include"string.h"
#include"vector"
#include"queue"
#include"iostream"
#include"algorithm"
using namespace std;
#define N 205
const int inf=(int)1e10;
int g[N][N];
int main()
{
    int i,j,k,n,a,b;
    char str[27];
    while(scanf("%d",&n),n)
    {
        memset(g,0,sizeof(g));
        while(scanf("%d%d",&a,&b),a||b)
        {
            scanf("%s",str);
            for(i=0;str[i]!='\0';i++)      //某一位上为一代表该公司可以连接a,b
                g[a][b]|=1<<(str[i]-'a');
        }
        for(k=1;k<=n;k++)
        {
            for(i=1;i<=n;i++)
            {
                for(j=1;j<=n;j++)
                {
                    g[i][j]|=(g[i][k]&g[k][j]);
                }
            }
        }
        while(scanf("%d%d",&a,&b),a||b)
        {
            if(!g[a][b])
            {
                printf("-\n");
                continue;
            }
            for(char i='a';i<='z';i++)
            {
                if(g[a][b]&(1<<i-'a'))
                    printf("%c",i);
            }
            puts("");
        }
        puts("");
    }
    return 0;
}
时间: 2024-12-28 11:22:21

poj 2570 Fiber Network (Floyd)的相关文章

POJ 2570 Fiber Network(最短路 二进制处理)

题目翻译 一些公司决定搭建一个更快的网络,称为"光纤网".他们已经在全世界建立了许多站点,这 些站点的作用类似于路由器.不幸的是,这些公司在关于站点之间的接线问题上存在争论,这样"光纤网"项目就被迫终止了,留下的是每个公司自己在某些站点之间铺设的线路. 现在,Internet 服务供应商,当想从站点 A传送数据到站点 B,就感到困惑了,到底哪个公司 能够提供必要的连接.请帮助供应商回答他们的查询,查询所有可以提供从站点 A到站定 B的线 路连接的公司. 输入描述:

POJ2570 Fiber Network(Floyd)

d[i][j]表示从i点到j点可以全程提供光纤的公司的集合,集合用26位的二进制压缩. 那么状态转移方程就是dk[i][j]|=dk-1[i][k]&dk-1[k][j]. 1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 int n,d[222][222]; 5 void Floyd(){ 6 for(int k=1; k<=n; ++k){ 7 for(int i=1; i<=n;

Poj 2570 Fiber Network Floyd思想处理

感觉非常有意思,也不难想. f[i][j] |= f[i][k] & f[k][j] #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <

poj 1459 Power Network (dinic)

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 23059   Accepted: 12072 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied

poj 2349 Arctic Network(prime)

Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25165   Accepted: 7751 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication technologies are t

poj 2570 Fiber Network(floyd)

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int mp[30][250][250]; int main() { int n,u,v,i,j,l,k,p,f; char s[1024]; while(~scanf("%d",&n)) { if(n==0) break; memset(mp,0,sizeof(mp)); while(~

POJ 2570 Fiber Network

最短路变形. 题意是说不同的点之间有不同的公司建立了不同连接. 询问 A,B之间如果存在通路,有那些公司. 我用bool  g[][][26] 来表示26个字母.然后Floyd, G++就超时.C++ 就AC了. 然后看别人代码才知道还有位运算--ORZ... 自己的代码:C++ AC.813ms #include<cstdio> #include<cstring> #include<string> #include<queue> #include<a

POJ 2349 Arctic Network (最小生成树)

Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication technologies are to be used in establishing the network: every outpost will have a radio transceiver a

ZOJ 1967 POJ 2570 Fiber Network

枚举起点和公司,每次用DFS跑一遍图,预处理出所有的答案.询问的时候很快就能得到答案. #include<cstdio> #include<cmath> #include<cstring> #include<vector> #include<algorithm> using namespace std; int jz[300][300][30]; int ans[300][300][30]; int flag[300]; vector<in