[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. 数据库范式以及ER图

    数据库范式包括第一.第二.第三以及BCNF范式,关于范式的探讨,博主在知乎上看见了一篇很不错的文章,分享文中,这边就不再做阐述.地址:https://www.zhihu.com/question/24 ...

  2. CF#356 div2 C 猜数字

    C. Bear and Prime 100 time limit per test 1 second memory limit per test 256 megabytes input standar ...

  3. 论文阅读:Forwarding Metamorphosis: Fast Programmable Match-Action Processing in Hardware for SDN

    摘要: 在软件定义网络中,控制平面在物理上与转发平面分离,控制软件使用开放接口(例如OpenFlow)对转发平面(例如,交换机和路由器)进行编程. 本文旨在克服当前交换芯片和OpenFlow协议的两个 ...

  4. 安装MongoDB到CentOS 6

    MongoDB是一个面向海量文档存数据动态存储的NoSQL型数据库.是一个除了用于关系型数据库如MySQL,PostgreSQL数据库表格的格式,和微软SQL以外的一种数据模型存储形式.他的功能包括了 ...

  5. unittest详解(一) unittest初识

    unittest是python内置的一个单元测试框架,在学习怎么使用它之前,我们先来了解它的一些概念和原理. Test Case:测试用例,一个TestCase的实例就是一个测试用例.什么是测试用例呢 ...

  6. AcWing:138. 兔子与兔子(字符串Hash)

    很久很久以前,森林里住着一群兔子. 有一天,兔子们想要研究自己的 DNA 序列. 我们首先选取一个好长好长的 DNA 序列(小兔子是外星生物,DNA 序列可能包含 26 个小写英文字母). 然后我们每 ...

  7. JS框架_(JQuery.js)纯css3进度条动画

    百度云盘 传送门 密码:wirc 进度条动画效果: <!DOCTYPE html> <html lang="zh"> <head> <me ...

  8. IDEA下从零开始搭建SpringBoot工程

    SpringBoot的具体介绍可以参看其他网上介绍,这里就不多说了,就这几天的学习,个人理解,简而言之: (1)它是Spring的升级版,Spring容器能做到的事情,它都能做到,而且更简便,从配置形 ...

  9. 分布式-信息方式-JMS Topic示例

                                                      Topic消息 非持久的 Topic消息示例对于非持久的 Topic消息的发送       基本跟前 ...

  10. 把execel表数据导入mysql数据库

    今天,是我来公司第二周的第一天. 作为新入职的实习生,目前还没适合我的实质项目工作,今天的学习任务是: 把execel表数据导入到mysql数据库,再练习下java操作JDBC. 先了解下execel ...