大意: 给定无向图, 边权只有两种, 对于每个点$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. git submodule 如何push代码

    某git项目关联了一个submodule 如何更新该项目下的submodule https://stackoverflow.com/questions/5814319/git-submodule-pu ...

  2. win10 合并磁盘 disk Acronis Disk Director

    硬盘调整C盘大小的工具.zip 无损分区软件(Acronis Disk Director)下载 11.0 破解版  ADD11H_trial_en-US(11.0.0.2343).exe 注册码: E ...

  3. Mac地址转换成long长整型

    Mac地址转换成long长整型 using System;using System.Collections.Generic;using System.IO;using System.Text;usin ...

  4. MySQL格式化时间date_format

    select date_format(deal_date, '%Y年%m月%d日 %H时%i分%s秒'), date_format(deal_date, '%Y-%m-%d %H:%i:%s') fr ...

  5. NTC热敏电阻温度计算方法,Steinhart-Hart方程和B值法(转)

    NTC热敏电阻计算器使用方法 NTC热敏电阻计算器 V1.0 10K负温度系数热敏电阻(NTC)温度与阻值对应关系表 Rt = R(25℃)*EXP[B*(1/T - 1/(T+25))] 说明: 1 ...

  6. osg fbx 模型树结构

    void Test::printOsgGroup(osg::ref_ptr<osg::Group> &groupParam) { qDebug() <<groupPar ...

  7. Unicode浅析——调用科大讯飞语音合成接口(日语)所遇到的天坑

    如题,最近做的项目需要调用科大讯飞的语音合成接口,将日文合成日语.然后坑爹的是跟我对接的那一方直接扔过来一份接口文档,里面并未提及日语合成所需要的参数.中文.英文合成倒是没问题,就这个日语合成的音频始 ...

  8. 阶段5 3.微服务项目【学成在线】_day16 Spring Security Oauth2_07-SpringSecurityOauth2研究-Oauth2授权码模式-资源服务授权测试

    下面要完成  5.6两个步骤 3.3.4 资源服务授权 3.3.4.1 资源服务授权流程 资源服务拥有要访问的受保护资源,客户端携带令牌访问资源服务,如果令牌合法则可成功访问资源服务中的资 源,如下图 ...

  9. Python使用设计模式中的责任链模式与迭代器模式的示例

    Python使用设计模式中的责任链模式与迭代器模式的示例 这篇文章主要介绍了Python使用设计模式中的责任链模式与迭代器模式的示例,责任链模式与迭代器模式都可以被看作为行为型的设计模式,需要的朋友可 ...

  10. 一种可以避免数据迁移的分库分表scale-out扩容模式

    转自: http://jm.taobao.org/ 一种可以避免数据迁移的分库分表scale-out扩容方式 目前绝大多数应用采取的两种分库分表规则 mod方式 dayofweek系列日期方式(所有星 ...