您的位置:首页 > 其它

Hdu 3311 Dig The Wells (综合_斯坦纳树)

2013-08-14 21:30 330 查看




Zoj 3613 Wormhole Transport (综合_斯坦纳树)

分类: 全部博客 ACM_动态规划(DP) ACM_数据结构 ACM_图论系列2012-09-05
00:16 462人阅读 评论(0) 收藏 举报

treestructnulloutputinputc

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3613

题目大意: 给定一张图有n个顶点,某些点有工厂,某些点有资源,每个资源可以供应给一个工厂,有m条无向边,边有边权。已知有资源的点不超过4个,有工厂的点也不超过4个,问选择若干条边如何使有资源工厂的工厂最多,当数量一样多时输出最小权值。n <= 200, m <= 5000.

解题思路: 很一般的斯坦纳树。这类问题模型一般是固定的,就比如这题和Hdu 4085就十分相像,但这题会略微复杂点。首先是找有工厂和有资源的点然后先存起来,k = 工厂点数+资源点数,总状态为1<<k.然后这题的工厂数应该要大等于资源数,这个很显然,这样的话判断转改合法不合法就要判断着两个数量的关系了。

测试数据:

Input:

2

0 1

1 0

1

1 2 3

2

1 1

1 1

1

1 2 3

4

2 1

1 0

0 1

1 1

3

1 2 3

2 3 4

3 4 4

4

0 1

0 1

1 0

1 0

2

1 3 1

2 4 1

OutPut:

1 3

2 0

3 4

2 2

C艹代码:

[b][cpp]
view
plaincopy

#include <stdio.h>

#include <string.h>

#include <queue>

using namespace std;

#define MIN 1000

#define MAX 5100

#define INF (1<<29)

#define min(a,b) ((a)<(b)?(a):(b))

struct planet{

int a,b;

}arr[MIN];

struct node {

int v,len;

node *next;

}*head[MAX*2],tree[MAX*2];

queue<int> qu;

bool in[MIN][MIN];

int k,res[MIN],tot,fac[MIN];

int n,m,ans,ansi,ptr,st[MIN],nn;

int orik,cost[MIN][MIN],dp[MIN];

void Initial() {

int i,j,t;

ptr = 0,orik = k;

memset(st,0,sizeof(st));

memset(in,false,sizeof(in));

memset(head,NULL,sizeof(head));

for (i = 0; i < tot; ++i)

res[k++] = fac[i];

nn = 1 << k;

for (i = 0; i < nn; ++i)

for (j = 0; j < n; ++j)

cost[j][i] = INF;

for (i = 0; i < k; ++i)

st[res[i]] = 1<<i,cost[res[i]][st[res[i]]] = 0;

}

void AddEdge(int a,int b,int c) {

tree[ptr].v = b,tree[ptr].len = c;

tree[ptr].next = head[a],head[a] = &tree[ptr++];

}

void Spfa() {

while (!qu.empty()) {

int j = qu.front() / MAX;

int i = qu.front() % MAX;

qu.pop(),in[j][i] = false;

node *p = head[j];

while (p != NULL) {

int v = p->v,nst = i | st[v];

if (cost[j][i] + p->len < cost[v][nst]) {

cost[v][nst] = cost[j][i] + p->len;

if (nst == i && !in[v][nst])

qu.push(v*MAX+nst),in[v][nst] = true;

}

p = p->next;

}

}

}

void Steiner_Tree() {

int i,j,t,s;

for (i = 0; i < nn; ++i) {

for (j = 0; j < n; ++j) {

if (st[j] && !(st[j] & i)) continue;

for (t = (i-1)&i; t; t = (t-1)& i)

cost[j][i] = min(cost[j][i],cost[j][t|st[j]]+cost[j][(i-t)|st[j]]);

if (cost[j][i] != INF) qu.push(j*MAX+i),in[j][i] = true;

}

Spfa();

}

}

int Check(int st) {

int i,cnt = 0;

for (i = 0; i < orik; ++i)

if (st & (1<<i)) cnt++;

for (i = orik; i < k; ++i)

if (st & (1<<i)) cnt -= arr[fac[i-orik]].a;

return cnt <= 0;

}

void Solve_DP() {

int i,j,t;

for (i = 0; i < nn; ++i) {

dp[i] = INF;

for (j = 0; j < n; ++j)

dp[i] = min(dp[i],cost[j][i]);

}

for (i = 0; i < nn; ++i)

if (Check(i))for (t = (i-1)&i; t; t = (t-1)&i)

if (Check(t)&&Check(i-t)) dp[i] = min(dp[i],dp[t]+dp[i-t]);

for (i = 0; i < nn; ++i)

if (dp[i] != INF && Check(i)){

int cnt = 0;

for (j = 0; j < orik; ++j)

if (i & (1<<j)) cnt++;

if (cnt > ans) ans = cnt,ansi = dp[i];

else if (cnt == ans && ansi > dp[i]) ansi = dp[i];

}

}

int main()

{

int i,j,a,b,c,num;

while (scanf("%d",&n) != EOF) {

ans = ansi = 0;

num = k = tot = 0;

for (i = 0; i < n; ++i) {

scanf("%d%d",&arr[i].a,&arr[i].b);

if (arr[i].a && arr[i].b)

num++,arr[i].a--,arr[i].b--;

if (arr[i].a) fac[tot++] = i;

if (arr[i].b) res[k++] = i;

}

Initial();

scanf("%d",&m);

for (i = 0; i < m; ++i) {

scanf("%d%d%d",&a,&b,&c);

a--,b--;

AddEdge(a,b,c),AddEdge(b,a,c);

}

Steiner_Tree();

Solve_DP();

printf("%d %d\n",ans+num,ansi);

}

}

