poj 2570 关于floyd的新思考

  刷图论的艰难历程开始了,这是随便一翻看到的例题,刚开始没有一点思路,但一点点顺着书看下去,发现这道题的背后,是对于floyd的新思考。就如同看到传递闭包所使用的warshall算法的惊讶,这一题着实让我觉得惊艳了一把。不废话,这题是说,铺设网络,网络中的一些节点是有一部分运营商运营的,然后,给你两个节点,让你求能够提供这两个节点间服务的运营商,没有的话输出“-”。首先,把提供这些服务的运营商的代号,转换为对应字典序的位,然后,通过’|‘运算把它并入一个集合之中,并不断求它们连通的交并将之并入集合,可得如下递推式:

      map[i][j] |= map[i][k]&map[k][j];

由此,该题得解。

代码如下:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int MAXN = 210;
char str[30];
int map[MAXN][MAXN];


int main()
{
    int n;
    int i,j,k;
    int f = 0;
    while(scanf("%d",&n) && n)
    {
        int x,y;
        if(f) putchar('\n');
        if(f == 0) f = 1;
        memset(map,0,sizeof(map));
        while(scanf("%d%d",&x,&y) && x && y)
        {
            scanf("%s",str);
            for(i = 0; str[i]; i ++)
                map[x][y] |= 1 << (str[i] - 'a');
        }
        
        for(k = 1; k <= n ; k ++)
            for(i = 1; i <= n; i ++)
                for(j = 1; j <= n; j ++)
                    map[i][j] |= map[i][k] & map[k][j];
            
            
            while(scanf("%d%d",&x,&y) && x && y)
            {
                char ch;
                int flag = 0;
                for(ch = 'a';ch <= 'z'; ch ++)
                {
                    if(map[x][y]&(1 << (ch - 'a')))
                    {
                        flag = 1;
                        putchar(ch);
                    }
                }
                
                if(flag) putchar('\n');
                else printf("-\n");
            }
    }
    
    return 0;
}

 

posted @ 2013-04-08 21:31  寒风剑海  阅读(138)  评论(0)    收藏  举报