Codeforces Round #228 (Div. 1) B. Fox and Minimal path
来源:互联网 发布:下载语文辅导软件 编辑:程序博客网 时间:2024/05/22 14:13
题目链接
http://codeforces.com/contest/388/problem/B
题目大意
给你一个数k
让你构造一个图G, 使1号点到2号点最短路径有k条
输出的图用一个矩阵表示
矩阵有一个两个坐标轴, 把矩阵左上角的坐标记为(1, 1)坐标值分别从左向右、从上到小递增
坐标(x, y)为’Y’表示编号为x的点到编号为y的点有一条路, 该路为单向的, 为’N’则表示x与y之间没有路
题目要求构造的图是无向的, 且没有自环, 即Gii = ‘N’
Gij = Gji
题目为特判, 矩阵大小可以自定义
思路
题目要求节点个数不超过1000, 所以矩阵大小最多1000*1000,
直接构造显然是不行的
这时我们就要把路径压缩, 每增添两个节点就能使路径数量*2
1e9的k最多只需用2*30个节点就够了
所以我们可以把k转化为二进制数
比如k = 23(10111)(2^4 + 2^2 + 2^1 + 2^0)
如上图所示, 这样主路就铺好了, 也就是实现了2^4这条路
对于剩下的我们需要铺一条支路
对于2^2, 我们把9 连到18, 因为3到9有2^2条路, 且18到15与9到15路程一样, 同理, 把6连到20, 3连到22
这样就完成了23条路的构建
代码
#include<bits/stdc++.h>using namespace std;bool mp[1005][1005], num[50];int main(){ int k, top = -1, re1 = 4, re2 = 5, re = 3, len = 0; scanf("%d", &k); while(k) { num[++top] = (k & 1); k >>= 1; } mp[1][3] = 1, mp[3][1] = 1; for(int i=0; i<top; ++i) { mp[re1][re] = 1, mp[re][re1] = 1; mp[re2][re] = 1, mp[re][re2] = 1; re += 3; mp[re1][re] = 1, mp[re][re1] = 1; mp[re2][re] = 1, mp[re][re2] = 1; re1 += 3, re2 += 3, len += 2; } mp[re][2] = 1, mp[2][re] = 1, --len; int tmp = re; for(int i=0; i<len; ++i) { mp[tmp][tmp+1] = 1, mp[tmp+1][tmp] = 1; ++tmp; } tmp = re+1; for(int i=top-1; i>=0; --i) { if(num[i]) { int t = re - (top - i) * 3; mp[t][tmp] = 1, mp[tmp][t] = 1; } tmp += 2; } puts("1000"); for(int i=1; i<=1000; ++i) for(int j=1; j<=1000; ++j) j==1000 ? printf("%c\n", mp[i][j] ? 'Y' : 'N') : printf("%c", mp[i][j] ? 'Y' : 'N'); return 0;}
阅读全文
0 0
- Codeforces Round #228 (Div. 1)-B-Fox and Minimal path
- Codeforces Round #228 (Div. 1) B. Fox and Minimal path
- Codeforces Round #228 (Div. 2) D - Fox and Minimal path
- Codeforces Round #228 Div1 B Fox and Minimal path
- codeforces 388B Fox and Minimal path
- Codeforces B. Fox and Minimal path
- CodeForces 388B Fox and Minimal path
- Codeforces 388B Fox and Minimal path(构造)
- Codeforces 388B Fox and Minimal path 构图
- Codeforces 388B Fox and Minimal path(构造)
- Codeforces Round #228 (Div. 2) B. Fox and Cross
- Codeforces Round #228 (Div. 2)B. Fox and Cross
- Codeforces Round #228 (Div. 2)B. Fox and Cross
- Codeforces Round #228 (Div. 2) B. Fox and Cross
- Codeforces Round #290 (Div. 1)B. Fox And Jumping
- Codeforces Round #290 (Div. 1) B. Fox And Jumping
- Codeforces #228 D2D / D1B:Fox and Minimal path
- Codeforces Round #228 (Div. 1) C. Fox and Card Game
- 2017.8.14 分手是祝愿 失败总结
- 士兵杀敌(三)||NYOJ119
- jQuery添加自定义扩展
- 关于监控文件变化的web工厂
- 4.2矩阵的存储
- Codeforces Round #228 (Div. 1) B. Fox and Minimal path
- CSS选择符间空格与不空格的区别。
- 搭建一个基础的Spring Cloud项目
- 同步复制技术实现步骤
- PB中实现文件的拷贝与删除
- Gas Station问题及解法
- javascript 常用获取时间的方法
- 最新海康摄像机、NVR、流媒体服务器、回放取流RTSP地址规则说明
- springmvc http content-type