CF1005F Berland and the Shortest Paths 最短路树计数
问题描述
题解
由题面显然可得,所求即最短路树。
所以跑出最短路树,计数,输出方案即可。
\(\mathrm{Code}\)
#include<bits/stdc++.h>
using namespace std;
template <typename Tp>
void read(Tp &x){
x=0;char ch=1;int fh;
while(ch!='-'&&(ch>'9'||ch<'0')) ch=getchar();
if(ch=='-') ch=getchar(),fh=-1;
else fh=1;
while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
x*=fh;
}
const int maxn=200007;
const int maxm=400007;
const int INF=0x3f3f3f3f;
int n,m,k;
int Head[maxn],to[maxm],Next[maxm],tot,w[maxm];
void add(int x,int y){
to[++tot]=y,Next[tot]=Head[x],Head[x]=tot,w[tot]=1;
}
int dis[maxn];
bool vis[maxn];
priority_queue< pair<int,int> > q;
#define pii(x,y) make_pair(x,y)
void dijkstra(){
memset(dis,0x3f,sizeof(dis));
q.push(pii(0,1));dis[1]=0;
while(q.size()){
int x=q.top().second;q.pop();
if(vis[x]) continue;vis[x]=1;
for(int i=Head[x];i;i=Next[i]){
int y=to[i];
if(dis[y]>dis[x]+w[i]){
dis[y]=dis[x]+w[i];
q.push(pii(-dis[y],y));
}
}
}
}
int ans=1;
vector<int>g[maxn];
int val[maxn];
void build(){
for(int x=1;x<=n;x++){
for(int i=Head[x];i;i=Next[i]){
int y=to[i];
if(dis[y]==dis[x]+w[i]){
val[y]++;
g[y].push_back((i+1)>>1);
}
}
}
for(int i=1;i<=n;i++){
if(val[i]) ans=ans*val[i];
if(ans>=k){
ans=k;return;
}
}
}
bool v[maxm];
int md;
void dfs(int x){
if(x==n+1){
for(int i=1;i<=tot;i+=2) printf("%d",v[(i+1)>>1]);
puts("");++md;
if(md==ans) exit(0);return;
}
for(int i=0;i<g[x].size();i++){
v[g[x][i]]=1;dfs(x+1);v[g[x][i]]=0;
}
}
int main(){
read(n);read(m);read(k);
for(int i=1,x,y;i<=m;i++){
read(x);read(y);
add(x,y);add(y,x);
}
dijkstra();build();
printf("%d\n",ans);
dfs(2);
return 0;
}
CF1005F Berland and the Shortest Paths 最短路树计数的更多相关文章
- [Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs)
[Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs) 题面 题意:给你一个无向图,1为起点,求生成树让起点到其他个点的距离最小,距离最小 ...
- [CF1005F]Berland and the Shortest Paths_最短路树_堆优化dij
Berland and the Shortest Paths 题目链接:https://www.codeforces.com/contest/1005/problem/F 数据范围:略. 题解: 太鬼 ...
- 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 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 ...
- [CF1051F]The Shortest Statement_堆优化dij_最短路树_倍增lca
The Shortest Statement 题目链接:https://codeforces.com/contest/1051/problem/F 数据范围:略. 题解: 关于这个题,有一个重要的性质 ...
随机推荐
- 一个页面从输入url到加载到内容,这个过程经历了什么
首先,当浏览器接收到url,会查看本地缓存(浏览器缓存-系统缓存-路由器缓存)中是否有,有则直接显示 没有则进行DNS域名解析,将域名解析成IP地址,通过ip地址去访问相应的服务器, 浏览器访问服务器 ...
- 【Excel】删除重复值
- STL pair类型的介绍
pair标准库类型它定义在头文件utility中. 一个pair保存两个数据成员.类似容器,pair是一个用来生成特定类型的模板.当创建一个pair时,我们必须提供两个类型名,pair的数据成员将具有 ...
- Python进阶实战之三级菜单
目录 一.Python进阶实战之三级菜单 1.1 面条版 1.2 文艺青年版 一.Python进阶实战之三级菜单 打印省.市.县三级菜单 可返回上一级 可随时退出程序 1.1 面条版 menu = { ...
- Windows许可证 即将过期
最近打开电脑,系统总是自动弹出Windows许可证即将过期的弹窗,现在总结方法如下. 命令都是在运行窗口输入的打开方式:win+R组合键或者右键点击win10开始菜单,点击“运行”查看系统版本:win ...
- node 下载 md5.js
命令:npm install js-md5
- html公用头部和尾部
这个方式比较简单,样式和js也有效果,还有object和iframe方式 效果图,可以看出公共的样式对于引入的文件也有效果,在加载完文件后js也是有效果的 index.html header.html ...
- 1+x证书web前端开发jquery专项练习测试题
javascript程序设计-题库 1.下面哪一种不属于Jquery的选择器? A. 基本选择器 B. 层次选择器 C. 表单选择器 D. 节点选择器 答案: D 2.如果需要匹配包含文本的元素,用下 ...
- java 类内部定义接口
java类内部可以定义接口,作用可以看作是对类功能的进一步补充,类里面包含两部分:一部分是自己的固定的,一部分是可以变化的,而这可变的部分就编程了一个接口. 另一个作用是避免命名冲突. 示例 类Fru ...
- MongoDB for OPS 04:备份恢复
写在前面的话 和 MySQL 一样,mongodb 也是需要将数据进行备份的,毕竟天有不测风云,谁也不知道哪天机器就炸了. 备份恢复 mongodb 提供了两种备份恢复手段:mongoexport / ...