Codeforces Round #228 (Div. 1)-B-Fox and Minimal path
来源:互联网 发布:vs2010 c语言 编辑:程序博客网 时间:2024/05/18 15:25
做法:
输入一个数n(例如n=11)。
把n转化成二进制为(1011)。
那么11构造出来的图为:
从点1到点2有三条路,分别为路1,2,3。
三条路分别是1011中的三个1,即把11分离成8+2+1。
1路上分离出来8条路,8=2*2*2;
2路上分离出来2条路,2=2;
3路上分离出来1条路。
把所有的点画出来即为:
红色的点代表图中的点,黑色的线是边。
这样,点1到点2之间就有8+2+1条最短路了。
总体思路:
把一个输入的数n转化成2进制,在2进制中有多少个1,就分离出来多少条路,每条路存在2^x条最短路。
注意:
比赛的时候由于怕二进制的时候点会不够,就转化成3进制做的。
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<iostream>#include<algorithm>#include<vector>using namespace std;#define LL __int64#define maxn 50000001int maps[1001][1001];int biao[101];int lens;int nums;int st;void chu(int x){ lens=0; nums=0; while(x) { if(x%3) { biao[lens]=x%3; nums++; } else biao[lens]=0; lens++; x=x/3; }}void dos(){ int i,j; for(i=1;i<=st;i++) { for(j=1;j<=st;j++) { if(maps[i][j])maps[j][i]=1; } }}int main(){ int n,i,j,k; int ans; while(~scanf("%d",&n)) { chu(n);//把n转化成3进制 st=3; vector<int>vec; for(i=0;i<lens;i++) { if(biao[i])//如果n对应的3进制当前位不为0,那么分离出来一条路 { int tt; tt=0; vec.clear(); if(biao[i]==1) { maps[1][st]=1; vec.push_back(st); st++; } else { maps[1][st]=1; vec.push_back(st); st++; maps[1][st]=1; vec.push_back(st); st++; } for(j=0;j<=i;j++) { for(k=0;k<vec.size();k++) { maps[vec[k]][st]=1; maps[vec[k]][st+1]=1; maps[vec[k]][st+2]=1; } vec.clear(); vec.push_back(st); vec.push_back(st+1); vec.push_back(st+2); st=st+3; } st=st-3; for(k=1;k<=1000;k++) { maps[k][st+1]=0; maps[k][st+2]=0; } for(;j<lens;j++) { maps[st][st+1]=1; st++; } maps[st][2]=1; st++; } } st--; dos(); cout<<st<<endl; for(i=1;i<=st;i++) { for(j=1;j<=st;j++) { if(maps[i][j])cout<<"Y"; else cout<<"N"; } cout<<endl; } } 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
- debian 7 wheezy 163 更新源
- 总结在学校的几年(2013 毕业)
- 题目1136:Number Steps
- 《基于Android微博整合客户端的设计与实现》毕业设计论文开题报告
- 游戏编程该读哪些书?最新已加入(本帖持续更新中...)
- Codeforces Round #228 (Div. 1)-B-Fox and Minimal path
- HTML5画布路径教程
- freebsd资料
- 药品查询接口,提供开放的药品API
- HTML5画布线加入教程
- 在CSS中通过@font-face属性来实现网页中嵌入特殊字体
- FreeBSD基本及重要指令集
- 1047. Student List for Course (25)
- iOS7 Networking with NSURLSession: Part 1