POJ 2579 Fiber Network(状态压缩+Floyd)

Fiber Network

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3328   Accepted: 1532

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
-

Source

Ulm Local 2001

题意:从起点到终点找到所有边权上的相同的地方。

思路: 首先,由于边权是字母又是要求边权上的相同的地方,想到状态压缩。且具有传递性,就用Floyd.

收获:a[u][v] |= 1<<(s[i] - ‘a‘);中的|=是存入状态。a[i][k] & a[k][j] 中的&是来判断有相同的地方。

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std;

const int INF = 0x3f3f3f3f;
const double eps = 1e-10;
const double PI = acos(-1.0);

const int maxn = 500;
int a[maxn][maxn];
int main()
{
    int n;
    while(~scanf("%d", &n)&&n)
    {
        int u, v;
        memset(a, 0, sizeof(a));
        while(scanf("%d%d", &u, &v)&& (u+v))
        {
            char s[100];
            scanf("%s", s);
            for(int i = 0; i < strlen(s); i++)
                a[u][v] |= 1<<(s[i] - ‘a‘);
        }

        for(int k = 1; k <= n; k++)
            for(int i = 1; i <= n; i++)
                for(int j = 1; j <= n; j++)
                a[i][j] |= a[i][k] & a[k][j];

        while(scanf("%d%d", &u, &v) && (u+v))
        {
                for(int i = ‘a‘; i <= ‘z‘; i++)
                     if(a[u][v] & (1 << (i-‘a‘)))
                     printf("%c",i);
                if(!a[u][v])
                    printf("%c",‘-‘);
                puts("");
        }
        puts("");
    }
    return 0;
}
时间: 2024-08-03 16:54:30

POJ 2579 Fiber Network(状态压缩+Floyd)的相关文章

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 3254 Corn Fields 状态压缩DP (C++/Java)

http://poj.org/problem?id=3254 题目大意: 一个农民有n行m列的地方,每个格子用1代表可以种草地,而0不可以.放牛只能在有草地的,但是相邻的草地不能同时放牛, 问总共有多少种方法. 思路: 状态压缩的DP. 可以用二进制数字来表示放牧情况并判断该状态是否满足条件. 这题的限制条件有两个: 1.草地限制. 2.相邻限制. 对于草地限制,因为输入的时候1是可以种草地的. 以"11110"草地分析,就只有最后一个是不可以种草的.取反后得00001  .(为啥取反

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 ac

HDU 5418——Victor and World——————【状态压缩+floyd】

Victor and World Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 891    Accepted Submission(s): 399 Problem Description After trying hard for many years, Victor has finally received a pilot li

POJ 3254 Corn Fields 状态压缩DP

题目链接:http://poj.org/problem?id=3254 思路:状态压缩DP,状态方程为dp[i][j] += (dp[i-1][k]) code: #include <stdio.h> #include <string.h> #define N 500 const int MOD = 100000000; int dp[15][N],ant[N],n,m,k,map[15]; bool ok(int x) { if(x&(x<<1))return

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

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

POJ 3691 (AC自动机+状态压缩DP)

题目链接:  http://poj.org/problem?id=3691 题目大意:给定N的致病DNA片段以及一个最终DNA片段.问最终DNA片段最少修改多少个字符,使得不包含任一致病DNA. 解题思路: 首先说一下AC自动机在本题中的作用. ①字典树部分:负责判断当前0~i个字符组成的串是否包含致病DNA,这部分靠字典树上的cnt标记完成. ②匹配部分:主要依赖于匹配和失配转移关系的计算,这部分非常重要,用来构建不同字符间状态压缩的转移关系(代替反人类的位运算). 这也是必须使用AC自动机而

POJ 3254. Corn Fields 状态压缩DP (入门级)

Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Description Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yumm

[POJ 2923] Relocation (动态规划 状态压缩)

题目链接:http://poj.org/problem?id=2923 题目的大概意思是,有两辆车a和b,a车的最大承重为A,b车的最大承重为B.有n个家具需要从一个地方搬运到另一个地方,两辆车同时开,问最少需要搬运几次? 我先想的是我由A车开始搬,搬运能装的最大的家具总重,然后状态压缩记录下搬运了哪些,然后再由B车搬运,状态压缩记录搬运了哪些.以此类推,直到状态满了. 以上方法TLE 然后,实在想不出来了,看了题解:http://blog.csdn.net/woshi250hua/articl