链接:http://codeforces.com/problemset/problem/388/B

【题意】

给出一个整数K,构造出刚好含有K条从1到2的最短路的图。

【分析】

由于是要自己构造图,当然构造的方法很多了,需要考虑简单性和可行性。出于简单考虑,图中从1到2的所有路径都是最短路,为了保证路径长度一样,在构图时就需要分层次,使得每一层的点距离上一层的点的距离都是一个单位。

那么如何使得路径条数刚好为K呢,这里涉及到相邻层次的点的链接方式。比如说每个点和上一层的所有点都有链接,那么这样总的路径数就是每层点的个数乘起来,但是这很难保证乘起来的值刚好是K,于是想到进制数的方法,可以构造出不同底数的链接模型,最后串联起来,起到相加作用,最后一定能凑成K。好需要注意的是点的个数,如果层次分的少了点的个数就多了,超过限制就错了。

不过我在这里不用上面那种拼接方式,而是使用种更巧妙地构图,这种图有许多很好的性质,说不定有其它的用途,我就不用文字说明了,看图:

【代码】

 #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
int cnt[];
long long dp[];
bool g[][];
int k;
int top;
void addedge(int a,int b)
{
g[a][b]=g[b][a]=true;
}
void work(int c)
{
vector<int> fin;
int p=,offset;
while (p<=c)
{
int t=top;
for (;top<t+p;++top)
addedge(top,top-p);
for (offset=top-*p;offset<top-p;++offset)
addedge(offset,top);
++top;
++p;
}
for (offset=top-p;offset<top;++offset)
fin.push_back(offset);
for (int i=fin.size()-;i>=;--i)
{
int t=i-;
if (t<) t=;
if ((<<t)<=k)
{
k-=(<<t);
addedge(,fin[i]);
}
}
--top;
printf("%d\n",top);
for (int i=;i<=top;++i)
{
for (int j=;j<=top;++j)
if (g[i][j]) printf("Y"); else printf("N");
printf("\n");
}
}
int main()
{
while (~scanf("%d",&k))
{
memset(g,,sizeof g);
addedge(,);
addedge(,);
top=;
int c=,tem=k;
while (tem){++c;tem>>=;}
work(c);
}
}

最新文章

  1. js正则表达式大全(2)
  2. TSQL Merge 用法
  3. iOS定位到崩溃代码行数
  4. 资源Createwindow,对应标识符,绑定窗口
  5. paper 19 :机器学习算法(简介)
  6. mysql 加入列,改动列,删除列。
  7. jquery生成二维码
  8. 在SSIS 的 64 位版本中不支持 Excel 连接管理器
  9. OpenDialog获取文件名
  10. java动态缓存技术:WEB缓存应用(转)
  11. 记录一个原因不明的段错误(libxml2 proc activemq的三角恋)
  12. Python 链接MysqlDB
  13. Python的内置函数open()的注意事项
  14. JQuery DOM操作 、属性和CSS样式操作、其他函数
  15. Spring Boot 2.X 如何快速集成单元测试?
  16. phpstorm快捷键大全
  17. 【Codeforces 1109C 】Sasha and a Patient Friend
  18. spring中关于&lt;context:component-scan&gt;的使用说明(转)
  19. 动态设置和访问cxgrid列的Properties
  20. ubuntu16.04, git 的配置

热门文章

  1. unity值得推荐的网址
  2. React01补充
  3. mininet、floodlight在第一次SDN上机作业中出现的一些问题
  4. 用树莓派做3G无线路由器
  5. 【bzoj2238】Mst 最小生成树+树链剖分+线段树
  6. 【bzoj3866】The Romantic Hero dp
  7. Python字符串相关
  8. Citrix NetScaler HA(高可用性)解析
  9. [HNOI2015][bzoj4011] 落叶枫音 [拓扑DP]
  10. margin-top影响父元素定位