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

除了模板,就是记录 ans[ s ] 表示 s 合法的最小代价。合法即保证 s 里同一种的连通,整体可以不连通。ans[  ] 也枚举子集转移即可,初值就是模板算出来的 dp[ ][ s ] 最小值。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int rdn()
{
int ret=;bool fx=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')fx=;ch=getchar();}
while(ch>=''&&ch<='')ret=ret*+ch-'',ch=getchar();
return fx?ret:-ret;
}
int Mn(int a,int b){return a<b?a:b;}
int Mx(int a,int b){return a>b?a:b;}
const int N=,M=,K=,Lm=(<<)+,INF=6e7+;
int n,m,cnt,hd[N],xnt,to[M<<],nxt[M<<],w[M<<];
int col[K],dy[K],tot,ct[K],bin[K];
int dp[N][Lm],ans[Lm];bool b[Lm],ins[N];
queue<int> q;
void add(int x,int y,int z){to[++xnt]=y;nxt[xnt]=hd[x];hd[x]=xnt;w[xnt]=z;}
void b_init()
{
for(int s=;s<bin[cnt];s++)
{
int tct[K]={};
for(int i=;i<cnt;i++)
if(s&bin[i])tct[col[i+]]++;
b[s]=;
for(int i=;i<=tot;i++)if(tct[i]&&tct[i]<ct[i]){b[s]=;break;}
}
}
void spfa(int s)
{
for(int i=;i<=n;i++)q.push(i),ins[i]=;
while(q.size())
{
int k=q.front();q.pop();ins[k]=;
for(int i=hd[k],v;i;i=nxt[i])
if(dp[v=to[i]][s]>dp[k][s]+w[i])
{
dp[v][s]=dp[k][s]+w[i];
if(!ins[v])q.push(v),ins[v]=;
}
}
}
int main()
{
n=rdn();m=rdn();cnt=rdn();
for(int i=,u,v,z;i<=m;i++)
u=rdn(),v=rdn(),z=rdn(),add(u,v,z),add(v,u,z);
memset(dp,0x3f,sizeof dp);
bin[]=;for(int i=;i<=cnt;i++)bin[i]=bin[i-]<<;
for(int i=;i<=cnt;i++)
{
int c=rdn();int d=rdn();
if(!dy[c])dy[c]=++tot; col[i]=dy[c]; ct[col[i]]++;
dp[d][bin[i-]]=;//[d]not[i]
}
b_init(); memset(ans,0x3f,sizeof ans);
for(int s=;s<bin[cnt];s++)
{
for(int i=;i<=n;i++)
for(int d=(s-)&s;d;d=(d-)&s)
dp[i][s]=Mn(dp[i][s],dp[i][d]+dp[i][s^d]);
spfa(s);
for(int i=;i<=n;i++)ans[s]=Mn(ans[s],dp[i][s]);
}
for(int s=;s<bin[cnt];s++)
{
if(!b[s])continue;
for(int d=(s-)&s;d;d=(d-)&s)
if(b[d])ans[s]=Mn(ans[s],ans[d]+ans[s^d]);
}
printf("%d\n",ans[bin[cnt]-]);
return ;
}

bzoj 4006 [JLOI2015]管道连接——斯坦纳树的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. 洛谷P3264 [JLOI2015]管道连接 (斯坦纳树)

    题目链接 题目大意:有一张无向图,每条边有一定的花费,给出一些点集,让你从中选出一些边,用最小的花费将每个点集内的点相互连通,可以使用点集之外的点(如果需要的话). 算是斯坦纳树的入门题吧. 什么是斯 ...

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

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

  8. BZOJ 4006 [JLOI2015]管道连接(斯坦纳树+子集DP)

    明显是一道斯坦纳树的题. 然而这题只需要属性相同的点互相连接. 我们还是照常先套路求出\(ans[s]\). 然后对\(ans[s]\)做子集DP即可. 具体看代码. #include<iost ...

  9. bzoj 4006 管道连接 —— 斯坦纳树+状压DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4006 用斯坦纳树求出所有关键点的各种连通情况的代价,把这个作为状压(压的是集合选择情况)的初 ...

随机推荐

  1. P1879 [USACO06NOV]玉米田Corn Fields(状压dp)

    P1879 [USACO06NOV]玉米田Corn Fields 状压dp水题 看到$n,m<=12$,肯定是状压鸭 先筛去所有不合法状态,蓝后用可行的状态跑一次dp就ok了 #include& ...

  2. 2017 java期末上机练习

    仅供参考! 一.最大值.最小值.平均数 package examination; import java.util.Arrays; import java.util.Scanner; /** * 1. ...

  3. What's the difference between SDK and Runtime in .NET Core?

    What's the difference between SDK and Runtime in .NET Core? Answer1 According to the .Net Core Guide ...

  4. 【重新挂载磁盘空间】Linux系统/home的磁盘空间重新挂载给/root

    以下是在centos7版本上做测试 使用如下命令查看磁盘使用情况 ls -lh 文件系统 容量 已用 可用 已用% 挂载点 devtmpfs 3.9G 0 3.9G 0% /dev tmpfs 3.9 ...

  5. 嵌入式Linux应用程序开发环境搭建记录

    2016年2月 参考资料: OK210软件手册(Linux版).pdf Ubuntu下Qt4.7.1编译环境配置说明.pdf 我阅读了以下内容: OK210软件手册(Linux版).pdf 第七章 O ...

  6. 线程池ThreadPoolExecutor里面4种拒绝策略

    ThreadPoolExecutor类实现了ExecutorService接口和Executor接口,可以设置线程池corePoolSize,最大线程池大小,AliveTime,拒绝策略等.常用构造方 ...

  7. java中interrupt、join、sleep、notify、notifyAll、wait详解

    首先介绍一下中断概念:举个例子容易理解一点 例子:假如你正在给朋友写信,电话铃响了.这时,你放下手中的笔,去接电话.通话完毕,再继续写信.这个例子就表现了中断及其处理过程:电话铃声使你暂时中止当前的工 ...

  8. Java网络编程学习A轮_04_TCP连接异常

    参考资料: https://huoding.com/2016/01/19/488 示例代码: https://github.com/gordonklg/study,socket module A. C ...

  9. Idea使用(摘抄至java后端技术公众号-孤独烟)

    1. idea自动编译需要手动开启: 2. 手动去掉idea自动提示时候不区分字母大小写 3. idea自动导入包 4. 悬浮开关提示:鼠标放上去就给出提示 5. 打开的所有类tabs换行显示,不单行 ...

  10. Mongo Plugin插件(编辑器PyCharm的Mongo插件安装与使用)

    博主接触到MongoDB数据库.用普通的Navicat工具 是不支持的 正准备重新安装一款对应的可视化工具.刚好发现在PyCharm编辑中有连接mongoDB数据的插件 Mongo Plugin 这里 ...