poj 2570 关于floyd的新思考
2013-04-08 21:31
441 查看
刷图论的艰难历程开始了,这是随便一翻看到的例题,刚开始没有一点思路,但一点点顺着书看下去,发现这道题的背后,是对于floyd的新思考。就如同看到传递闭包所使用的warshall算法的惊讶,这一题着实让我觉得惊艳了一把。不废话,这题是说,铺设网络,网络中的一些节点是有一部分运营商运营的,然后,给你两个节点,让你求能够提供这两个节点间服务的运营商,没有的话输出“-”。首先,把提供这些服务的运营商的代号,转换为对应字典序的位,然后,通过’|‘运算把它并入一个集合之中,并不断求它们连通的交并将之并入集合,可得如下递推式:
map[i][j] |= map[i][k]&map[k][j];
由此,该题得解。
代码如下:
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; }
相关文章推荐
- POJ 3624 Charm Bracelet【裸01背包和关于贪心解 01 背包问题的思考】
- poj 2570 Fiber Network(floyd)
- poj2570 Fiber Network 最短路floyd
- POJ 2570 Fiber Network(Floyd+位运算)
- POJ2570 二进制,位运算,Floyd
- poj 2570 Fiber Network (Floyd)
- poj 2570 Fiber Network(floyd)
- poj 2570 Fiber Network floyd + 位运算
- poj 2570 Fiber Network(传递闭包,floyd+位运算)
- POJ--2570--Fiber Network【floyd+位运算】
- poj 2570 Fiber Network(最短路+Floyd)
- poj 2570 Fiber Network(floyd)
- POJ 2570 Fiber Network(Floyd传递闭包+状态压缩)
- POJ 2570 传递闭包 Floyd
- poj 2570 floyd 二进制+位运算
- poj 2570 Fiber Network(最短路+Floyd)
- poj2570 Floyd
- Poj 2570 Fiber Network Floyd思想处理
- POJ 2570 floyd 二进制
- poj 2570 Fiber Network Floyd