# 斯坦纳树

$$f_{i,S}$$为以$$i$$为根，目前已选点集为$$S$$的最小代价。

$f_{i,S}=min\{f_{i,T}+f_{i,S\bigoplus T}\}(T \subseteq S)$

$f_{u,S}=min\{ f_{v,S}+w_{u,v}\}$

Code

#include<bits/stdc++.h>
using namespace std;

{
int x = 0, f = 1; char ch = getchar();
for (; ch < '0' || ch > '9'; ch = getchar()) if (ch == '-') f = -1;
for (; ch >= '0' && ch <= '9'; ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
return x * f;
}

const int N = 105;
const int M = 1005;
const int K = 11;

struct node{ int to, nxt, val; }edge[M];
int f[N][1 << K];
bool vis[N];

void addedge(int u, int v, int w)
{
edge[++tot] = (node){ v, head[u], w };
}

priority_queue<pair<int, int> > Q;

void Dijkstra(int s)
{
memset(vis, 0, sizeof(vis));
while (!Q.empty())
{
int u = Q.top().second;
Q.pop();

if (vis[u]) continue;
vis[u] = 1;

for (int i = head[u]; i; i = edge[i].nxt)
{
int v = edge[i].to, w = edge[i].val;
if (f[v][s] > f[u][s] + w)
{
f[v][s] = f[u][s] + w;
Q.push({-f[v][s], v});
}
}
}
}

int main()
{
for (int i = 1; i <= m; ++i)
{

}

memset(f, 0x3f, sizeof(f));
for (int i = 1; i <= k; ++i)
{
f[x][1 << (i - 1)] = 0;
}

for (int s = 1; s < (1 << k); ++s)
{
for (int i = 1; i <= n; ++i)
{
for (int s1 = s & (s - 1); s1; s1 = s & (s1 - 1))
{
int s2 = s1 ^ s;
f[i][s] = min(f[i][s], f[i][s1] + f[i][s2]);
}
Q.push({-f[i][s], i});
}
Dijkstra(s);
}
printf("%d\n", f[x][(1 << k) - 1]);
return 0;
}


$$g_S$$为已选点集为$$S$$的最小代价。

Code

WC2008 游览计划

posted @ 2020-07-03 17:21  OIerC  阅读(181)  评论(0编辑  收藏  举报