HDU 4085 Steiner树模板称号
Dig The Wells
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 971 Accepted Submission(s): 416
will cost money. They do not want to cost too much money. Now they want you to find a cheapest plan.
Each test case will starts with three numbers n , m, and p in one line, n stands for the number of monks and m stands for the number of places that can been used, p stands for the number of roads between these places. The places the monks stay is signed from
1 to n then the other m places are signed as n + 1 to n + m. (1 <= n <= 5, 0 <= m <= 1000, 0 <=p <= 5000)
Then n + m numbers followed which stands for the value of digging a well in the ith place.
Then p lines followed. Each line will contains three numbers a, b, and c. means build a road between a and b will cost c.
3 1 3
1 2 3 4
1 4 2
2 4 2
3 4 4
4 1 4
5 5 5 5 1
1 5 1
2 5 1
3 5 1
4 5 1
6
5
题意:有n个和尚。每个和尚一个庙,有m个村庄,p条路。每条路有费用,每个地方打井也须要费用,求最少花多少钱能够使得全部和尚喝上水。
斯坦纳树比較裸的问题。
代码:
/* ***********************************************
Author :rabbit
Created Time :2014/7/17 0:59:57
File Name :13.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 100000000
#define eps 1e-8
#define pi acosi
typedef long long ll;
int head[1100],tol;
struct Edge{
int next,to,val;
}edge[1001000];
void addedge(int u,int v,int w){
edge[tol].to=v;
edge[tol].next=head[u];
edge[tol].val=w;
head[u]=tol++;
}
int weight[1100],d[1100][1<<5],dp[1100],in[1010][1<<5];
int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
int n,m,p;
while(~scanf("%d%d%d",&n,&m,&p)){
memset(head,-1,sizeof(head));tol=0;
for(int i=0;i<n+m;i++)
scanf("%d",&weight[i]);
while(p--){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
u--;v--;
addedge(u,v,w);
addedge(v,u,w);
}
for(int i=0;i<n+m;i++)
for(int j=0;j<(1<<n);j++)
d[i][j]=INF;
for(int i=0;i<(1<<n);i++)dp[i]=INF;
memset(in,0,sizeof(in));
for(int i=0;i<n;i++)
d[i][1<<i]=weight[i];
for(int i=1;i<(1<<n);i++){
queue<int> Q;
for(int j=0;j<n+m;j++){
for(int k=i&(i-1);k;k=(k-1)&i)
d[j][i]=min(d[j][i],d[j][i-k]+d[j][k]-weight[j]);
if(d[j][i]<INF)Q.push(100000*j+i),in[j][i]=1;
}
while(!Q.empty()){
int v=Q.front()/100000,t=Q.front()%100000;
Q.pop();
in[v][t]=0;
for(int e=head[v];e!=-1;e=edge[e].next){
int s=edge[e].to;
if(d[s][t]>d[v][t]+edge[e].val+weight[s]-weight[v]){
d[s][t]=d[v][t]+edge[e].val+weight[s]-weight[v];
if(!in[s][t]){
in[s][t]=1;
Q.push(100000*s+t);
}
}
}
}
}
for(int i=1;i<(1<<n);i++)
for(int j=0;j<n+m;j++)
dp[i]=min(dp[i],d[j][i]);
for(int i=1;i<(1<<n);i++){
for(int j=i&(i-1);j;j=i&(j-1))
dp[i]=min(dp[i],dp[j]+dp[i-j]);
}
cout<<dp[(1<<n)-1]<<endl;
}
return 0;
}
HDU 4085 Steiner树模板称号的更多相关文章
- HDU 4085 Steiner树
主题链接:pid=4085">http://acm.hdu.edu.cn/showproblem.php? pid=4085 由于这题专门花一晚上学习斯坦纳树.找到比較好的学习资料,链 ...
- HDU 1251 Trie树模板题
1.HDU 1251 统计难题 Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #incl ...
- HDU 1166 线段树模板&树状数组模板
HDU1166 上好的线段树模板&&树状数组模板 自己写的第一棵线段树&第一棵树状数组 莫名的兴奋 线段树: #include <cstdio> using nam ...
- HDU - 1251 字典树模板题
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input输入数据的第一部 ...
- HDU(1166),线段树模板,单点更新,区间总和
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 第一次做线段树,帆哥的一句话,我记下来了,其实,线段树就是一种处理数据查询和更新的手段. 然后, ...
- hdu 2072(字典树模板,set,map均可做)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=2072 lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词 ...
- hdu 1754 线段树模板题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 #include <cstdio> #include <cmath> # ...
- hdu 1251 字典树模板题 ---多串 查找单词出现次数
这道题题目里没有给定数据范围 我开了2005 疯狂的WA 然后开了50000, A掉 我以为自己模板理解错 然后一天没吃饭,饿得胃疼还是想着把这题A掉再去吃,谁知竟然是这样的问题,,,呵呵~~~ ...
- hdu 2665 划分树模板题(可作为模板)
Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
随机推荐
- 简单ESB的服务架构
简单ESB的服务架构 这几个月一直在修改架构,所以迟迟没有更新博客. 新的架构是一个基于简单esb的服务架构,主要构成是esb服务注册,wcf服务,MVC项目构成. 首先,我门来看一看解决方案, 1. ...
- sql语句中单引号嵌套问题
在sql语句中,我们难免会用到单引号嵌套的时候,但是直接嵌套肯定是不行的,java中用反斜杠做转义符也是不行的,在sql中是用单引号来做转义符的. 比如下面例子是存储过程里查询时的语句示例 exec ...
- Scripting Java #3:Groovy与invokedynamic
只需看看今天Groovy语言实现机制.在此之前,是第一个推倒静态类型与动态类型语言在实现上面的一些差异. 静态类型 vs. 动态类型 看以下这个简单的栗子. def addtwo(a, b) { re ...
- 给Ubuntu安装KDE桌面 [转]
自ubuntu5.10发布以来,我非常难得的让她在我的硬盘上顽强生存了近3个月,仔细想想就连当年我第一个接触的红旗Linux也不过坚持了一个月,呵呵,又写了一段废话── 所 谓的“重回Kununtu” ...
- C# WinForm多线程(三)Control.Invoke
下面我们就把在Windows Form软件中使用Invoke时的多线程要注意的问题给大家做一个介绍. 首先,什么样的操作需要考虑使用多线程?总的一条就是,负责与用户交互的线程(以下简称为UI线程)应该 ...
- 传智播客成都校园php纪律指控
继传智播客成都校区php第一期班圆满开班,说明php的火爆一点儿也不亚于java! 经传智播客商讨决定,传智播客成都校区php学科收费标准例如以下: 採用下面不论什么一种方式都能够享受优惠价: 一.自 ...
- JavaScript概要
当用户填写表单在线,用户输入表单经常要推测在页面上,它提示用户填写正确的邮箱.哪些项目不填之类,这些都是JavaScript小的特点. JavaScript是一种基于对象的脚本语言,使用它能够开 ...
- 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern)
原文:乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) 作者:webabc ...
- BZOJ 1052 HAOI2007 覆盖问题 二分法答案+DFS
标题效果:特定n点.涵盖所有的点与同方三面.斧头要求方垂直边界,最小平方的需求方长值 最大值至少.答案是很明显的二分法 但验证是一个问题 考虑仅仅有三个正方形,故用一个最小矩形覆盖这三个正方形时至少有 ...
- TCP连接建立过程中为什么需要“三次握手”(转)
传输控制协议(Transmission Control Protocol, TCP)是一种面向连接的.可靠的.基于字节流的运输层(Transport layer)通信协议.是专门为了在不可靠的互联网络 ...