BZOJ4006: [JLOI2015]管道连接(斯坦纳树,状压DP)
Time Limit: 30 Sec Memory Limit: 128 MB
Submit: 1171 Solved: 639
[Submit][Status][Discuss]
Description
小铭铭最近进入了某情报部门,该部门正在被如何建立安全的通道连接困扰。
Input
第一行包含三个整数 n;m;p,表示情报站的数量,可以建立的通道数量和重要情报站的数
Output
输出一行一个整数,表示任意相同频道的情报站之间都建立通道连接所花费的最少资源总量。
Sample Input
1 2 3
1 3 2
1 5 1
2 4 2
2 5 1
3 4 3
3 5 1
4 5 1
1 1
1 2
2 3
2 4
Sample Output
HINT
选择 (1; 5); (3; 5); (2; 5); (4; 5) 这 4 对情报站连接。
Source
斯坦纳森林
设$f[i][sta]$表示$i$号节点,与关键节点的联通性为$sta$时的最小值
假设我们已经求出了$f$
那么我们令$g[sta]$表示,颜色联通性为$sta$时的最小值
$g$比较好求,枚举子集转移就可以
$f$的话,如果学过斯坦纳树也比较好求
按照套路,两种转移方法,一种是枚举子集,一种是SPFA
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int MAXN = 1e6 + ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') {x = x * + c - ''; c = getchar();}
return x * f;
}
int N, M, P;
struct node {
int u, v, w, nxt;
}E[MAXN];
int head[MAXN], num = ;
inline void AddEdge(int x, int y,int z) {
E[num].u = x; E[num].v = y; E[num].w = z;
E[num].nxt = head[x]; head[x] = num++;
}
struct Point {
int color, ID;
}a[MAXN];
int f[][], g[];
int vis[MAXN], sum[MAXN];
queue<int>q;
void SPFA(int now) {
while(q.size() != ) {
int p = q.front(); q.pop(); vis[p] = ;
for(int i = head[p]; i != -; i = E[i].nxt) {
int v = E[i].v;
if(f[v][now] > f[p][now] + E[i].w) {
f[v][now] = f[p][now] + E[i].w;
if(!vis[v]) vis[v] = , q.push(v);
}
}
}
}
int tmp[];
bool check(int sta) {
memset(tmp, , sizeof(tmp));
for(int i = ; i <= ; i++)
if(sta & ( << i - )) tmp[a[i].color]++;
for(int i = ; i <= ; i++)
if(tmp[i] && tmp[i] != sum[i]) return ;
return ;
}
int main() {
#ifdef WIN32
freopen("a.in", "r", stdin);
#endif
memset(head, -, sizeof(head));
N = read(), M = read(), P = read();
for(int i = ; i <= M; i++) {
int x = read(), y = read(), z = read();
AddEdge(x, y, z);AddEdge(y, x, z);
}
memset(f, 0x3f, sizeof(f));
memset(g, 0x3f, sizeof(g));
for(int i = ; i <= P; i++)
a[i].color = read(), a[i].ID = read(), sum[a[i].color]++,f[a[i].ID][ << (i - )] = ;
int limit = ( << P) - ;
for(int sta = ; sta <= limit; sta++) {
for(int i = ; i <= N; i++) {
for(int S = sta; S; S = (S - ) & sta)
f[i][sta] = min(f[i][sta], f[i][S] + f[i][sta - S]);
q.push(i),vis[i] = ;
}
SPFA(sta);
}
for(int sta = ; sta <= limit; sta++)
for(int i = ; i <= N; i++)
g[sta] = min(g[sta], f[i][sta]);
for(int sta = ; sta <= limit; sta++)
if(check(sta))
for(int S = sta; S; S = (S - ) & sta)
if(check(S))
g[sta] = min(g[sta], g[S] + g[sta - S]);
printf("%d", g[( << P) - ]);
return ;
}
BZOJ4006: [JLOI2015]管道连接(斯坦纳树,状压DP)的更多相关文章
- 【bzoj4006】[JLOI2015]管道连接 斯坦纳树+状压dp
题目描述 给出一张 $n$ 个点 $m$ 条边的无向图和 $p$ 个特殊点,每个特殊点有一个颜色.要求选出若干条边,使得颜色相同的特殊点在同一个连通块内.输出最小边权和. 输入 第一行包含三个整数 n ...
- bzoj 4006 管道连接 —— 斯坦纳树+状压DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4006 用斯坦纳树求出所有关键点的各种连通情况的代价,把这个作为状压(压的是集合选择情况)的初 ...
- BZOJ4006 JLOI2015 管道连接(斯坦纳树生成森林)
4006: [JLOI2015]管道连接 Time Limit: 30 Sec Memory Limit: 128 MB Description 小铭铭最近进入了某情报部门,该部门正在被如何建立安全的 ...
- bzoj 4006 [JLOI2015]管道连接(斯坦纳树+状压DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4006 [题意] 给定n点m边的图,连接边(u,v)需要花费w,问满足使k个点中同颜色的 ...
- 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]管道连接 (斯坦纳树)
题目链接 题目大意:有一张无向图,每条边有一定的花费,给出一些点集,让你从中选出一些边,用最小的花费将每个点集内的点相互连通,可以使用点集之外的点(如果需要的话). 算是斯坦纳树的入门题吧. 什么是斯 ...
- bzoj1402 Ticket to Ride 斯坦纳树 + 状压dp
给定\(n\)个点,\(m\)条边的带权无向图 选出一些边,使得\(4\)对点之间可达,询问权值最小为多少 \(n \leqslant 30, m \leqslant 1000\) 首先看数据范围,\ ...
- BZOJ2595: [Wc2008]游览计划(斯坦纳树,状压DP)
Time Limit: 10 Sec Memory Limit: 256 MBSec Special JudgeSubmit: 2030 Solved: 986[Submit][Status][ ...
随机推荐
- Eclipse工程 导入 Android Studio
最近Eclipse好多项目转Android Studio 百度翻看好多文章 这篇不错 特纪录下 地址:http://www.cnblogs.com/bluestorm/p/3757402.html 一 ...
- 润乾在东方通tongweb5.0上部署手册
作为国内领先的中间件开发商,东方通是国内最早研究J2EE技术和开发应用服务器产品的厂商.应用服务器TongWeb的开发目标,是利用公司在中间件 领域的技术优势,实现符合J2EE规范的企业应用支撑 ...
- 如何从 GitHub 上下载单个文件夹
DownGit 好用记得回来点赞(建议***)
- onInterceptTouchEvent与onTouchEvent默认返回值
其中Layout里的onInterceptTouchEvent默认返回值是false,这样touch事件会传递到View控件,Layout里的onTouch默认返回值是false, View里的onT ...
- node和iisnode express手工安装
一.安装node.js的x86版本: 这样,node.js会安装在C:\Program Files (x86)\nodejs,这符合iisnode express7版本的期待. 二.安装iisnode ...
- 启用优酷html5播放器的办法
方法就是在浏览器中设置下sessionStorage window.sessionStorage.setItem("P_l_h5", true);
- 2.Servlet基础总结
一.简介 1.什么是Servlet Servlet(Server Applet),全称Java Servlet,未有中文译文.是用Java编写的服务器端程序.其主要功能在于交互式地浏览和修改数据,生成 ...
- 开源一款私藏Management Studio插件,ProjkyAddin,送给所有使用SQLServer的园友们
ProjkyAddin 是一款Management Studio 插件,安装包才500多kb,兼容SSMS 2005.SSMS 2008.SSMS 2008 R2.SSMS 2012.SSMS 201 ...
- C#.NET里面抽象类,接口,虚方法
1抽象类 (1) 抽象方法只作声明,而不包含实现,可以看成是没有实现体的虚方法 (2) 抽象类不能被实例化 (3) 抽象类可以但不是必须有抽象属性和抽象方法,但是一旦有了抽象方法,就一定要把这个类声明 ...
- September 27th 2017 Week 39th Wednesday
We both look up at the same stars, yet we see such different things. 我们仰望同一片星空,却看见了不同的事物. Looking up ...