题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4006

用斯坦纳树求出所有关键点的各种连通情况的代价,把这个作为状压(压的是集合选择情况)的初值DP即可。

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#define pb push_back
using namespace std;
int rd()
{
int ret=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=; ch=getchar();}
while(ch>=''&&ch<='')ret=ret*+ch-'',ch=getchar();
return f?ret:-ret;
}
int const xn=,xm=,xxn=(<<)+,inf=0x3f3f3f3f;
int n,m,hd[xn],ct,to[xm],nxt[xm],w[xm],f[xn][xxn],g[xxn];
int c[],bin[],cnt;
bool vis[xn];
queue<int>q;
vector<int>v[];
int Min(int x,int y){return x<y?x:y;}
void add(int x,int y,int z){to[++ct]=y; nxt[ct]=hd[x]; hd[x]=ct; w[ct]=z;}
void spfa(int sta)
{
memset(vis,,sizeof vis);
for(int i=;i<=n;i++)if(f[i][sta]<inf)q.push(i),vis[i]=;
while(q.size())
{
int x=q.front(); q.pop(); vis[x]=;
for(int i=hd[x],u;i;i=nxt[i])
if(f[u=to[i]][sta]>f[x][sta]+w[i])
{
f[u][sta]=f[x][sta]+w[i];
if(!vis[u])vis[u]=,q.push(u);
}
}
}
int main()
{
bin[]=; for(int i=;i<=;i++)bin[i]=bin[i-]*;
n=rd(); m=rd(); int K=rd();
for(int i=,x,y,z;i<=m;i++)
x=rd(),y=rd(),z=rd(),add(x,y,z),add(y,x,z);
memset(f,0x3f,sizeof f);
for(int i=,cr,x;i<=K;i++)
{
cr=rd(); x=rd();
if(!c[cr])c[cr]=++cnt;
v[c[cr]].pb(i); f[x][bin[i-]]=;//i!
}
int mx=bin[K];
for(int sta=;sta<mx;sta++)
{
for(int i=;i<=n;i++)
for(int s=(sta&(sta-));s;s=(sta&(s-)))
f[i][sta]=Min(f[i][sta],f[i][s]+f[i][sta^s]);
spfa(sta);
}
memset(g,0x3f,sizeof g); mx=bin[cnt];
for(int sta=;sta<mx;sta++)
{
int s=;
for(int j=;j<=cnt;j++)
if(sta&bin[j-]){for(int k=;k<v[j].size();k++)s|=bin[v[j][k]-];}
for(int j=;j<=n;j++)g[sta]=Min(g[sta],f[j][s]);
}
for(int sta=;sta<mx;sta++)
for(int s=(sta&(sta-));s;s=(sta&(s-)))
g[sta]=Min(g[sta],g[s]+g[sta^s]);
printf("%d\n",g[mx-]);
return ;
}

