CF1005F Berland and the Shortest Paths 最短路树计数
问题描述
题解
由题面显然可得,所求即最短路树。
所以跑出最短路树,计数,输出方案即可。
\(\mathrm{Code}\)
#include<bits/stdc++.h>
using namespace std;
template <typename Tp>
void read(Tp &x){
x=0;char ch=1;int fh;
while(ch!='-'&&(ch>'9'||ch<'0')) ch=getchar();
if(ch=='-') ch=getchar(),fh=-1;
else fh=1;
while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
x*=fh;
}
const int maxn=200007;
const int maxm=400007;
const int INF=0x3f3f3f3f;
int n,m,k;
int Head[maxn],to[maxm],Next[maxm],tot,w[maxm];
void add(int x,int y){
to[++tot]=y,Next[tot]=Head[x],Head[x]=tot,w[tot]=1;
}
int dis[maxn];
bool vis[maxn];
priority_queue< pair<int,int> > q;
#define pii(x,y) make_pair(x,y)
void dijkstra(){
memset(dis,0x3f,sizeof(dis));
q.push(pii(0,1));dis[1]=0;
while(q.size()){
int x=q.top().second;q.pop();
if(vis[x]) continue;vis[x]=1;
for(int i=Head[x];i;i=Next[i]){
int y=to[i];
if(dis[y]>dis[x]+w[i]){
dis[y]=dis[x]+w[i];
q.push(pii(-dis[y],y));
}
}
}
}
int ans=1;
vector<int>g[maxn];
int val[maxn];
void build(){
for(int x=1;x<=n;x++){
for(int i=Head[x];i;i=Next[i]){
int y=to[i];
if(dis[y]==dis[x]+w[i]){
val[y]++;
g[y].push_back((i+1)>>1);
}
}
}
for(int i=1;i<=n;i++){
if(val[i]) ans=ans*val[i];
if(ans>=k){
ans=k;return;
}
}
}
bool v[maxm];
int md;
void dfs(int x){
if(x==n+1){
for(int i=1;i<=tot;i+=2) printf("%d",v[(i+1)>>1]);
puts("");++md;
if(md==ans) exit(0);return;
}
for(int i=0;i<g[x].size();i++){
v[g[x][i]]=1;dfs(x+1);v[g[x][i]]=0;
}
}
int main(){
read(n);read(m);read(k);
for(int i=1,x,y;i<=m;i++){
read(x);read(y);
add(x,y);add(y,x);
}
dijkstra();build();
printf("%d\n",ans);
dfs(2);
return 0;
}
CF1005F Berland and the Shortest Paths 最短路树计数的更多相关文章
- [Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs)
[Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs) 题面 题意:给你一个无向图,1为起点,求生成树让起点到其他个点的距离最小,距离最小 ...
- [CF1005F]Berland and the Shortest Paths_最短路树_堆优化dij
Berland and the Shortest Paths 题目链接:https://www.codeforces.com/contest/1005/problem/F 数据范围:略. 题解: 太鬼 ...
- CF1005F Berland and the Shortest Paths (树上构造最短路树)
题目大意:给你一个边权为$1$的无向图,构造出所有$1$为根的最短路树并输出 性质:单源最短路树上每个点到根的路径 ,一定是这个点到根的最短路之一 边权为$1$,$bfs$出单源最短路,然后构建最短路 ...
- CF1005F Berland and the Shortest Paths
\(\color{#0066ff}{ 题目描述 }\) 一个无向图(边权为1),输出一下选边的方案使\(\sum d_i\)最小(\(d_i\)为从1到i的最短路) 输出一个方案数和方案(方案数超过k ...
- Codeforces 1005 F - Berland and the Shortest Paths
F - Berland and the Shortest Paths 思路: bfs+dfs 首先,bfs找出1到其他点的最短路径大小dis[i] 然后对于2...n中的每个节点u,找到它所能改变的所 ...
- Codeforces Round #496 (Div. 3) F - Berland and the Shortest Paths
F - Berland and the Shortest Paths 思路:还是很好想的,处理出来最短路径图,然后搜k个就好啦. #include<bits/stdc++.h> #defi ...
- 【例题收藏】◇例题·II◇ Berland and the Shortest Paths
◇例题·II◇ Berland and the Shortest Paths 题目来源:Codeforce 1005F +传送门+ ◆ 简单题意 给定一个n个点.m条边的无向图.保证图是连通的,且m≥ ...
- Berland and the Shortest Paths CodeForces - 1005F(最短路树)
最短路树就是用bfs走一遍就可以了 d[v] = d[u] + 1 表示v是u的前驱边 然后遍历每个结点 存下它的前驱边 再用dfs遍历每个结点 依次取每个结点的某个前驱边即可 #include &l ...
- [CF1051F]The Shortest Statement_堆优化dij_最短路树_倍增lca
The Shortest Statement 题目链接:https://codeforces.com/contest/1051/problem/F 数据范围:略. 题解: 关于这个题,有一个重要的性质 ...
随机推荐
- 201871010116-祁英红《面向对象程序设计(java)》第一周学习总结
项目 内容 <面向对象程序设计(java)> https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/ ...
- Linux设备树文件结构与解析深度分析
Copy from :https://blog.csdn.net/woyimibayi/article/details/77574736 正文开始 1. Device Tree简介 设备树就是描述单板 ...
- rsync的简介及使用
1.rsync的基础概述 1.什么是备份 相当于给源文件增加一个副本,但是备份只会备份当前状态的数据,当你在写数据是,不会备份新写入的数据,除非自己手动在备份一次. 2.为什么要做备份 1.需要备份一 ...
- SourceTree3.2.6版本跳过注册办法
一.去sourceTree官网下载最新的包 官网:https://www.sourcetreeapp.com/windows版下载地址:https://product-downloads.atlass ...
- UmiJS 目录及约定
在文件和目录的组织上,umi 更倾向于选择约定的方式. 一个复杂应用的目录结构如下: . ├── dist/ // 默认的 build 输出目录 ├── mock/ // mock 文件所在目录,基于 ...
- 避免Java中NullPointerException的Java技巧和最佳实践
Java中的NullPointerException是我们最经常遇到的异常了,那我们到底应该如何在编写代码是防患于未然呢.下面我们就从几个方面来入手,解决这个棘手的问题吧. 值得庆幸的是,通过应用 ...
- IT兄弟连 Java语法教程 流程控制语句 循环结构语句4
do-while循环 Java还有一种循环是do-while.与for.while这些在循环顶部判断条件表达式的语句不同,do-while是在循环底部进行条件表达式的检查.这意味着do-while循环 ...
- EJB学习
EJB:企业级JavaBean(Enterprise JavaBean, EJB)是一个用来构筑企业级应用的服务器端可被管理组件. EJB主要有三种Bean: Session Beans: 会在单个特 ...
- ASP.NET 数据绑定
控件绑定数据源控件手动方式: DataSourceID = 数据源控件名称下拉框绑定 A.设置Datasource B.DataTextField="name"' //显示的值 C ...
- [转]使用IConfigureNamedOptions和ConfigureAll配置命名选项
这是我上一篇关于在ASP.NET Core 2.x中使用多个强类型设置实例的后续文章.在文章的结尾,我介绍了命名选项的概念,该选项已添加到ASP.NET Core 2.0中.在本文中,我将详细介绍如何 ...