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 <cstdlib>
#include <list>
#include <stack>

using namespace std;

typedef long long LL;
const int maxn = 205;
int n,val[maxn][maxn];
bool e[maxn][maxn];
char buf[1024];

int getval(char *s) {
    int len = strlen(s),ret = 0;
    for(int i = 0;i < len;i++) {
        ret |= (1 << (s[i] - ‘a‘));
    }
    return ret;
}

void floyd() {
    for(int i = 1;i <= n;i++) {
        for(int j = 1;j <= n;j++) {
            for(int k = 1;k <= n;k++) {
                val[j][k] |= val[j][i] & val[i][k];
            }
        }
    }
}

int main() {
    while(scanf("%d",&n),n) {
        memset(val,0,sizeof(val));
        int a,b;
        while(scanf("%d%d",&a,&b),a) {
            scanf("%s",buf);
            val[a][b] = getval(buf) ;
        }
        floyd();
        while(scanf("%d%d",&a,&b),a) {
            int cnt = 0;
            for(int i = 0;i < 26;i++) {
                if(val[a][b] & (1 << i)) {
                    cnt++;
                    putchar(i + ‘a‘);
                }
            }
            if(cnt == 0) putchar(‘-‘);
            puts("");
        }
        puts("");
    }
    return 0;
}

Poj 2570 Fiber Network Floyd思想处理

时间: 2024-08-25 09:34:43

Poj 2570 Fiber Network Floyd思想处理的相关文章

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 (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

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

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

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

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

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 ac

zoj1967 poj2570 Fiber Network (floyd算法)

虽然不是最短路,但是询问时任意两点之间的信息都要知道才能回答,由此联想到floyd算法,只要都floyd算法的原理理解清楚了就会发现:这道题的思想和求任意两点之间的最短路的一样的,只不过是更新的信息不同而已. 这道题还有一个难点在于状态压缩:如果直接用字符串来表示maps[i][j],那么在floyd中还需要再加一层循环来找maps[i][k]和maps[k][j]有哪些一样的字母,这样时间复杂度太高,实际测试表明会TLE.那么可以用状态压缩的技巧,用二进制表示集合,最多就26位(代表26个字母

POJ 1459 Power Network(网络流 最大流 多起点,多汇点)

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

poj 1459 Power Network, 最大流,多源多汇

点击打开链接 多源多汇最大流,虚拟一个源点s'和一个汇点t',原来的源点.汇点向它们连边. #include<cstdiO> #include<cstring> #include<iostream> #include<algorithm> #include<queue> #include<vector> using namespace std; const int maxn = 500 + 5; const int INF = 100