[Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs)
[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)的更多相关文章
- CF1005F Berland and the Shortest Paths 最短路树计数
问题描述 LG-CF1005F 题解 由题面显然可得,所求即最短路树. 所以跑出最短路树,计数,输出方案即可. \(\mathrm{Code}\) #include<bits/stdc++.h& ...
- [CF1005F]Berland and the Shortest Paths_最短路树_堆优化dij
Berland and the Shortest Paths 题目链接:https://www.codeforces.com/contest/1005/problem/F 数据范围:略. 题解: 太鬼 ...
- Codeforces 1005 F - Berland and the Shortest Paths
F - Berland and the Shortest Paths 思路: bfs+dfs 首先,bfs找出1到其他点的最短路径大小dis[i] 然后对于2...n中的每个节点u,找到它所能改变的所 ...
- Codeforces Round #496 (Div. 3) F - Berland and the Shortest Paths
F - Berland and the Shortest Paths 思路:还是很好想的,处理出来最短路径图,然后搜k个就好啦. #include<bits/stdc++.h> #defi ...
- 【例题收藏】◇例题·II◇ Berland and the Shortest Paths
◇例题·II◇ Berland and the Shortest Paths 题目来源:Codeforce 1005F +传送门+ ◆ 简单题意 给定一个n个点.m条边的无向图.保证图是连通的,且m≥ ...
- Berland and the Shortest Paths CodeForces - 1005F(最短路树)
最短路树就是用bfs走一遍就可以了 d[v] = d[u] + 1 表示v是u的前驱边 然后遍历每个结点 存下它的前驱边 再用dfs遍历每个结点 依次取每个结点的某个前驱边即可 #include &l ...
- CF1005F Berland and the Shortest Paths (树上构造最短路树)
题目大意:给你一个边权为$1$的无向图,构造出所有$1$为根的最短路树并输出 性质:单源最短路树上每个点到根的路径 ,一定是这个点到根的最短路之一 边权为$1$,$bfs$出单源最短路,然后构建最短路 ...
- CF1005F Berland and the Shortest Paths
\(\color{#0066ff}{ 题目描述 }\) 一个无向图(边权为1),输出一下选边的方案使\(\sum d_i\)最小(\(d_i\)为从1到i的最短路) 输出一个方案数和方案(方案数超过k ...
- 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 ...
随机推荐
- Libraries&Workflow for a modern geospatial processing(现代地理空间处理的库与工作流)
Libraries for a modern geospatial workflow现代地理空间工作的类库 Distribution Writing, Running, and Distributin ...
- python学习---50行代码实现图片转字符画2
from PIL import Image codeLib = '''@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<> ...
- LeetCode 19. 删除链表的倒数第N个节点(Remove Nth Node From End Of List)
题目描述 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后, ...
- 【Spark机器学习速成宝典】模型篇07梯度提升树【Gradient-Boosted Trees】(Python版)
目录 梯度提升树原理 梯度提升树代码(Spark Python) 梯度提升树原理 待续... 返回目录 梯度提升树代码(Spark Python) 代码里数据:https://pan.baidu.co ...
- spring cloud microservice provider and consumer
MicroService Provider:https://files.cnblogs.com/files/xiandedanteng/empCloud190824.rarMicroService C ...
- 7. 进行图片的数据补全和增强(随机亮度,随机饱和度,随机翻转) Image.open(进行图片的读入) 2.ImageEnhance.Brightness(亮度变化) 3.ImageEnhance.Contrast(饱和度变化) 4.enhance_image.transpose(图片随机翻转) 5.enhance_image.save(进行图片保存)
1.Image.open(image_path) 进行图片的打开 参数说明:image_path 表示图片的路径 2. ImageEnhance.Brightness(image) # 进行图片的 ...
- 访问 Django 项目的静态资源
from django.urls import path, re_path from django.conf import settingsfrom django.views.static impor ...
- android开源图表库MPAndroidChart(曲线图、直方图、饼状图)
github地址:https://github.com/PhilJay/MPAndroidChart 添加依赖: Add the following to your project level bui ...
- react介绍、环境搭建、demo运行实例
React官网:https://reactjs.org/docs/create-a-new-react-app.html cnpm网址:http://npm.taobao.org/ 1.react介绍 ...
- ControlTemplate in WPF —— DatePicker
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...