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 ...
随机推荐
- Code Blocks中配置OpenGL方法
关于在Code Blocks中配置OpenGL的方法,在网上一直没有找到实用的方法,后来在马龙师兄的帮助下终于配置成功了,现把配置过程记录如下. (1)下载codeblocks,最好是带mingw的版 ...
- 2 25urllib.py
""" urllib.request.urlopen(url,data,timeout) """ # from urllib.request ...
- 软工实践Alpha冲刺(10/10)
队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 完成所有界面的链接,整理与测试 展示GitHub当日代码/ ...
- (总结)Linux下查看Nginx Apache MySQL的并发连接数和连接状态
1.查看Web服务器(Nginx Apache)的并发请求数及其TCP连接状态:netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a ...
- Scala 基础(6)—— 控制结构
1. Scala 的内建控制结构 Scala 有几个内建的控制结构,包括: if 表达式 while 循环和 do-while 循环 for 表达式 try 表达式 match 表达式 Scala 的 ...
- poj 1743 Musical Theme (后缀数组+二分法)
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16162 Accepted: 5577 De ...
- CORS跨域cookie传递
服务端 Access-Control-Allow-Credentials:true Access-Control-Allow-Methods:* Access-Control-Allow-Origin ...
- linu触摸屏幕
一..前提知识 1.Linux输入子系统(Input Subsystem): 在Linux中,输入子系统是由输入子系统设备驱动层.输入子系统核心层(Input Core)和输入子系统事件处理层(Eve ...
- Hibernate中多种方式解除延迟加载
问题引发:因为dao使用load(),默认延迟加载的,当在biz关闭session之后,UI层无法获取对象的非id属性值 解决方案: 1.变成get,即时加载 2.用Hibernate.isIniti ...
- 【IDEA】IDEA下maven项目无法提示和使用EL表达式的解决办法
今天在IDEA创建web项目之后发现无法使用EL和JSTL, 一.如果JSP中无法自动提示EL表达式,比如${pageContext.request.contextPath},可在pom.xml的&l ...