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

题面

题意:给你一个无向图,1为起点,求生成树让起点到其他个点的距离最小,距离最小的生成树可能有多个。给定k,如果方案数比k小就输出全部方案,否则输出k种方案。

分析

先跑最短路,对于每个点找到它在最短路树上可能的父亲.即对于\((x,y) \in E,dist(y)=dist(x)+len(x,y)\)。那么y在最短路上可能的父亲就是x.说“可能”是因为最短路树可能不唯一。

然后dfs枚举每个点选哪个父亲,输出答案即可。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#define maxn 300000
#define maxm 300000
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;
int n,m,k;
struct edge {
int from;
int to;
int next;
int id;
int type;
} E[maxm*2+5];
int head[maxn+5];
int esz=1;
void add_edge(int u,int v,int id) {
esz++;
E[esz].from=u;
E[esz].to=v;
E[esz].next=head[u];
E[esz].id=id;
head[u]=esz;
} struct node {
int id;
ll dist;
node() { }
node(int _id,ll _dist) {
id=_id;
dist=_dist;
}
friend bool operator < (node p,node q) {
return p.dist>q.dist;
}
};
int pre[maxn+5];
bool vis[maxn+5];
ll dist[maxn+5];
void dijkstra(int s) {
priority_queue<node>q;
memset(vis,0,sizeof(vis));
memset(dist,0x3f,sizeof(dist));
dist[s]=0;
q.push(node(s,0));
while(!q.empty()) {
int x=q.top().id;
q.pop();
if(vis[x]) continue;
vis[x]=1;
for(int i=head[x]; i; i=E[i].next) {
int y=E[i].to;
if(dist[y]>dist[x]+1) {
dist[y]=dist[x]+1;
pre[y]=i;
if(!vis[y]) q.push(node(y,dist[y]));
}
}
}
// for(int i=1; i<=n; i++) {
// if(pre[i]) E[pre[i]].type=E[pre[i]^1].type=1;
// }
} vector<int>T[maxn+5];
int cnt=0;
vector<string>ans;
string now;
void dfs(int x){//搜索每个点的前驱
if(ans.size()>=k) return;
if(x>n){
ans.push_back(now);
return;
}
for(int i=0;i<T[x].size();i++){
//枚举选哪个前驱
// printf("db: %d\n",T[x][i]);
now[T[x][i]-1]='1';
dfs(x+1);
now[T[x][i]-1]='0';
}
}
int main() {
int u,v;
scanf("%d %d %d",&n,&m,&k);
for(int i=1;i<=m;i++){
scanf("%d %d",&u,&v);
add_edge(u,v,i);
add_edge(v,u,i);
}
dijkstra(1);
for(int i=2;i<=esz;i++){
int x=E[i].from;
int y=E[i].to;
if(dist[y]==dist[x]+1){
T[y].push_back(E[i].id);
//找每个点的前驱
//注意不是x而是id
}
}
now.resize(m);
for(int i=0;i<m;i++) now[i]='0';
dfs(2);//从2开始搜索前驱
printf("%d\n",ans.size());
for(int i=0;i<ans.size();i++){
// for(int j=0;j<ans[i].length();j++) putchar(ans[i][j]);
cout<<ans[i];
putchar('\n');
}
}

[Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs)的更多相关文章

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

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

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

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

  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. Berland and the Shortest Paths CodeForces - 1005F(最短路树)

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

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

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

  8. CF1005F Berland and the Shortest Paths

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

  9. Codeforces Round #303 (Div. 2) E. Paths and Trees 最短路+贪心

    题目链接: 题目 E. Paths and Trees time limit per test 3 seconds memory limit per test 256 megabytes inputs ...

随机推荐

  1. jquery type属性 语法

    jquery type属性 语法 作用:type 属性描述触发哪种事件类型.大理石直角尺 语法:event.typ 参数: 参数 描述 event     必需.规定要检查的事件.这个 event 参 ...

  2. 51nod 1120 机器人走方格V3

    1120 机器人走方格 V3  基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  关注 N * N的方格,从左上到右下画一条线.一个机器人从左上走到右下,只 ...

  3. 1003: [ZJOI2006]物流运输

    就我一开始写状压的吗? 调不过 后来发现(直接搜索)直接最短路就行了-- \(f[i]\)表示前\(i\)天最少需要多少 \(f[i] = min(f[j] + dis(j + 1, i))\) 然后 ...

  4. JavaScript 的基本概念( ES5 )

    语法 区分大小写 标识符 第一个字符必须是一个字母,下划线或者一个美元符( $ ).其他规则无论,最好按照通用的驼峰大小写. 注释 // 单行注释 /* 多行注释 */ 严格模式 在顶部添加如下代码 ...

  5. vue 组件 Vue.component 用法

    todo https://blog.csdn.net/weixin_41796631/article/details/82929139

  6. [CSP-S模拟测试]:你相信引力吗(单调栈)

    题目传送门(内部题124) 输入格式 第一行一个整数$n$代表环的长度. 第二行$n$个整数表示每个冰锥的高度. 输出格式 一行一个整数表示有多少对冰锥是危险的. 样例 样例输入1: 51 2 4 5 ...

  7. Python set 用法

    (原文链接)http://blog.csdn.net/business122/article/details/7541486# python的set和其他语言类似, 是一个无序不重复元素集, 基本功能 ...

  8. python 生成随机数的几种方法

      随机取一个: import random random.choice(string.digits)#从数字里随机选取一位数字: 随机取多位数:   random.sample(string.dig ...

  9. 第七周总结&第五次实验报告

    学习总结 这周我们加深了对抽象类与接口的学习,获得的知识点也比上周多了许多,抽象类与接口很相似,就比如别人还没有做完的是交给你来做,而他那些样式都做好了,你只需要完善即可 但也有不同点. 区别点 抽象 ...

  10. Linux高级调试与优化——Address Sanitizer

    Address Sanitizer ASAN最早可以追溯到 LLVM 的 sanitizers项目(https://github.com/google/sanitizers),这个项目包含了Addre ...