Codeforces Round #228 (Div. 1) 388B Fox and Minimal path
链接: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);
}
}
Codeforces Round #228 (Div. 1) 388B Fox and Minimal path的更多相关文章
- Codeforces Round #228 (Div. 1) B. Fox and Minimal path 构造
B. Fox and Minimal path 题目连接: http://codeforces.com/contest/388/problem/B Description Fox Ciel wants ...
- Codeforces Round #228 (Div. 1) C. Fox and Card Game 博弈
C. Fox and Card Game 题目连接: http://codeforces.com/contest/388/problem/C Description Fox Ciel is playi ...
- Codeforces Round #228 (Div. 1) A. Fox and Box Accumulation 贪心
A. Fox and Box Accumulation 题目连接: http://codeforces.com/contest/388/problem/A Description Fox Ciel h ...
- Codeforces Round #228 (Div. 2) C. Fox and Box Accumulation(贪心)
题目:http://codeforces.com/contest/389/problem/C 题意:给n个箱子,给n个箱子所能承受的重量,每个箱子的重量为1: 很简单的贪心,比赛的时候没想出来.... ...
- Codeforces Round #228 (Div. 2) C. Fox and Box Accumulation
C. Fox and Box Accumulation time limit per test 1 second memory limit per test 256 megabytes input s ...
- Codeforces Round #228 (Div. 2) B. Fox and Cross
#include <iostream> #include <string> #include <vector> #include <algorithm> ...
- Codeforces Round #228 (Div. 2) A. Fox and Number Game
#include <iostream> #include <algorithm> #include <vector> #include <numeric> ...
- Codeforces Round #228 (Div. 2)
做codeforces以来题目最水的一次 A题: Fox and Number Game 题意:就是用一堆数字来回减,直到减到最小值为止,再把所有最小值加,求这个值 sol: 简单数论题目,直接求所有 ...
- Codeforces Round #228 (Div. 1) B
B. Fox and Minimal path time limit per test 1 second memory limit per test 256 megabytes input stand ...
随机推荐
- Linux静态ip设置及一些网络设置
网络服务配置文件 /etc/sysconfig/network 网络接口配置文件 /etc/sysconfig/network-scripts/ifcfg-INTERFACE_NAME 修改IP永久生 ...
- 微信小程序--获取form表单初始值提交数据
<form bindsubmit="formSubmit"> <view class="txt"> <view class=&qu ...
- JSP/Servlet Web 学习笔记 DayFive
ServletConfig <只对当前Servlet有效> (1)在Web容器初始化Servlet实例时,都会为这个Servlet准备一个唯一的ServletConfig实例(俗称Serv ...
- Struts2基本程序演示
Struts2启动配置 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns=" ...
- JavaScript jQuery 中定义数组与操作及jquery数组操作 http://www.jb51.net/article/76601.htm
首先给大家介绍javascript jquery中定义数组与操作的相关知识,具体内容如下所示: 1.认识数组 数组就是某类数据的集合,数据类型可以是整型.字符串.甚至是对象Javascript不支持多 ...
- ACM-The Coco-Cola Store
题目: Once upon a time, there is a special coco-cola store. If you return three empty bottles to the s ...
- 《R语言实战》读书笔记--第三章 图形初阶(一)
3.1使用图形 可以使用pdf等函数将图形直接保存在文件中.在运用attach和detach函数的使用中经常出现错误,比如命名重复的问题,所以,应该尽量避免使用这两个函数. plot是一般的画图函数, ...
- Codeforces Round #357 (Div. 2) A
A. A Good Contest time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Pandas之DataFrame——Part 2
''' [课程2.] 时间模块:datetime datetime模块,主要掌握:datetime.date(), datetime.datetime(), datetime.timedelta() ...
- linux常用60条命令 转
Linux必学的60个命令 Linux提供了大量的命令,利用它可以有效地完成大量的工作,如磁盘操作.文件存取.目录操作.进程管理.文件权限设定等.所以,在Linux系统上工作离不开使用系统提供的命 ...