BZOJ 4006 [JLOI2015]管道连接(斯坦纳树+子集DP)
明显是一道斯坦纳树的题。
然而这题只需要属性相同的点互相连接。
我们还是照常先套路求出\(ans[s]\)。
然后对\(ans[s]\)做子集DP即可。
具体看代码。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
const int INF=1e9;
const int N=1111;
const int M=3333;
struct edge{
int to,nxt,w;
}e[M*2];
int cnt,head[N];
void add_edge(int u,int v,int w){
cnt++;
e[cnt].nxt=head[u];
e[cnt].to=v;
e[cnt].w=w;
head[u]=cnt;
}
struct node{
int id,w;
node(int idd=0,int ww=0){
id=idd,w=ww;
}
};
struct point{
int k,id;
}c[N];
bool operator <(node a,node b){
return a.w>b.w;
}
priority_queue<node> q;
bool vis[N];
int dp[N][N];
void dij(int x){
memset(vis,0,sizeof(vis));
while(!q.empty()){
node uu=q.top();
q.pop();
int u=uu.id;
if(vis[u])continue;vis[u]=1;
for(int i=head[u];i;i=e[i].nxt){
int v=e[i].to;
if(dp[x][v]>dp[x][u]+e[i].w){
dp[x][v]=dp[x][u]+e[i].w;
q.push(node(v,dp[x][v]));
}
}
}
}
int a[15],num[15];
int n,m,k,tot,ans[N];
bool check(int s){
for (int i=1;i<=k;++i) a[i]=0;
for (int i=1;i<=k;++i)
if (s&(1<<(i-1))) a[c[i].k]++;
for (int i=1;i<=k;++i) if (a[i]&&a[i]!=num[i]) return false;
return true;
}
int read(){
int sum=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){sum=sum*10+ch-'0';ch=getchar();}
return sum*f;
}
int main(){
n=read(),m=read(),k=read();
for(int i=1;i<=m;i++){
int u=read(),v=read(),w=read();
add_edge(u,v,w);add_edge(v,u,w);
}
tot=(1<<k)-1;
for(int i=0;i<=tot;i++)
for(int j=1;j<=n;j++)
dp[i][j]=INF;
for(int i=1;i<=k;i++){
c[i].k=read(),c[i].id=read();
num[c[i].k]++;
}
for(int i=1;i<=k;i++)dp[1<<i-1][c[i].id]=0;
for(int t=0;t<=tot;t++){
for(int i=1;i<=n;i++)
for(int j=t;j;j=(j-1)&t)
dp[t][i]=min(dp[t][i],dp[j][i]+dp[t^j][i]);
for(int i=1;i<=n;i++)if(dp[t][i]!=INF)q.push(node(i,dp[t][i]));
dij(t);
}
for(int i=0;i<=tot;i++){
ans[i]=INF;
for(int j=1;j<=n;j++)
ans[i]=min(ans[i],dp[i][j]);
}
for(int i=0;i<=tot;i++)
if(check(i))
for(int j=i;j;j=(j-1)&i)
if(check(j))
ans[i]=min(ans[i],ans[j]+ans[j^i]);
printf("%d",ans[tot]);
return 0;
}
BZOJ 4006 [JLOI2015]管道连接(斯坦纳树+子集DP)的更多相关文章
- bzoj 4006 [JLOI2015]管道连接——斯坦纳树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4006 除了模板,就是记录 ans[ s ] 表示 s 合法的最小代价.合法即保证 s 里同一 ...
- BZOJ4006 JLOI2015 管道连接(斯坦纳树生成森林)
4006: [JLOI2015]管道连接 Time Limit: 30 Sec Memory Limit: 128 MB Description 小铭铭最近进入了某情报部门,该部门正在被如何建立安全的 ...
- 洛谷P3264 [JLOI2015]管道连接 (斯坦纳树)
题目链接 题目大意:有一张无向图,每条边有一定的花费,给出一些点集,让你从中选出一些边,用最小的花费将每个点集内的点相互连通,可以使用点集之外的点(如果需要的话). 算是斯坦纳树的入门题吧. 什么是斯 ...
- 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 & ...
- BZOJ4006: [JLOI2015]管道连接(斯坦纳树,状压DP)
Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 1171 Solved: 639[Submit][Status][Discuss] Descripti ...
- 【bzoj4006】[JLOI2015]管道连接 斯坦纳树+状压dp
题目描述 给出一张 $n$ 个点 $m$ 条边的无向图和 $p$ 个特殊点,每个特殊点有一个颜色.要求选出若干条边,使得颜色相同的特殊点在同一个连通块内.输出最小边权和. 输入 第一行包含三个整数 n ...
- bzoj 4006 [JLOI2015]管道连接(斯坦纳树+状压DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4006 [题意] 给定n点m边的图,连接边(u,v)需要花费w,问满足使k个点中同颜色的 ...
- bzoj 4006 管道连接 —— 斯坦纳树+状压DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4006 用斯坦纳树求出所有关键点的各种连通情况的代价,把这个作为状压(压的是集合选择情况)的初 ...
随机推荐
- [luogu3952 noip2017] 逛公园 (计数dp+最短路)
传送门 Description Input Output 输出文件包含 T 行,每行一个整数代表答案. Sample Input 2 5 7 2 10 1 2 1 2 4 0 4 5 2 2 3 2 ...
- [BZOJ1999] 树网的核 [数据加强版] (树的直径)
传送门 如果只是想验证算法正确性这里是洛谷数据未加强版 Description 设T=(V, E, W) 是一个无圈且连通的无向图(也称为无根树),每条边带有正整数的权,我们称T为树网(treenet ...
- Problem 1
Problem 1 # Problem_1.py """ If we list all the natural numbers below 10 that are mul ...
- vue 如何清除定时器
在页面中需要定时刷新局部数据,在数据变化是否频繁的情况下,没有必要使用webSocket,因为数据变化频繁,数据实时变化太快看不清楚.因此页面会定时调用后台接口以达到实时刷新数据的效果. 1.在dat ...
- Windows使用docker打开新窗口error解决办法
环境 win7 Error: error during connect: Get http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.26/containers/json ...
- uni-app 之验证码
手机APP---验证码 最近公司在开发手机APP,app避不可免的就是登录了,emmmm 登录验证码那必须的是有的,我们公司发给我们的图片是酱紫的~~ 这个要求大家应该都能看懂,做这个手机号啊,验证码 ...
- ActiveMQ 整合 spring
一.添加 jar 包 <dependency> <groupId>org.apache.activemq</groupId> <artifactId>a ...
- 启动 Appium 自带模拟器
1.先在sclipse中新建并打开一个设备 2.启动appium 3.安装apk 打开cmd 并在sdk安装目录的tools文件夹下输入安装命令adb install xxx.apk(在这之前需要把 ...
- 0111MySQL优化的奇技淫巧之STRAIGHT_JOIN
转自博客http://huoding.com/2013/06/04/261 问题 通过「SHOW FULL PROCESSLIST」语句很容易就能查到问题SQL,如下: SELECT post.* F ...
- Chrome下使用百度地图报错Cannot read property 'minZoom' of undefined
问题:工作中在Google chome下面的js console里面测试百度地图API var map = new BMap.Map("container"); map.cente ...