[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 ...
随机推荐
- hdu_2159(二维费用背包)
HDU_2159 二维费用背包问题 http://acm.hdu.edu.cn/showproblem.php?pid=2159 #include<cstdio> #include< ...
- windows基础提权
Window基础提权 提到system权限 甚至让他变成你的肉鸡 我们了解一下windows下面有那些用户 Guests是用户最低的权限 而且一般是被禁用的 User权限也很低 连关机都不行 还有wi ...
- war包部署到tomcat
1.maven web app打包成app.war.打包命令:mvn clean package Dmaven.test.skip=true war 是什么?里面有什么东西?a.web.app所有必 ...
- Zookeeper入门(五)之Linux环境下Zookeeper安装
本文参考地址为:http://www.mamicode.com/info-detail-2243059.html1.安装wget http://archive.apache.org/dist/zook ...
- Override和Overload的含义与区别
overload是重载,重载是一种参数多态机制,即代码通过参数的类型或个数不同而实现的多态机制. 是一种静态的绑定机制(在编译时已经知道具体执行的是哪个代码段). override是重写,重写是一种 ...
- nodejs 配置服务器
node 是 js 的运行的后台环境,他自身集成了很多模块,集成的模块直接 require 就行了: npm 第三方平台,他也是为 node 服务的,对于 npm 中的模块,先 npm install ...
- heigth innerheigt outerheight详解
height() :height innerHeight(): height + paddingouterHeight(): height + padding + border outerHeight ...
- GestureDetector手势识别器
package com.loaderman.gesturedetectordemo; import android.os.Bundle; import android.support.v7.app.A ...
- 阶段3 2.Spring_08.面向切面编程 AOP_10 总结和作业安排
由转账添加事物,使得我们的操作变的非常麻烦.重复代码产生了很多 实际的开发中如果想记录日志每个方法都要执行 如果判断用户是否登陆也是每个方法都需要判断 这些重复的代码我们都需要去解决. 解决的方式,以 ...
- 三十五:数据库之SQLAlchemy外建之一对多关系
准备工作 from sqlalchemy import create_engine, Column, Integer, String, Float, Text, ForeignKeyfrom sqla ...