问题描述

LG-CF1005F


题解

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

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


\(\mathrm{Code}\)

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. template <typename Tp>
  4. void read(Tp &x){
  5. x=0;char ch=1;int fh;
  6. while(ch!='-'&&(ch>'9'||ch<'0')) ch=getchar();
  7. if(ch=='-') ch=getchar(),fh=-1;
  8. else fh=1;
  9. while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
  10. x*=fh;
  11. }
  12. const int maxn=200007;
  13. const int maxm=400007;
  14. const int INF=0x3f3f3f3f;
  15. int n,m,k;
  16. int Head[maxn],to[maxm],Next[maxm],tot,w[maxm];
  17. void add(int x,int y){
  18. to[++tot]=y,Next[tot]=Head[x],Head[x]=tot,w[tot]=1;
  19. }
  20. int dis[maxn];
  21. bool vis[maxn];
  22. priority_queue< pair<int,int> > q;
  23. #define pii(x,y) make_pair(x,y)
  24. void dijkstra(){
  25. memset(dis,0x3f,sizeof(dis));
  26. q.push(pii(0,1));dis[1]=0;
  27. while(q.size()){
  28. int x=q.top().second;q.pop();
  29. if(vis[x]) continue;vis[x]=1;
  30. for(int i=Head[x];i;i=Next[i]){
  31. int y=to[i];
  32. if(dis[y]>dis[x]+w[i]){
  33. dis[y]=dis[x]+w[i];
  34. q.push(pii(-dis[y],y));
  35. }
  36. }
  37. }
  38. }
  39. int ans=1;
  40. vector<int>g[maxn];
  41. int val[maxn];
  42. void build(){
  43. for(int x=1;x<=n;x++){
  44. for(int i=Head[x];i;i=Next[i]){
  45. int y=to[i];
  46. if(dis[y]==dis[x]+w[i]){
  47. val[y]++;
  48. g[y].push_back((i+1)>>1);
  49. }
  50. }
  51. }
  52. for(int i=1;i<=n;i++){
  53. if(val[i]) ans=ans*val[i];
  54. if(ans>=k){
  55. ans=k;return;
  56. }
  57. }
  58. }
  59. bool v[maxm];
  60. int md;
  61. void dfs(int x){
  62. if(x==n+1){
  63. for(int i=1;i<=tot;i+=2) printf("%d",v[(i+1)>>1]);
  64. puts("");++md;
  65. if(md==ans) exit(0);return;
  66. }
  67. for(int i=0;i<g[x].size();i++){
  68. v[g[x][i]]=1;dfs(x+1);v[g[x][i]]=0;
  69. }
  70. }
  71. int main(){
  72. read(n);read(m);read(k);
  73. for(int i=1,x,y;i<=m;i++){
  74. read(x);read(y);
  75. add(x,y);add(y,x);
  76. }
  77. dijkstra();build();
  78. printf("%d\n",ans);
  79. dfs(2);
  80. return 0;
  81. }

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. 201871010116-祁英红《面向对象程序设计(java)》第一周学习总结

    项目 内容 <面向对象程序设计(java)> https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/ ...

  2. Linux设备树文件结构与解析深度分析

    Copy from :https://blog.csdn.net/woyimibayi/article/details/77574736 正文开始 1. Device Tree简介 设备树就是描述单板 ...

  3. rsync的简介及使用

    1.rsync的基础概述 1.什么是备份 相当于给源文件增加一个副本,但是备份只会备份当前状态的数据,当你在写数据是,不会备份新写入的数据,除非自己手动在备份一次. 2.为什么要做备份 1.需要备份一 ...

  4. SourceTree3.2.6版本跳过注册办法

    一.去sourceTree官网下载最新的包 官网:https://www.sourcetreeapp.com/windows版下载地址:https://product-downloads.atlass ...

  5. UmiJS 目录及约定

    在文件和目录的组织上,umi 更倾向于选择约定的方式. 一个复杂应用的目录结构如下: . ├── dist/ // 默认的 build 输出目录 ├── mock/ // mock 文件所在目录,基于 ...

  6. 避免Java中NullPointerException的Java技巧和最佳实践

    Java中的NullPointerException是我们最经常遇到的异常了,那我们到底应该如何在编写代码是防患于未然呢.下面我们就从几个方面来入手,解决这个棘手的​问题吧.​ 值得庆幸的是,通过应用 ...

  7. IT兄弟连 Java语法教程 流程控制语句 循环结构语句4

    do-while循环 Java还有一种循环是do-while.与for.while这些在循环顶部判断条件表达式的语句不同,do-while是在循环底部进行条件表达式的检查.这意味着do-while循环 ...

  8. EJB学习

    EJB:企业级JavaBean(Enterprise JavaBean, EJB)是一个用来构筑企业级应用的服务器端可被管理组件. EJB主要有三种Bean: Session Beans: 会在单个特 ...

  9. ASP.NET 数据绑定

    控件绑定数据源控件手动方式: DataSourceID = 数据源控件名称下拉框绑定 A.设置Datasource B.DataTextField="name"' //显示的值 C ...

  10. [转]使用IConfigureNamedOptions和ConfigureAll配置命名选项

    这是我上一篇关于在ASP.NET Core 2.x中使用多个强类型设置实例的后续文章.在文章的结尾,我介绍了命名选项的概念,该选项已添加到ASP.NET Core 2.0中.在本文中,我将详细介绍如何 ...