传递闭包+二进制位运算+floyd(poj2570)

 
 
Fiber Network
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3125 Accepted: 1436

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
-
题意:给出n个点,然后给出边a,b ,str代表str中的这些字母可以保证a到b的联通,然后有多组询问u和v,问u到v可以由那些字母保证连通性,若没有输出-
分析:一共26个字母可以使用位运算,g[a][b]代表那些可以联通,用传递闭包即可
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"queue"
#include"algorithm"
#include"string.h"
#include"string"
#include"math.h"
#include"vector"
#include"stack"
#include"map"
#define eps 1e-8
#define inf 0x3f3f3f3f
#define M 250
using namespace std;
int g[M][M],vis[M];
char str[M];
int main()
{
    int n,i,a,b,c,j,k,kk=0;
    while(scanf("%d",&n),n)
    {
        memset(g,0,sizeof(g));
        memset(vis,0,sizeof(vis));
        while(scanf("%d%d",&a,&b),a||b)
        {
            scanf("%s",str);
            for(i=0;str[i]!='\0';i++)
            {
                c=str[i]-'a';
                g[a][b]|=(1<<c);
            }
        }
        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]);
                }
            }
        }
        if(kk)
            printf("\n");
        kk++;
        while(scanf("%d%d",&a,&b),a||b)
        {
            int flag=0;
            for(i=0;i<26;i++)
            {
                if(g[a][b]&(1<<i))
                {
                    printf("%c",i+'a');
                    flag++;
                }
            }
            if(flag)
            printf("\n");
            else
                printf("-\n");
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
传递闭包运算是一个经典的离散数学概念,可以通过 C 语言的代码进行实现。以下是一个简单的离散数学传递闭包运算的 C 语言实现。 ```c #include <stdio.h> #include <stdlib.h> #define MAX_SIZE 50 int main() { int n; printf("请输入矩阵的大小:"); scanf("%d", &n); int matrix[MAX_SIZE][MAX_SIZE]; int transitive_closure[MAX_SIZE][MAX_SIZE]; printf("请依次输入矩阵元素:\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf("%d", &matrix[i][j]); } } // 初始化传递闭包矩阵 for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { transitive_closure[i][j] = matrix[i][j]; } } // 计算传递闭包矩阵 for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (transitive_closure[i][k] && transitive_closure[k][j]) { transitive_closure[i][j] = 1; } } } } printf("传递闭包矩阵为:\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { printf("%d ", transitive_closure[i][j]); } printf("\n"); } return 0; } ``` 代码中,首先要求用户输入矩阵的大小和元素,然后初始化传递闭包矩阵,接着进行传递闭包运算,最后输出传递闭包矩阵。在传递闭包运算中,我们通过三重循环遍历每个元素,如果存在从 i 到 k 和从 k 到 j 的路径,那么就可以从 i 到 j,因此将 transitive_closure[i][j] 赋值为 1。 需要注意的是,这里的矩阵的行列数必须是相同的。如果矩阵不是方阵,则传递闭包运算依然可以进行,但是需要对矩阵进行扩展,使得行列数相同。另外,在实际使用中,我们可能需要对输入的矩阵进行合法性检查,以确保输入的矩阵满足传递闭包运算的要求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值