@description@

给定一个 n 点 m 条边的无向连通图,每条边的边权为 a 或 b。

对于 1 ~ n 中的每一个 i,求在所有可能的最小生成树中 1 -> i 的最短路的最小值。

Input

第一行包含 4 个整数 n, m, a 与 b (2≤n≤70, n−1≤m≤200, 1≤a<b≤10^7)

接下来 m 行,每行三个整数 u, v, c (1≤u,v≤n, u≠v, c∈{a,b}) 描述了一条带权边 (u, v),其权值为 c。

图连通且无自环重边。

Output

输出一行 n 个整数,第 p 个描述了从 1 到 p 的可能最小值。

Examples

Input

5 5 20 25

1 2 25

2 3 25

3 4 20

4 5 20

5 1 20

Output

0 25 60 40 20

Input

6 7 13 22

1 2 13

2 3 13

1 4 22

3 4 13

4 5 13

5 6 13

6 1 13

Output

0 13 26 39 26 13

@solution@

感觉这道题有一点乱搞.jpg。

考虑 kruskal 求最小生成树的过程:把边按从小到大的顺序尝试加入。

这道题只有两种边权,所以一定是先用边权为 a 的边构成一个森林,再用边权为 b 的边连接这些森林。

一个最短路满足什么条件时不合法?

稍微动手画一画,发现边权为 a 的边并不会影响合法性(可以通过破圈法证明一条简单路径上的 a 边可以同时存在于最小生成树之中)。

不合法的情况只可能因为有些边权为 b 的边不能同时共存于最小生成树中。

什么时候边权为 b 的边不能同时共存?

再联系到我们 kruskal 的过程:假如将 a 边连接的连通块缩点,这些 b 边如果在缩点后的图上形成了环就不合法。

即我们走 b 边不能在以前经过的结点。

状压?但是这是 2^70 啊。

注意到单个结点用不着状压,那么复杂度降到 2^35。

还是很大?继续。2 个结点与 3 个结点也不需要状压:假如形成了环,则可以经过 2 条 b 边从 u -> p -> v。

然而我们内部的 a 边 <= 2 条,走这个内部的 a 边肯定比绕外部的 b 边短,所以最短路不会绕外面。

只有 >= 4 的连通块需要状压,状压部分的复杂度降成 2^17,够了。

那么再套一个 dijkstra 的复杂度,总复杂度为 O(2^(n/4)*m*log(m))。官方题解好像可以更快来着。

不过这个复杂度足够通过本题了。

@accepted code@

#include <cstdio>
#include <queue>
using namespace std;
const int MAXN = 70;
const int MAXM = 200;
const int INF = int(1E9);
struct edge{
int to, dis;
edge *nxt;
}edges[2*MAXM + 5], *adj[MAXN + 5], *ecnt = edges;
void addedge(int u, int v, int w) {
edge *p = (++ecnt);
p->to = v, p->dis = w, p->nxt = adj[u], adj[u] = p;
p = (++ecnt);
p->to = u, p->dis = w, p->nxt = adj[v], adj[v] = p;
}
struct node{
int d, s, x;
node(int _d=0, int _s=0, int _x=0) : d(_d), s(_s), x(_x) {}
friend bool operator < (node a, node b) {
return a.d > b.d;
}
};
bool vis[MAXN][1<<17];
int d[MAXN][1<<17];
priority_queue<node>que;
int dis[MAXN + 5], fa[MAXN + 5], siz[MAXN + 5];
int find(int x) {
return fa[x] = (fa[x] == x ? x : find(fa[x]));
}
void unite(int x, int y) {
int fx = find(x), fy = find(y);
if( fx != fy ) fa[fx] = fy, siz[fy] += siz[fx];
}
int id[MAXN + 5], cnt, tot;
int u[MAXM + 5], v[MAXM + 5], w[MAXM + 5];
int n, m, a, b;
int main() {
scanf("%d%d%d%d", &n, &m, &a, &b);
for(int i=0;i<n;i++) fa[i] = i, siz[i] = 1;
for(int i=1;i<=m;i++) {
scanf("%d%d%d", &u[i], &v[i], &w[i]), u[i]--, v[i]--;
if( w[i] == a )
addedge(u[i], v[i], w[i]), unite(u[i], v[i]);
}
for(int i=0;i<n;i++)
if( find(i) == i ) {
if( siz[i] >= 4 ) id[i] = (cnt++);
else id[i] = -1;
}
for(int i=0;i<n;i++)
id[i] = id[find(i)];
tot = (1<<cnt);
for(int i=0;i<n;i++) {
for(int s=0;s<tot;s++)
d[i][s] = INF;
dis[i] = INF;
}
for(int i=1;i<=m;i++)
if( w[i] == b && find(u[i]) != find(v[i]) )
addedge(u[i], v[i], w[i]);
que.push(node(0, 0, 0));
while( !que.empty() ) {
node x = que.top(); que.pop();
if( vis[x.x][x.s] ) continue;
dis[x.x] = min(dis[x.x], x.d);
vis[x.x][x.s] = true;
for(edge *p=adj[x.x];p;p=p->nxt) {
if( p->dis == a && x.d + p->dis < d[p->to][x.s] )
que.push(node(d[p->to][x.s]=x.d+p->dis, x.s, p->to));
else if( id[p->to] == -1 || !((x.s >> id[p->to]) & 1) ) {
int s = (x.s | (id[x.x] == -1 ? 0 : (1<<id[x.x])));
if( x.d + p->dis < d[p->to][s] )
que.push(node(d[p->to][s]=x.d+p->dis, s, p->to));
}
}
}
for(int i=0;i<n;i++)
printf("%d%c", dis[i], (i == n - 1 ? '\n' : ' '));
}

