题目链接

题意:

给出\(n\)个点,\(m\)条边,同时给出\(p\)个重要的点以及对应特征。

现在要选出一些边,问使得这\(p\)个所有特征相同的点相连,问最小代价。

思路:

斯坦纳树的应用场景一般就为:使得一些点连通,在此基础上,允许连接一些其它的点,加入一些其它的边。可以说最小生成树是斯坦纳树的一个特例。

那么这个题首先看到要使\(p\)个点连通,那么就可以斯坦纳树搞一搞。

因为题目要求特征相同的点相连,斯坦纳树搞出来后还不够,他要求的是一个斯坦纳树森林。

我们将特征相同的所有点扣出来,然后作个子集\(dp\)就行了。

感觉这应该是一道斯坦纳树的标准题?

详见代码:

/*
* Author: heyuhhh
* Created Time: 2019/11/27 14:23:05
*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <iomanip>
#include <queue>
#include <cstdio>
#include <cstring>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << '\n'; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
#define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1005, M = 3005, P = 11; int n, m, p;
int c[P], d[P]; struct Edge {
int v, w, next;
}e[M << 1];
int head[N], tot;
void adde(int u, int v, int w) {
e[tot].v = v; e[tot].w = w; e[tot].next = head[u]; head[u] = tot++;
}
int dp[N][1 << P];
queue <int> q;
bool in[N], chk[N];
void spfa(int s) {
while(!q.empty()) {
int u = q.front(); q.pop(); in[u] = 0;
for(int i = head[u]; i != -1; i = e[i].next) {
int v = e[i].v;
if(dp[v][s] > dp[u][s] + e[i].w) {
dp[v][s] = dp[u][s] + e[i].w;
if(!in[v]) q.push(v), in[v] = 1;
}
}
}
}
int g[1 << P], sum[P], tmp[P];
bool ok(int s) {
memset(tmp, 0, sizeof(tmp));
for(int i = 1; i <= p; i++) if((s >> (i - 1)) & 1) ++tmp[d[i]];
for(int i = 1; i <= p; i++) if(tmp[d[i]] && tmp[d[i]] != sum[d[i]]) return false;
return true;
}
void run(){
memset(head, -1, sizeof(head)); tot = 0;
for(int i = 1; i <= m; i++) {
int u, v, w; cin >> u >> v >> w;
adde(u, v, w); adde(v, u, w);
}
memset(dp, INF, sizeof(dp));
memset(g, INF, sizeof(g));
for(int i = 1; i <= p; i++) {
cin >> d[i] >> c[i];
dp[c[i]][1 << (i - 1)] = 0;
++sum[d[i]];
}
int lim = (1 << p);
for(int S = 1; S < lim; S++) {
for(int i = 1; i <= n; i++) {
for(int s = (S - 1) & S; s; s = (s - 1) & S) {
dp[i][S] = min(dp[i][S], dp[i][s] + dp[i][S - s]);
}
if(dp[i][S] < INF) q.push(i), in[i] = 1;
}
spfa(S);
for(int i = 1; i <= n; i++) g[S] = min(g[S], dp[i][S]);
}
for(int i = 1; i < lim; i++) if(ok(i)) {
for(int j = i; j; j = (j - 1) & i) if(ok(j)){
g[i] = min(g[i], g[j] + g[i ^ j]);
}
}
cout << g[lim - 1] << '\n';
} int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
while(cin >> n >> m >> p) run();
return 0;
}

【bzoj4006】[JLOI2015]管道连接(斯坦纳树+dp)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. bzoj 4006 [JLOI2015]管道连接——斯坦纳树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4006 除了模板,就是记录 ans[ s ] 表示 s 合法的最小代价.合法即保证 s 里同一 ...

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

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

  9. 【LuoguP3264】[JLOI2015] 管道连接(斯坦那树)

    题目链接 题目描述 小铭铭最近进入了某情报部门,该部门正在被如何建立安全的通道连接困扰.该部门有 n 个情报站,用 1 到 n 的整数编号.给出 m 对情报站 ui;vi 和费用 wi,表示情报站 u ...

  10. [bzoj4006][JLOI2015]管道连接_斯坦纳树_状压dp

    管道连接 bzoj-4006 JLOI-2015 题目大意:给定一张$n$个节点$m$条边的带边权无向图.并且给定$p$个重要节点,每个重要节点都有一个颜色.求一个边权和最小的边集使得颜色相同的重要节 ...

随机推荐

  1. add to explorer context menu需不需要勾选

    添加到鼠标右键菜单,添加以后,可以直接右键文件,直接选择这个软件打开选择的文件,建议勾选

  2. 添加数据库数据后(SVN的更新和启动)、SVN启动

    一.添加数据库数据: 1.修改Excel表格,添加字段 2.修改数据库,添加字段 3.修改程序 4.使用Excel生成Tabletotable文件,并修改生成文件的内容. (1)删除cpp文件所有的H ...

  3. MATLAB实例:为匹配真实标签,对训练得到的标签进行调整

    MATLAB实例:为匹配真实标签,对训练得到的标签进行调整 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 1. MATLAB程序 munkres.m ...

  4. 实操《kubernetes网络权威指南》之tun设备

    跟着网上作一次,OK的. tun.c #include <net/if.h> #include <sys/ioctl.h> #include <sys/stat.h> ...

  5. layUI学习第四日:layUI布局系列一

    1.栅格布局规则 1.1 layui-row定义行,如:<div class="layui-row"></div> 1.2 layui-col-md*这样的 ...

  6. 2019年最新50道java基础部分面试题(三)

    前21题请看之前的随笔 22.面向对象的特征有哪些方面 计算机软件系统是现实生活中的业务在计算机中的映射,而现实生活中的业务其实就是一个个对象协作的过程.面向对象编程就是按现实业务一样的方式将程序代码 ...

  7. 设计模式-Adapter(结构型模式)针对第三方库 函数 接口不匹配的问题,分为类模式 与 对象模式 两种

    以下代码来源: 设计模式精解-GoF 23种设计模式解析附C++实现源码 //****************************类模式的Adaptr*********************** ...

  8. subprocess模块(了解)

    目录 一.subprocess模块 一.subprocess模块 subprocess模块允许你去创建一个新的进程让其执行另外的程序,并与它进行通信,获取标准的输入.标准输出.标准错误以及返回码等.更 ...

  9. 【linux运维】linux系统上忘记密码如何操作

    目录 红帽系统忘记密码操作 红帽系统设置ip,主机名 centos系统忘记密码操作 一.红帽系统忘记密码操作 1)再出现以下界面后按键盘e键 2)进入以下界面后,再次按e键 3)使用上下键选择第2项或 ...

  10. app自动化测试初体验

    一.appium环境开启 1.开启MUMU模拟器/真机(真机需要开启“USB开发调试模式”),使用命令行adb devices检查设备是否正常连接 2.开启appium 双击运行appium应用 正常 ...