【bzoj4006】[JLOI2015]管道连接(斯坦纳树+dp)
题意:
给出\(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)的更多相关文章
- BZOJ4006: [JLOI2015]管道连接(斯坦纳树,状压DP)
Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 1171 Solved: 639[Submit][Status][Discuss] Descripti ...
- BZOJ4006 JLOI2015 管道连接(斯坦纳树生成森林)
4006: [JLOI2015]管道连接 Time Limit: 30 Sec Memory Limit: 128 MB Description 小铭铭最近进入了某情报部门,该部门正在被如何建立安全的 ...
- 【bzoj4006】[JLOI2015]管道连接 斯坦纳树+状压dp
题目描述 给出一张 $n$ 个点 $m$ 条边的无向图和 $p$ 个特殊点,每个特殊点有一个颜色.要求选出若干条边,使得颜色相同的特殊点在同一个连通块内.输出最小边权和. 输入 第一行包含三个整数 n ...
- BZOJ 4006 Luogu P3264 [JLOI2015]管道连接 (斯坦纳树、状压DP)
题目链接: (bzoj)https://www.lydsy.com/JudgeOnline/problem.php?id=4006 (luogu)https://www.luogu.org/probl ...
- 【BZOJ4774/4006】修路/[JLOI2015]管道连接 斯坦纳树
[BZOJ4774]修路 Description 村子间的小路年久失修,为了保障村子之间的往来,法珞决定带领大家修路.对于边带权的无向图 G = (V, E),请选择一些边,使得1 <= i & ...
- 洛谷P3264 [JLOI2015]管道连接 (斯坦纳树)
题目链接 题目大意:有一张无向图,每条边有一定的花费,给出一些点集,让你从中选出一些边,用最小的花费将每个点集内的点相互连通,可以使用点集之外的点(如果需要的话). 算是斯坦纳树的入门题吧. 什么是斯 ...
- bzoj 4006 [JLOI2015]管道连接——斯坦纳树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4006 除了模板,就是记录 ans[ s ] 表示 s 合法的最小代价.合法即保证 s 里同一 ...
- bzoj 4006 管道连接 —— 斯坦纳树+状压DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4006 用斯坦纳树求出所有关键点的各种连通情况的代价,把这个作为状压(压的是集合选择情况)的初 ...
- 【LuoguP3264】[JLOI2015] 管道连接(斯坦那树)
题目链接 题目描述 小铭铭最近进入了某情报部门,该部门正在被如何建立安全的通道连接困扰.该部门有 n 个情报站,用 1 到 n 的整数编号.给出 m 对情报站 ui;vi 和费用 wi,表示情报站 u ...
- [bzoj4006][JLOI2015]管道连接_斯坦纳树_状压dp
管道连接 bzoj-4006 JLOI-2015 题目大意:给定一张$n$个节点$m$条边的带边权无向图.并且给定$p$个重要节点,每个重要节点都有一个颜色.求一个边权和最小的边集使得颜色相同的重要节 ...
随机推荐
- Python 实现 Hangman 小游戏
Hangman--游戏简介--> 百度百科 打印Hangman def printHangman(N): # 第一行 print("\t____") # 第二行 print( ...
- Ubuntu18.04初始化
Ubuntu18.04初始化 更新源: sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak sudo gedit /etc/apt/sour ...
- Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises)
传送门 A. Optimal Currency Exchange 枚举一下就行了. Code #include <bits/stdc++.h> using namespace std; t ...
- LeetCode 5129. 下降路径最小和 II Minimum Falling Path Sum II
地址 https://leetcode-cn.com/contest/biweekly-contest-15/problems/minimum-falling-path-sum-ii/ 题目描述给你一 ...
- Java程序猿想要月薪2万+必须必备哪些技术?
现在程序员是比较紧俏的一个岗位,其实可以写代码的人许多,但是为什么程序员还那么缺呢? 除了需求大以外,还有一个原因就是,实在合格的程序员确实比较少. 想要成为一个合格的程序员,咱们需求满意以下几点要求 ...
- Python与用户交互
目录 一.为什么交互? 二.如何交互? 三.Python2的交互 一.为什么交互? 让我们来回顾计算机的发明有何意义,计算机的发明是为了奴役计算机,解放劳动力.假设我们现在写了一个ATM系统取代了 ...
- ASP.NET Core 获取主机名时的 "Decoded string is not a valid IDN name" 错误
在 ASP.NET Core 中通过 Request.Host.Host 获取主机名(hostname)时,如果主机名中包含非 ASCII 字符(比如 puny code),就会引发下面的异常: Sy ...
- PHP读取文件和目录
1:目录列表 2:文件列表
- SpringMVC方法的返回值类型和自动装配
1. void类型作为返回值类型 /** * 如果方法写成了void就跟原来servlet含义是差不多 的 * json */ @RequestMapping("/firstRequest& ...
- mysql数据库的十种查询方式及多表查询
--mysql数据库的十种查询方式 -- (1)查询时起别名 SELECT id AS '编号',NAME AS '姓名',age AS '年龄' FROM student; -- (2)查询时添加常 ...