# BZOJ4006: [JLOI2015]管道连接(斯坦纳树,状压DP)

Time Limit: 30 Sec  Memory Limit: 128 MB
Submit: 1171  Solved: 639
[Submit][Status][Discuss]

5 8 4
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

4

20000。

## Source

$g$比较好求，枚举子集转移就可以

$f$的话，如果学过斯坦纳树也比较好求

#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int MAXN = 1e6 + 10;
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
return x * f;
}
int N, M, P;
struct node {
int u, v, w, nxt;
}E[MAXN];
inline void AddEdge(int x, int y,int z) {
E[num].u = x; E[num].v = y; E[num].w = z;
}
struct Point {
int color, ID;
}a[MAXN];
int f[1051][1051], g[1051];
int vis[MAXN], sum[MAXN];
queue<int>q;
void SPFA(int now) {
while(q.size() != 0) {
int p = q.front(); q.pop(); vis[p] = 0;
for(int i = head[p]; i != -1; 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] = 1, q.push(v);
}
}
}
}
int tmp[11];
bool check(int sta) {
memset(tmp, 0, sizeof(tmp));
for(int i = 1; i <= 10; i++)
if(sta & (1 << i - 1)) tmp[a[i].color]++;
for(int i = 1; i <= 10; i++)
if(tmp[i] && tmp[i] != sum[i]) return 0;
return 1;
}
int main() {
#ifdef WIN32
freopen("a.in", "r", stdin);
#endif
for(int i = 1; i <= M; i++) {
}
memset(f, 0x3f, sizeof(f));
memset(g, 0x3f, sizeof(g));
for(int i = 1; i <= P; i++)
a[i].color = read(), a[i].ID = read(), sum[a[i].color]++,f[a[i].ID][1 << (i - 1)] = 0;
int limit = (1 << P) - 1;
for(int sta = 0; sta <= limit; sta++) {
for(int i = 1; i <= N; i++) {
for(int S = sta; S; S = (S - 1) & sta)
f[i][sta] = min(f[i][sta], f[i][S] + f[i][sta - S]);
q.push(i),vis[i] = 1;
}
SPFA(sta);
}
for(int sta = 0; sta <= limit; sta++)
for(int i = 1; i <= N; i++)
g[sta] = min(g[sta], f[i][sta]);
for(int sta = 0; sta <= limit; sta++)
if(check(sta))
for(int S = sta; S; S = (S - 1) & sta)
if(check(S))
g[sta] = min(g[sta], g[S] + g[sta - S]);
printf("%d", g[(1 << P) - 1]);
return 0;
}

posted @ 2018-05-01 19:59  自为风月马前卒  阅读(248)  评论(0编辑  收藏

……