问题描述

LG-CF1005F


题解

由题面显然可得,所求即最短路树。

所以跑出最短路树,计数,输出方案即可。


\(\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 最短路树计数的更多相关文章

  1. [Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs)

    [Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs) 题面 题意:给你一个无向图,1为起点,求生成树让起点到其他个点的距离最小,距离最小 ...

  2. [CF1005F]Berland and the Shortest Paths_最短路树_堆优化dij

    Berland and the Shortest Paths 题目链接:https://www.codeforces.com/contest/1005/problem/F 数据范围:略. 题解: 太鬼 ...

  3. CF1005F Berland and the Shortest Paths (树上构造最短路树)

    题目大意:给你一个边权为$1$的无向图,构造出所有$1$为根的最短路树并输出 性质:单源最短路树上每个点到根的路径 ,一定是这个点到根的最短路之一 边权为$1$,$bfs$出单源最短路,然后构建最短路 ...

  4. CF1005F Berland and the Shortest Paths

    \(\color{#0066ff}{ 题目描述 }\) 一个无向图(边权为1),输出一下选边的方案使\(\sum d_i\)最小(\(d_i\)为从1到i的最短路) 输出一个方案数和方案(方案数超过k ...

  5. Codeforces 1005 F - Berland and the Shortest Paths

    F - Berland and the Shortest Paths 思路: bfs+dfs 首先,bfs找出1到其他点的最短路径大小dis[i] 然后对于2...n中的每个节点u,找到它所能改变的所 ...

  6. Codeforces Round #496 (Div. 3) F - Berland and the Shortest Paths

    F - Berland and the Shortest Paths 思路:还是很好想的,处理出来最短路径图,然后搜k个就好啦. #include<bits/stdc++.h> #defi ...

  7. 【例题收藏】◇例题·II◇ Berland and the Shortest Paths

    ◇例题·II◇ Berland and the Shortest Paths 题目来源:Codeforce 1005F +传送门+ ◆ 简单题意 给定一个n个点.m条边的无向图.保证图是连通的,且m≥ ...

  8. Berland and the Shortest Paths CodeForces - 1005F(最短路树)

    最短路树就是用bfs走一遍就可以了 d[v] = d[u] + 1 表示v是u的前驱边 然后遍历每个结点 存下它的前驱边 再用dfs遍历每个结点 依次取每个结点的某个前驱边即可 #include &l ...

  9. [CF1051F]The Shortest Statement_堆优化dij_最短路树_倍增lca

    The Shortest Statement 题目链接:https://codeforces.com/contest/1051/problem/F 数据范围:略. 题解: 关于这个题,有一个重要的性质 ...

随机推荐

  1. FFT/NTT初探

    做了全家桶然后写了几道入门题. FFT.ref NTT.ref Luogu4238 [模板]多项式求逆 Link 套牛顿迭代完事.有一个细节问题是:这次运算多项式有几项就只赋几项的值,其他位置(次数大 ...

  2. 设计模式-Strategy Strategy将算法封装到类中,通过组合的方式 将具体算法的实现在组合对象中实现

    以下代码来源: 设计模式精解-GoF 23种设计模式解析附C++实现源码 //strategy.h #pragma once class Strategy { public: Strategy(); ...

  3. npm install 提示 `gyp: No Xcode or CLT version detected!` MacOS 10.15

    https://github.com/nodejs/node-gyp/issues/569 https://github.com/nodejs/node-gyp/issues/1927 解决链接:ht ...

  4. ArcGIS10.2配置PostgreSQL9.2标准教程

    ArcGIS 支持Oracle.DB2.PostgreSQL.SQLite关系型数据库升级为企业地理数据,Oracle太庞大,SQLite太小,DB2多在IBM上用,只有PostgreSQL最适合,它 ...

  5. 趣谈Linux操作系统学习笔记:第二十六讲

    一.内核页表 和用户态页表不同,在系统初始化的时候,我们就要创建内核页表了 我们从内核页表的根swapper_pg_dir开始找线索,在linux-5.1.3/arch/x86/include/asm ...

  6. Windows许可证 即将过期

    最近打开电脑,系统总是自动弹出Windows许可证即将过期的弹窗,现在总结方法如下. 命令都是在运行窗口输入的打开方式:win+R组合键或者右键点击win10开始菜单,点击“运行”查看系统版本:win ...

  7. 终端中的 zsh 和 bash-魔法切换

    常用ubuntu,这两个终端都装了,平时使用zsh比较方便,可是,有时候出现了问题,不知道是谁的问题时候,还要做一下切换操作的,怎么才能迅速切换呢? 要切换,首先要知道你现在使用的是什么,请看第一个命 ...

  8. Java多线程并发面试问答

    Java并发面试问答 什么是原子操作?Java并发API中的原子类是什么? 原子操作在单个任务单元中执行,而不受其他操作的干扰.在多线程环境中,原子操作是必需的,以避免数据不一致. int++不是原子 ...

  9. Find 查找命令时过滤掉某些文件或目录 以及 -maxdepth、-mindepth的用法

    1)find过滤目录使用find命令在linux系统中查找文件时,有时需要忽略某些目录,可以使用"-path 过滤的目录路径 -prune -o"参数来进行过滤.不过必须注意:要忽 ...

  10. 搭建Jupyter学习环境

    `python notebook`是一个基于浏览器的python数据分析工具,使用起来非常方便,具有极强的交互方式和富文本的展示效果.jupyter是它的升级版,它的安装也非常方便,一般`Anacon ...