CF1005F Berland and the Shortest Paths
\(\color{#0066ff}{ 题目描述 }\)
一个无向图(边权为1),输出一下选边的方案使\(\sum d_i\)最小(\(d_i\)为从1到i的最短路)
输出一个方案数和方案(方案数超过k个只需输出k个)
\(\color{#0066ff}{输入格式}\)
第一行n,m,k,为点数,边数和k
接下来m行为无向边(权为1)
\(\color{#0066ff}{输出格式}\)
第一行为方案数(超过k个只输出k个)
接下来是01表示的方案(每条边选或不选)
\(\color{#0066ff}{输入样例}\)
4 4 3
1 2
2 3
1 4
4 3
4 6 3
1 2
2 3
1 4
4 3
2 4
1 3
5 6 2
1 2
1 3
2 4
2 5
3 4
3 5
\(\color{#0066ff}{输出样例}\)
2
1110
1011
1
101001
2
111100
110110
\(\color{#0066ff}{数据范围与提示}\)
\(n\leq 2*10^5, n-1\leq m \leq 2*10^5, 1 \leq k \leq 2*10^5, m*k\leq 10^6\)
\(\color{#0066ff}{ 题解 }\)
显然这题要求的是最短路图的生成树方案
先找出最短路图,根据dis(到1距离)分层
然后从2开始每个点都有向上连边的方案,统计一下
\(O(m*k)\)输出方案(搜索,枚举)
#include<bits/stdc++.h>
#define LL long long
LL in() {
char ch; LL x = 0, f = 1;
while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
return x * f;
}
LL n, m, k;
const int maxn = 2e5 + 100;
struct node {
int to, id;
node *nxt;
node(int to = 0, int id = 0, node *nxt = NULL): to(to), id(id), nxt(nxt) {}
void *operator new(size_t) {
static node *S = NULL, *T = NULL;
return (S == T) && (T = (S = new node[1024]) + 1024), S++;
}
};
using std::pair;
using std::make_pair;
std::priority_queue<pair<int, int>, std::vector<pair<int, int> >, std::greater<pair<int, int> > > q;
std::queue<int> v;
int cnt[maxn], dis[maxn], du[maxn];
bool vis[maxn];
node *head[maxn], *h[maxn];
void add(int from, int to, int id, node **hh) {
hh[from] = new node(to, id, hh[from]);
}
void dij() {
for(int i = 1; i <= n; i++) dis[i] = maxn;
q.push(make_pair(dis[1] = 0, 1));
while(!q.empty()) {
int tp = q.top().second; q.pop();
if(vis[tp]) continue;
vis[tp] = true;
for(node *i = head[tp]; i; i = i->nxt)
if(dis[i->to] > dis[tp] + 1)
q.push(make_pair(dis[i->to] = dis[tp] + 1, i->to));
}
}
void toposort() {
LL tot = 1;
for(int i = 2; i <= n; i++) {
tot *= du[i];
if(tot >= k) break;
}
k = std::min(tot, k);
printf("%lld\n", k);
}
void dfs(int d) {
if(!k) return;
if(d == n + 1) {
for(int i = 1; i <= m; i++) printf("%d", vis[i]);
puts("");
k--;
return;
}
for(node *i = h[d]; i; i = i->nxt) {
vis[i->id] = true;
dfs(d + 1);
vis[i->id] = false;
if(!k) return;
}
}
int main() {
n = in(), m = in(), k = in();
int x, y;
for(int i = 1; i <= m; i++) {
x = in(), y = in();
add(x, y, i, head);
add(y, x, i, head);
}
dij();
for(int i = 1; i <= n; i++)
for(node *j = head[i]; j; j = j->nxt)
if(dis[j->to] == dis[i] + 1)
add(j->to, i, j->id, h), du[j->to]++;
toposort();
for(int i = 1; i <= m; i++) vis[i] = 0;
dfs(2);
return 0;
}
CF1005F Berland and the Shortest Paths的更多相关文章
- CF1005F Berland and the Shortest Paths (树上构造最短路树)
题目大意:给你一个边权为$1$的无向图,构造出所有$1$为根的最短路树并输出 性质:单源最短路树上每个点到根的路径 ,一定是这个点到根的最短路之一 边权为$1$,$bfs$出单源最短路,然后构建最短路 ...
- CF1005F Berland and the Shortest Paths 最短路树计数
问题描述 LG-CF1005F 题解 由题面显然可得,所求即最短路树. 所以跑出最短路树,计数,输出方案即可. \(\mathrm{Code}\) #include<bits/stdc++.h& ...
- 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≥ ...
- [CF1005F]Berland and the Shortest Paths_最短路树_堆优化dij
Berland and the Shortest Paths 题目链接:https://www.codeforces.com/contest/1005/problem/F 数据范围:略. 题解: 太鬼 ...
- [Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs)
[Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs) 题面 题意:给你一个无向图,1为起点,求生成树让起点到其他个点的距离最小,距离最小 ...
- Berland and the Shortest Paths CodeForces - 1005F(最短路树)
最短路树就是用bfs走一遍就可以了 d[v] = d[u] + 1 表示v是u的前驱边 然后遍历每个结点 存下它的前驱边 再用dfs遍历每个结点 依次取每个结点的某个前驱边即可 #include &l ...
- Shortest Paths
最短路径 APIs 带权有向图中的最短路径,这节讨论从源点(s)到图中其它点的最短路径(single source). Weighted Directed Edge API 需要新的数据类型来表示带权 ...
随机推荐
- 【转】onclick事件与href='javascript:function()'的区别
href='javascript:function()'和onclick能起到同样的效果,一般来说,如果要调用脚本还是在onclick事件里面写代码,而不推荐在href='javascript:fun ...
- Ubuntu下设置VNCServer
Ubuntu下设置VNCServer Virtual Network Computing(VNC)是进行远程桌面控制的一个软件.客户端的键盘输入和鼠标操作通过网络传输到远程服务器,控制服务器的操作.服 ...
- Python 标准库 -> Pprint 模块 -> 用于打印 Python 数据结构
使用 pprint 模块 pprint 模块( pretty printer ) 用于打印 Python 数据结构. 当你在命令行下打印特定数据结构时你会发现它很有用(输出格式比较整齐, 便于阅读). ...
- Eclipse中如何开启断言(Assert),方法有二
Eclipse中如何开启断言(Assert),方法有二:1.Run -> Run Configurations -> Arguments页签 -> VM arguments文本框中加 ...
- python之模块(在命令行当中使用pip install 模块来进行模块的安装)
模块:在程序设计中,为完成某一功能所需的一段程序或子程序:或指能由编译程序.装配程序等处理的独立程序单位. 在我们编程的时候我们如果将所有的文件都放在那个py文件中,我们的py文件会很大,这样也很不好 ...
- 【266】增加bash文件的执行权限
正常需要通过[bash pass.sh]来执行文件,但是可以通过增加bash文件的执行权限实现通过[./pass.sh]或者[pass.sh]来执行文件. 方法:通过chmod来增加权限,下面四种方法 ...
- Error: Cannot run program "/home/xxx/android_developer_tools/android-ndk-r8/ndk-build.cmd": Unknown reason
运行OpenCV官方例子 tutorial-2-mixedprocessing 总提示 /home/xxx/android_developer_tools/ 明明在PATH中采用好几种方法都加入了 ...
- 关于WebGIS开源解决方案的探讨(转)
关于WebGIS开源解决方案的探讨 文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.背景 公司目前的多数项目采 ...
- HTML5的头部、拨号、短信、邮件(转)
HTML5[语法要点] 一.头部设置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <!--页面窗口自动调整到设备 ...
- PDF的一些工具
PdfStreamDumper Pdftk 这里有一个链接,列出了很多pdf免费工具 https://en.wikipedia.org/wiki/Category:Free_PDF_software ...