题目: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. 获取本机IP,返回字符串

    public static String GetLocalIp() { String[] Ips = GetLocalIpAddress(); foreach (String ip in Ips) i ...

  2. CNN中dropout层的理解

    dropout是在训练神经网络模型时,样本数据过少,防止过拟合而采用的trick.那它是怎么做到防止过拟合的呢? 首先,想象我们现在只训练一个特定的网络,当迭代次数增多的时候,可能出现网络对训练集拟合 ...

  3. jvm 内存调整

    top查看java占用的内存比较多 top - :: up days, :, user, load average: 0.03, 0.05, 0.05 Tasks: total, running, s ...

  4. python 读取位于包中的数据文件

    假设你的包中的文件组织成如下: mypackage/ __init__.py somedata.dat spam.py 现在假设spam.py文件需要读取somedata.dat文件中的内容.你可以用 ...

  5. Windows服务程序_测试01

    1. #include <stdio.h> #include <Windows.h> #include <tchar.h> #include <process ...

  6. android:点击popupwindow以外区域 popupwindow自动消失

    方法一(这种方法可以处理popupwindows dimiss的时候一些其他的操作,比如让其他控件的隐藏,消失等): 代码如下popupWindow.setFocusable(false);//foc ...

  7. codefroce385E矩阵快速幂

    状态变化  (x,y,dx,dy,i) 表示i时刻熊站在(x,y)处速度向量(dx,dy)下一个状态是 ( 2x+y+dx+i , x+2y+dy+i , x+y+dx , x+y+dy , i+1 ...

  8. Lightoj-1220

    https://vjudge.net/problem/LightOJ-1220 求x=bp中最大的p,x可能为负数. 因数分解,x=p1x1*p2x2*...*pnxn x=(p1x1'*p2x2'* ...

  9. Leetcode 52

    //N皇后的基础上改了一点class Solution { public: int totalNQueens(int n) { ; vector<); DFS(pos,,res); return ...

  10. VM虚拟机安装的XP如何全屏

    首先安装install VMwear Tools..,如图: