大意: 给定无向图, 边权只有两种, 对于每个点$x$, 输出所有最小生成树中, 点$1$到$x$的最短距离.

先将边权为$a$的边合并, 考虑添加边权为$b$的边.

每条路径只能经过每个连通块一次, 直接状压的话有$O(n2^n)$个状态.

但是注意到点数不超过$3$的连通块内部最短路不超过$2a$, 所以求最短路时一定只经过$1$次, 所以可以不考虑.

这样总状态就为$O(n2^{\frac{n}{4}})$.

#include <iostream>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head const int N = 1e7+10;
int n, m, tot, a, b, fa[555], sz[555], id[555];
struct _ {int to,w;};
vector<_> g[555];
int Find(int x) {return fa[x]?fa[x]=Find(fa[x]):x;}
void add(int x, int y) {
x=Find(x),y=Find(y);
if (x!=y) fa[x]=y,sz[y]+=sz[x];
}
struct __ {
int id, w;
bool operator < (const __ &rhs) const {
return w>rhs.w;
}
};
priority_queue<__> q;
int dis[N], cnt;
int ID(int x, int y) {
return x*n+y;
}
pii get(int s) {
return pii((s-1)/n,(s-1)%n+1);
} int main() {
scanf("%d%d%d%d", &n, &m, &a, &b);
REP(i,1,n) sz[i]=1;
REP(i,1,m) {
int u, v, c;
scanf("%d%d%d", &u, &v, &c);
g[u].pb({v,c}),g[v].pb({u,c});
if (c==a) add(u,v);
}
REP(i,1,n) if (i==Find(i)&&sz[i]>3) {
REP(j,1,n) if (Find(j)==i) id[j] = 1<<tot;
++tot;
}
int mx = (1<<tot)-1;
memset(dis,0x3f,sizeof dis);
q.push({ID(id[1],1),dis[ID(id[1],1)]=0});
while (q.size()) {
__ t = q.top(); q.pop();
int u = t.id, w = t.w;
if (dis[u]!=w) continue;
for (auto &e:g[get(u).y]) {
int v = ID(get(u).x|id[e.to],e.to);
if (e.w==a) {
if (dis[v]>w+a) q.push({v,dis[v]=w+a});
}
else if (Find(get(u).y)!=Find(e.to)&&!(get(u).x&id[e.to])) {
if (dis[v]>w+b) q.push({v,dis[v]=w+b});
}
}
}
REP(i,1,n) {
int ans = INF;
REP(j,0,mx) ans = min(ans, dis[ID(j,i)]);
printf("%d ", ans);
} hr;
}

Abandoning Roads CodeForces - 1149D (最小生成树)的更多相关文章

  1. Fools and Roads CodeForces - 191C

    Fools and Roads CodeForces - 191C 题意:给出一棵n个节点的树,还有树上的k条简单路径(用路径的两个端点u和v表示),对于树上每一条边,求出其被多少条简单路径经过. 方 ...

  2. @codeforces - 1149D@ Abandoning Roads

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个 n 点 m 条边的无向连通图,每条边的边权为 a 或 ...

  3. CF1149D Abandoning Roads(图论,最短路,状态压缩,最小生成树)

    题目大意:$n$ 个点,$m$ 条边的无向图,边权只有两种,小的为 $a$,大的为 $b$. 对于每个点 $p$,询问在这张图所有的最小生成树上,$1$ 到 $p$ 的最短距离的最小值. $2\le ...

  4. Codeforces Round #556 CF1149D Abandoning Roads

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

  5. [Usaco2007 Dec]Building Roads 修建道路[最小生成树]

    Description Farmer John最近得到了一些新的农场,他想新修一些道路使得他的所有农场可以经过原有的或是新修的道路互达(也就是说,从任一个农场都可以经过一些首尾相连道路到达剩下的所有农 ...

  6. HDU 1102(Constructing Roads)(最小生成树之prim算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Ja ...

  7. hdu 1301 Jungle Roads krusckal,最小生成树,并查集

    The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was s ...

  8. Constructing Roads(1102 最小生成树 prim)

    Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. codeForces 472D 最小生成树

    题目大意:给出一个图中点的两两距离,问是否是一棵树,若是,求出平均边权最大的点 prim最小生成树,若原图是树,则最小生成树的距离就是原距离.否则不是. 搞出来树了,第二问随便dfs就好了. #inc ...

随机推荐

  1. Visual C++ 6.0精简绿色版下载及简单使用教程

    Visual C++ 6.0精简绿色版下载及简单使用教程 Microsoft Visual C++简介 Visual Studio 是微软公司推出的开发环境,Visual Studio 可以用来创建 ...

  2. docker-compose 布署应用nginx中的create-react-app应用获取环境变量

    文章来源:https://www.freecodecamp.org/news/how-to-implement-runtime-environment-variables-with-create-re ...

  3. Vue学习手记03-路由跳转与路由嵌套

    1.路由跳转 添加一个LearnVue.vue文件, 在router->index.js中 引入import Learn from '@/components/LearnVue' 在touter ...

  4. LiquiBase实战总结

    LiquiBase概述 Liquibase是一个用于跟踪.管理和应用数据库变化的开源的数据库重构工具.它将所有数据库的变化(包括结构和数据)都保存在XML文件中,便于版本控制. Liquibase具备 ...

  5. $createElement实现自定义弹窗

    <el-button type="text" @click="open4">点击打开 Message Box</el-button> m ...

  6. 如何交叉编译curl?

    1. 先准备一下openssl库 编译openssl库的方法在此 2. 获取curl源码 wget https://curl.haxx.se/download/curl-7.65.3.tar.gz 2 ...

  7. Docs-.NET-C#-指南-语言参考-预处理器指令:#endif(C# 参考)

    ylbtech-Docs-.NET-C#-指南-语言参考-预处理器指令:#endif(C# 参考) 1.返回顶部 1. #endif(C# 参考) 2015/07/20 #endif 指定条件指令的末 ...

  8. Ionic4.x 内置颜色

    primary/secondary/tertiary /success/warning/danger/dark/medium/light

  9. html js 遮罩层

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. 123456---com.twoapp.xiaoxiaofeixingyuan---小小飞行员

    com.twoapp.xiaoxiaofeixingyuan---小小飞行员