\(\color{#0066ff}{ 题目描述 }\)

一个无向图(边权为1),输出一下选边的方案使\(\sum d_i\)最小(\(d_i\)为从1到i的最短路)

输出一个方案数和方案(方案数超过k个只需输出k个)

\(\color{#0066ff}{输入格式}\)

第一行n,m,k,为点数,边数和k

接下来m行为无向边(权为1)

\(\color{#0066ff}{输出格式}\)

第一行为方案数(超过k个只输出k个)

接下来是01表示的方案(每条边选或不选)

\(\color{#0066ff}{输入样例}\)

  1. 4 4 3
  2. 1 2
  3. 2 3
  4. 1 4
  5. 4 3
  6. 4 6 3
  7. 1 2
  8. 2 3
  9. 1 4
  10. 4 3
  11. 2 4
  12. 1 3
  13. 5 6 2
  14. 1 2
  15. 1 3
  16. 2 4
  17. 2 5
  18. 3 4
  19. 3 5

\(\color{#0066ff}{输出样例}\)

  1. 2
  2. 1110
  3. 1011
  4. 1
  5. 101001
  6. 2
  7. 111100
  8. 110110

\(\color{#0066ff}{数据范围与提示}\)

\(n\leq 2*10^5, n-1\leq m \leq 2*10^5, 1 \leq k \leq 2*10^5, m*k\leq 10^6\)

\(\color{#0066ff}{ 题解 }\)

显然这题要求的是最短路图的生成树方案

先找出最短路图,根据dis(到1距离)分层

然后从2开始每个点都有向上连边的方案,统计一下

\(O(m*k)\)输出方案(搜索,枚举)

  1. #include<bits/stdc++.h>
  2. #define LL long long
  3. LL in() {
  4. char ch; LL x = 0, f = 1;
  5. while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
  6. for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
  7. return x * f;
  8. }
  9. LL n, m, k;
  10. const int maxn = 2e5 + 100;
  11. struct node {
  12. int to, id;
  13. node *nxt;
  14. node(int to = 0, int id = 0, node *nxt = NULL): to(to), id(id), nxt(nxt) {}
  15. void *operator new(size_t) {
  16. static node *S = NULL, *T = NULL;
  17. return (S == T) && (T = (S = new node[1024]) + 1024), S++;
  18. }
  19. };
  20. using std::pair;
  21. using std::make_pair;
  22. std::priority_queue<pair<int, int>, std::vector<pair<int, int> >, std::greater<pair<int, int> > > q;
  23. std::queue<int> v;
  24. int cnt[maxn], dis[maxn], du[maxn];
  25. bool vis[maxn];
  26. node *head[maxn], *h[maxn];
  27. void add(int from, int to, int id, node **hh) {
  28. hh[from] = new node(to, id, hh[from]);
  29. }
  30. void dij() {
  31. for(int i = 1; i <= n; i++) dis[i] = maxn;
  32. q.push(make_pair(dis[1] = 0, 1));
  33. while(!q.empty()) {
  34. int tp = q.top().second; q.pop();
  35. if(vis[tp]) continue;
  36. vis[tp] = true;
  37. for(node *i = head[tp]; i; i = i->nxt)
  38. if(dis[i->to] > dis[tp] + 1)
  39. q.push(make_pair(dis[i->to] = dis[tp] + 1, i->to));
  40. }
  41. }
  42. void toposort() {
  43. LL tot = 1;
  44. for(int i = 2; i <= n; i++) {
  45. tot *= du[i];
  46. if(tot >= k) break;
  47. }
  48. k = std::min(tot, k);
  49. printf("%lld\n", k);
  50. }
  51. void dfs(int d) {
  52. if(!k) return;
  53. if(d == n + 1) {
  54. for(int i = 1; i <= m; i++) printf("%d", vis[i]);
  55. puts("");
  56. k--;
  57. return;
  58. }
  59. for(node *i = h[d]; i; i = i->nxt) {
  60. vis[i->id] = true;
  61. dfs(d + 1);
  62. vis[i->id] = false;
  63. if(!k) return;
  64. }
  65. }
  66. int main() {
  67. n = in(), m = in(), k = in();
  68. int x, y;
  69. for(int i = 1; i <= m; i++) {
  70. x = in(), y = in();
  71. add(x, y, i, head);
  72. add(y, x, i, head);
  73. }
  74. dij();
  75. for(int i = 1; i <= n; i++)
  76. for(node *j = head[i]; j; j = j->nxt)
  77. if(dis[j->to] == dis[i] + 1)
  78. add(j->to, i, j->id, h), du[j->to]++;
  79. toposort();
  80. for(int i = 1; i <= m; i++) vis[i] = 0;
  81. dfs(2);
  82. return 0;
  83. }

CF1005F Berland and the Shortest Paths的更多相关文章

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

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

  2. CF1005F Berland and the Shortest Paths 最短路树计数

    问题描述 LG-CF1005F 题解 由题面显然可得,所求即最短路树. 所以跑出最短路树,计数,输出方案即可. \(\mathrm{Code}\) #include<bits/stdc++.h& ...

  3. Codeforces 1005 F - Berland and the Shortest Paths

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

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

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

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

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

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

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

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

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

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

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

  9. Shortest Paths

    最短路径 APIs 带权有向图中的最短路径,这节讨论从源点(s)到图中其它点的最短路径(single source). Weighted Directed Edge API 需要新的数据类型来表示带权 ...

随机推荐

  1. Python中try...except...else的用法

    Python中try...except...else的用法: try:    <语句>except <name>:    <语句>          #如果在try ...

  2. IDA Pro权威指南学习笔记(一)

    一直不懂逆向,最近刚好不忙,于是学习逆向,用来做笔记,顺便和大家分享交流. 参考书籍<IAD PRO权威指南> 工具: PETools: ETools 是另一款很好的PE文件编辑工具,以前 ...

  3. python-xlrd 实现excel 导入数据

    首先安装 xlrd 两种方式: 1.wheel 方式 安装: 首先要下载 wheel :

  4. windows配置apache tomcat 集群

      1,安装包 httpd-2.2.22-win32-x86-no_ssl.msi 两个tomcat6 2,配置apachehttpd---配置的过程中有错误可以查看logs文件夹下的log文件进行排 ...

  5. JavaScript语言基础-包装对象

  6. elastic(7)bulk

    转自:https://www.cnblogs.com/xing901022/p/5339419.html bulk批量导入 批量导入可以合并多个操作,比如index,delete,update,cre ...

  7. HTML布局,插件的调用方法

  8. 如何让DIALOG点击确定按钮之后由于数据不合法不关闭

    public void SetDialogIsClose(DialogInterface pDialog, Boolean pisClose) { try { Field _Field = pDial ...

  9. a标签中href=""的几种用法(转)

    a标签中href=""的几种用法   标签: html / a标签 / javascript 46371 众所周知,a标签的最重要功能是实现超链接和锚点.而且,大多数人认为a标签最 ...

  10. Codeforces #345div1 C Table Compression (650C) 并查集

    题意:给你一个n*m的矩阵,需要在不改变每一行和每一列的大小关系的情况下压缩一个矩阵,压缩后的矩阵所有数的总和尽量的小. 思路:我们有这样的初步设想:对于在一行或一列的数x,y,若x<y,则建立 ...