bzoj 4006 管道连接 —— 斯坦纳树+状压DP的更多相关文章

  1. 【bzoj4006】[JLOI2015]管道连接 斯坦纳树+状压dp

    题目描述 给出一张 $n$ 个点 $m$ 条边的无向图和 $p$ 个特殊点,每个特殊点有一个颜色.要求选出若干条边,使得颜色相同的特殊点在同一个连通块内.输出最小边权和. 输入 第一行包含三个整数 n ...

  2. BZOJ4006: [JLOI2015]管道连接(斯坦纳树,状压DP)

    Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 1171  Solved: 639[Submit][Status][Discuss] Descripti ...

  3. bzoj 4006 [JLOI2015]管道连接(斯坦纳树+状压DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4006 [题意] 给定n点m边的图,连接边(u,v)需要花费w,问满足使k个点中同颜色的 ...

  4. bzoj1402 Ticket to Ride 斯坦纳树 + 状压dp

    给定\(n\)个点,\(m\)条边的带权无向图 选出一些边,使得\(4\)对点之间可达,询问权值最小为多少 \(n \leqslant 30, m \leqslant 1000\) 首先看数据范围,\ ...

  5. BZOJ2595: [Wc2008]游览计划(斯坦纳树,状压DP)

    Time Limit: 10 Sec  Memory Limit: 256 MBSec  Special JudgeSubmit: 2030  Solved: 986[Submit][Status][ ...

  6. 绿色计算大赛决赛 第二阶段 消息传递(斯坦纳树 状压dp+spfa)

    传送门 Description 作为公司老板的你手下有N个员工,其中有M个特殊员工.现在,你有一个消息需要传递给你的特殊员工.因为你的公司业务非常紧张,所以你和员工之间以及员工之间传递消息会造成损失. ...

  7. BZOJ 4006 Luogu P3264 [JLOI2015]管道连接 (斯坦纳树、状压DP)

    题目链接: (bzoj)https://www.lydsy.com/JudgeOnline/problem.php?id=4006 (luogu)https://www.luogu.org/probl ...

  8. 【BZOJ4774/4006】修路/[JLOI2015]管道连接 斯坦纳树

    [BZOJ4774]修路 Description 村子间的小路年久失修,为了保障村子之间的往来,法珞决定带领大家修路.对于边带权的无向图 G = (V, E),请选择一些边,使得1 <= i & ...

  9. BZOJ4006 JLOI2015 管道连接(斯坦纳树生成森林)

    4006: [JLOI2015]管道连接 Time Limit: 30 Sec Memory Limit: 128 MB Description 小铭铭最近进入了某情报部门,该部门正在被如何建立安全的 ...

随机推荐

  1. Learning string similarity measures for gene/protein name dictionary look-up using logistic regression

    Yoshimasa Tsuruoka1,*, John McNaught1,2, Jun’ichi Tsujii1,2,3 and Sophia Ananiadou1,2 1 School of Co ...

  2. 我的Android进阶之旅------>Android疯狂连连看游戏的实现之状态数据模型(三)

    对于游戏玩家而言,游戏界面上看到的"元素"千变万化:但是对于游戏开发者而言,游戏界面上的元素在底层都是一些数据,不同数据所绘制的图片有所差异而已.因此建立游戏的状态数据模型是实现游 ...

  3. Unicode Category

    Lu(letter,uppercase):大写字母. Ll(letter.lowercase):小写字母. Lt(letter,titlecase):词首字母大写的字母. Lm(letter,modi ...

  4. OutOfMemoryError: Java heap space和GC overhead limit exceeded在Ant的Build.xml中的通用解决方式

    这个仅仅是一点点经验,总结一下,当中前两个相应第一个Error.后两个相应第二个Error,假设heap space还不够.能够再改大些. <jvmarg value="-Xms512 ...

  5. jQuery实现复选框全选/所有取消/反选/获得选择的值

    <!DOCTYPE html> <html> <head> <script type="text/javascript" src=&quo ...

  6. ALV调用的几个函数

     转 ALV的调用主要由以下几个标准函数实现,所有函数的输入输出参数必须大写,否则系统会出现异常中止,相关函数如下: 1)REUSE_ALV_FIENDCATALOG_MERGE:根据内表结构返回FI ...

  7. 寻找第K大 网易2016实习研发工程师编程题

    有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5,2,2] ...

  8. https 请求发送 例子 tls && ssl

    package com.dooioo.training.helper; import java.io.IOException; import java.io.UnsupportedEncodingEx ...

  9. linux基础part5

    linux 基础 一.网络基础 1.ifup 网卡名称:ifdown 网卡名称:ifconfig 网卡名称 ip 子网 其修改只是临时生效,一旦关机或重启命令失效,需要修改配置文件永久生效. 2.网卡 ...

  10. Yii2 如何实现表单事件之 Ajax 提交

    前言 Yii2 现在使用 JS 都必须要注册代码了. 要实现 Ajax 提交,有两种方法.一是直接在 ActiveForm 调用 beforeSubmit 参数,但是个人认为这样没有很好的把 JS 和 ...