问题描述

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. AcWing 836. 合并集合

    地址  https://www.acwing.com/problem/content/838/ 一共有n个数,编号是1~n,最开始每个数各自在一个集合中. 现在要进行m个操作,操作共有两种: “M a ...

  2. a迭代尾声处理

    这是小组开发a阶段迭代的成员工具teambition,现阶段a迭代需要最后的整合,同时我们也在开始了b迭代过程,并不断完善在开发工程中我们文档方面的记录

  3. Mybatis----传入参数parameterType类型详解

    Mybatis----传入参数parameterType类型详解 前言 Mybatis的Mapper文件中的select.insert.update.delete元素中有一个parameterType ...

  4. 使用VMware Workstation Player虚拟机安装Linux系统

    下载安装 VMware Workstation Player 首先下载并安装 VMware Workstation Player, VMware Workstation是一款非常强大的虚拟机软件,有p ...

  5. NLP中的数据增强

    相关方法合集见:https://github.com/quincyliang/nlp-data-augmentation 较为简单的数据增强的方法见论文:https://arxiv.org/pdf/1 ...

  6. 向技术领先的华为说No,就是对国家的通信前景说No!

    历史已经证明了,任何一项可以加速人员.物资.能源.金钱.信息迁移的技术,都会让社会原有的生产力成倍地增长.中国在互联网.移动互联网保持令整个世界震惊的飞速发展,以BAT为首的诸多商业帝国建立,还有人们 ...

  7. python request获取ip、获取登录设备

    from flask import request 获取ip request.remote_addr 获取登录设备 request.user_agent.string

  8. python做中学(八)匿名函数lambda的用法

    匿名函数,顾名思义即没有名称的函数,和def定义的函数的最大区别在于匿名函数创建后返回函数本身(即匿名函数不需要return来返回值),表达式本身结果就是返回值,而def创建后则赋值给一个变量名,在P ...

  9. Fuzzy finder(fzf+vim) 使用入门指南

    今天无意中尝试了fzf,才发现这个工具的威力无穷,毕竟是非常好的工具,第一次都把它的优点都释放出来也不现实,先熟悉一下吧,后面在实战中再不断地学习总结. 它是什么: Fuzzy finder 是一款使 ...

  10. error while loading shared libraries

    https://stackoverflow.com/questions/480764/linux-error-while-loading-shared-libraries-cannot-open-sh ...