@details@

n <= 70 你以为是什么多项式算法。

其实这道题是一个 np 问题 2333。

@codeforces - 1149D@ Abandoning Roads的更多相关文章

  1. Codeforces 835 F. Roads in the Kingdom

    \(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...

  2. Abandoning Roads CodeForces - 1149D (最小生成树)

    大意: 给定无向图, 边权只有两种, 对于每个点$x$, 输出所有最小生成树中, 点$1$到$x$的最短距离. 先将边权为$a$的边合并, 考虑添加边权为$b$的边. 每条路径只能经过每个连通块一次, ...

  3. Codeforces Round #556 CF1149D Abandoning Roads

    这道题并不简单,要得出几个结论之后才可以做.首先就是根据Kruskal求最小生成树的过程,短边是首选的,那么对于这道题也是,我们先做一次直选短边的最小生成树这样会形成多个联通块,这些联通块内部由短边相 ...

  4. Codeforces 711D Directed Roads - 组合数学

    ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it co ...

  5. codeforces 711D Directed Roads(DFS)

    题目链接:http://codeforces.com/problemset/problem/711/D 思路:由于每个点出度都为1,所以没有复杂的环中带环.DFS遍历,若为环则有2^k-2种,若为链则 ...

  6. 【图论】Codeforces 711D Directed Roads

    题目链接: http://codeforces.com/problemset/problem/711/D 题目大意: 给一张N个点N条有向边的图,边可以逆向.问任意逆向若干条边使得这张图无环的方案数( ...

  7. [刷题]Codeforces 746G - New Roads

    Description There are n cities in Berland, each of them has a unique id - an integer from 1 to n, th ...

  8. Codeforces 671 D. Roads in Yusland

    题目描述 Mayor of Yusland just won the lottery and decided to spent money on something good for town. Fo ...

  9. Codeforces 835 F Roads in the Kingdom(树形dp)

    F. Roads in the Kingdom(树形dp) 题意: 给一张n个点n条边的无向带权图 定义不便利度为所有点对最短距离中的最大值 求出删一条边之后,保证图还连通时不便利度的最小值 $n & ...

随机推荐

  1. scikit-learn的基本使用

    在机器学习和数据挖掘的应用中,scikit-learn是一个功能强大的python包.在数据量不是过大的情况下,可以解决大部分问题.学习使用scikit-learn的过程中,我自己也在补充着机器学习和 ...

  2. Leetcode500.Keyboard Row键盘行

    给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词.键盘如下图所示. 示例: 输入: ["Hello", "Alaska", "Dad& ...

  3. Python程序的执行过程

    1. Python是一门解释型语言? 我初学Python时,听到的关于Python的第一句话就是,Python是一门解释性语言,我就这样一直相信下去,直到发现了*.pyc文件的存在.如果是解释型语言, ...

  4. BaiduTemplate [ 百度模板引擎 ]

    地址: http://baidufe.github.io/BaiduTemplate/

  5. 在VUE中实现打印

    1.安装 npm install vue-print-nb --save 2.在mian.js中引入 import Print from 'vue-print-nb' Vue.use(Print); ...

  6. web前端学习(四)JavaScript学习笔记部分(10)-- JavaScript正则表达式

    1.JavaScript正则表达式课程概要 方便查找字符串.数字.特殊字串等等 2.正则表达式的介绍 RegExp是正则表达式的缩写 当检索某个文本时,可以使用一种模式来描述要检索的内容.RegExp ...

  7. CI框架--URL路径跳转与传值

    CI框架使用URL的前提是需要加载辅助函数$this->load->helper('url');当然我建议大家将所有需要加载的东西写在构造方法内,这样就不需每个控制器每个方法都去调用一次了 ...

  8. 一致性Hash算法原理,java实现,及用途

    学习记录: 一致性Hash算法原理及java实现:https://blog.csdn.net/suifeng629/article/details/81567777 一致性Hash算法介绍,原理,及使 ...

  9. JSP/FTL 中获取param、request、session、application中的值

      Java JSP(EL表达式) FTL ① <% page.getAttribute("attr") %> ${pageScope .attr} - ② reque ...

  10. IE9没有内置鼠标手势,还要自己写

    写了个IE插件,然后获取鼠标,信息, 模拟了鼠标手势,在虚拟机里面测试,完全好使,但是现在又不敢在Win7上用了. 愁死了... 为了实现一个鼠标手势. 写的那破玩意,竟然50多K.....太大了.. ...