zoj1967 poj2570 Fiber Network (floyd算法)
虽然不是最短路,但是询问时任意两点之间的信息都要知道才能回答,由此联想到floyd算法,只要都floyd算法的原理理解清楚了就会发现:这道题的思想和求任意两点之间的最短路的一样的,只不过是更新的信息不同而已。
这道题还有一个难点在于状态压缩:如果直接用字符串来表示maps[i][j],那么在floyd中还需要再加一层循环来找maps[i][k]和maps[k][j]有哪些一样的字母,这样时间复杂度太高,实际测试表明会TLE。那么可以用状态压缩的技巧,用二进制表示集合,最多就26位(代表26个字母),那么在floyd算法实现过程中只需要一些位运算就解决问题了。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<cctype>
#include<sstream>
using namespace std;
#define pii pair<int,int>
#define LL long long int
const int eps=1e-;
const int INF=;
const int maxn=+;
int n,a,b;
int maps[maxn][maxn];
void floyd();
int main()
{
//freopen("in8.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d",&n)==&&n)
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
maps[i][j]=;
}
}
while(scanf("%d%d",&a,&b)==)
{
if(a==&&b==) break;
char str[]="";
scanf("%s",str);
int len=strlen(str);
for(int i=;i<len;i++)
{
int t=str[i]-'a';
maps[a][b]|=(<<t);
}
}
floyd();
while(scanf("%d%d",&a,&b)==)
{
if(a==&&b==)
{
printf("\n");break;
}
if(maps[a][b]==)
{
printf("-\n");
}
else
{
for(int i=;i<;i++)
{
if(maps[a][b]&(<<i))
{
printf("%c",'a'+i);
}
}
printf("\n");
}
}
}
//fclose(stdin);
//fclose(stdout);
return ;
}
void floyd()
{
for(int k=;k<=n;k++)
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if((maps[i][k]!=)&&(maps[k][j]!=))
{
maps[i][j]|=maps[i][k]&maps[k][j];
}
}
}
}
}
zoj1967 poj2570 Fiber Network (floyd算法)的更多相关文章
- POJ2570 Fiber Network(Floyd)
d[i][j]表示从i点到j点可以全程提供光纤的公司的集合,集合用26位的二进制压缩. 那么状态转移方程就是dk[i][j]|=dk-1[i][k]&dk-1[k][j]. #include& ...
- poj 2570 Fiber Network(floyd)
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int ...
- POJ 2579 Fiber Network(状态压缩+Floyd)
Fiber Network Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3328 Accepted: 1532 Des ...
- Fiber Network ZOJ 1967(Floyd+二进制状态压缩)
Description Several startup companies have decided to build a better Internet, called the "Fibe ...
- Restoring Road Network(Floyd算法的推广)
个人心得:看懂题目花费了不少时间,后面实现确实时间有点仓促了,只是简单的做出了判断是否为真假的情况, 后面看了题解发现其实在判断时候其实能够一起解决的,算了,基础比较差还是慢慢的来吧. 题意概述: 就 ...
- 图论(floyd算法):NOI2007 社交网络
[NOI2007] 社交网络 ★★ 输入文件:network1.in 输出文件:network1.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] 在社交网络( ...
- POJ 1502 MPI Maelstrom(模板题——Floyd算法)
题目: BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distri ...
- POJ 2570 Fiber Network(最短路 二进制处理)
题目翻译 一些公司决定搭建一个更快的网络.称为"光纤网". 他们已经在全世界建立了很多网站.这 些网站的作用类似于路由器.不幸的是,这些公司在关于网站之间的接线问题上存在争论,这样 ...
- Floyd算法C++实现与模板题应用
简介 Floyd算法算是最简单的算法,没有之一. 其状态转移方程如下map[i , j] =min{ map[i , k] + map[k , j] , map[i , j] }: map[i , j ...
随机推荐
- TCP/IP/UDP 协议
互连网早期的时候,主机间的互连使用的是NCP协议.这种协议本身有很多缺陷,如:不能互连不同的主机,不能互连不同的操作系统,没有纠错功能.为了改善这种缺点,大牛弄出了TCP/IP协议.现在几乎所有的操作 ...
- 剑指offer 面试41题
面试41题: 题目:数据流中的中位数 题:如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值.如果从数据流中读出偶数个数值,那么中位数就是所有数值 ...
- C#中的foreach和yield
1. foreach C#编译器会把foreach语句转换为IEnumerable接口的方法和属性. foreach (Person p in persons) { Console.WriteLine ...
- EXCEL 从网页复制的内容 单/多选框 在EXCEL删不掉 及 2007添加开发工具选项卡
从网页复制到Excel中的单选.多选框等,有时候删除时怎么都删不掉,很是恶心.这时候需要使用“开发工具”来删除.它是设计模式下的一种组件或者说控件. Excel 2007 的可以用下图方式按delet ...
- PHP基本语法,类基本函数
C#中函数四要素返回类型,函数名,参数列表,函数体pulic void show()php函数定义1.最简单的定义function show(){echo "hello"}show ...
- hadoop linux 杂记
切换到root su 修改sudo sudo + 命令 --> root权限 + 命令 su root vim /etc/sudoers ...
- 【leetcode刷题笔记】Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- 虚构 css 父级选择器
能 CSS 解决的绝不用 JS,这句话又一次故作装逼地说出来还是挺爽的... 比如下拉列表,能用 CSS 的 :focus 就不用 JS 的 .on("focus blur") 能 ...
- Linux的Cache Memory(缓存内存)机制
转:https://blog.csdn.net/kaikai_sk/article/details/79177036 PS:为什么Linux系统没运行多少程序,显示的可用内存这么少?其实Linux与W ...
- 函数---迭代器&生成器&列表解析&三元表达式
可迭代对象:obj.__iter__ 迭代器:iter1=obj.__iter() 1iter1.__next__ 2iter2.__next__ 迭代器: 优点:不依赖索引 ...