[/b]

分类: 全部博客 ACM_动态规划(DP) ACM_数据结构 ACM_图论系列2012-09-08
23:53 517人阅读 评论(0) 收藏 举报

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3311

题目大意:n个和尚要去挑水,他们可以再他们所在的地方挑水,也可以再其他m个地方挑水,挑水之前需要挖井,在n+m个地方挖井对应着n+m个费用,然后给定p条边,表示两个地方可以互达,边有边权表示需要的费用。

解题思路: 因为必须覆盖n个和尚,那么便是求一颗包含前n个点的斯坦纳树。

一开始我的做法是将每个点挖井的费用表示在cost[i][st[i]]里,即以i为根并在i挖井的费用,写着写着就蛋疼了写不下去,点的费用会被重复计算好多次,这不是坑爹吗,然后就困了。睡一觉醒来后灵感大爆发,TMD的加个点0,然后连1条边到n+m的点,边权为点权,这样问题就转换成了:求覆盖点0,点1...点n的斯坦纳树,不需考虑点权了,也不会重复计算了。

按上面的思路套个斯坦纳树模版,发现结果全为0.在求解dp[i]的时候,我没有加一些就进行dp[i] = min(dp[i],dp[k]+dp[i-k])。这样结果当然都为0,如dp[7] = dp[4] + dp[2] + dp[1],dp[1] = cost[0][1] = 0,dp[2]=cost[1][2] = 0,dp[4] = cost[2][4] = 0,这样的话一个集合就由若干个单点集合组合,费用都为0.其实这样忽视了一个条件,我们增加的点0是必须和1.2..n绑定在一起的,也就是说必须判断某个集合里是否为点0在。

其实,ans = min(cost[i][(1<<(n+1))-1])(i<=i<=n+m),因为这样的集合包含了所有点,并且这只会是一棵树,也就是答案了。

测试数据:

InPut:

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

OutPut:

6

5

代码:

[cpp] view
plaincopy

#include <stdio.h>

#include <string.h>

#include <queue>

using namespace std;

#define MIN (1<<6)

#define MAX 1100

#define INF (1<<29)

#define min(a,b) ((a)<(b)?(a):(b))

struct node {

int v,len;

node *next;

}*head[MAX],tree[MAX*11];

queue<int> qu;

bool in[MAX][MIN];

int n,m,p,nn,ptr,ans,st[MAX];

int well[MAX],cost[MAX][MIN],dp[MAX];

void Initial() {

ptr = 0,nn = (1<<(n+1)) - 1;

memset(st,0,sizeof(st));

memset(in,false,sizeof(in));

memset(head,NULL,sizeof(head));

for (int i = 0; i <= (n+m); ++i)

for (int j = 0; j < MIN; ++j)

cost[i][j] = INF;

for (i = 0; i <= n ; ++i)

st[i] = 1<<i,cost[i][st[i]] = 0;

}

void AddEdge(int a,int b,int c) {

tree[ptr].v = b,tree[ptr].len = c;

tree[ptr].next = head[a],head[a] = &tree[ptr++];

}

void Spfa() {

while (!qu.empty()) {

int i = qu.front() / MAX;

int j = qu.front() % MAX;

qu.pop(),in[i][j] = false;

node *p = head[i];

while (p != NULL) {

int v = p->v,nst = j | st[v];

if (cost[i][j] + p->len < cost[v][nst]) {

cost[v][nst] = cost[i][j] + p->len;

if (nst == j && !in[v][nst])

qu.push(v * MAX + nst),in[v][nst] = true;

}

p = p->next;

}

}

}

void Steiner_Tree() {

int i,j,k;

for (j = 0; j <= nn; ++j){

for (i = 0; i <= (n + m); ++i){

for (k = (j-1)&j; k; k = (k-1) & j)

cost[i][j] = min(cost[i][j],cost[i][k|st[i]]+cost[i][(j-k)|st[i]]);

if (cost[i][j] != INF) qu.push(i * MAX + j),in[i][j] = true;

}

Spfa();

}

}

int Solve_DP() {

int i,j,k;

for (j = 0; j <= nn; ++j) {

dp[j] = INF;

for (i = 0; i <= (n + m); ++i)

dp[j] = min(dp[j],cost[i][j]);

}

//for (i = 1; i <= nn; ++i)

// if (i&1)for (k = (i-1)&i; k; k = (k-1)&i)

// if ((k&1)&&((i-k)&1))dp[i] = min(dp[i],dp[k]+dp[i-k]);

return dp[nn];

}

int main()

{

int i,j,k,a,b,c;

while (scanf("%d%d%d",&n,&m,&p) != EOF) {

for (i = 1; i <= (n + m); ++i)

scanf("%d",&well[i]);

Initial();

for (i = 1; i <= p; ++i) {

scanf("%d%d%d",&a,&b,&c);

AddEdge(a,b,c),AddEdge(b,a,c);

}

for (i = 1; i <= (n + m); ++i)

AddEdge(0,i,well[i]),AddEdge(i,0,well[i]);

Steiner_Tree();

ans = Solve_DP();

printf("%d\n",ans);

}

}

本文ZeroClock原创,但可以转载,因为我们是兄弟。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: