4006: [JLOI2015]管道连接
Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 648 Solved: 360
[ Submit][ Status][ Discuss]
Description
小铭铭最近进入了某情报部门,该部门正在被如何建立安全的通道连接困扰。
该部门有 n 个情报站,用 1 到 n 的整数编号。给出 m 对情报站 ui;vi 和费用 wi,表示情
报站 ui 和 vi 之间可以花费 wi 单位资源建立通道。
如果一个情报站经过若干个建立好的通道可以到达另外一个情报站,那么这两个情报站就
建立了通道连接。形式化地,若 ui 和 vi 建立了通道,那么它们建立了通道连接;若 ui 和 vi 均
与 ti 建立了通道连接,那么 ui 和 vi 也建立了通道连接。
现在在所有的情报站中,有 p 个重要情报站,其中每个情报站有一个特定的频道。小铭铭
面临的问题是,需要花费最少的资源,使得任意相同频道的情报站之间都建立通道连接。
Input
第一行包含三个整数 n;m;p,表示情报站的数量,可以建立的通道数量和重要情报站的数
量。接下来 m 行,每行包含三个整数 ui;vi;wi,表示可以建立的通道。最后有 p 行,每行包含
两个整数 ci;di,表示重要情报站的频道和情报站的编号。
Output
输出一行一个整数,表示任意相同频道的情报站之间都建立通道连接所花费的最少资源总量。
Sample Input
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
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
Sample Output
4
HINT
选择 (1; 5); (3; 5); (2; 5); (4; 5) 这 4 对情报站连接。
对于 100% 的数据,0 <ci <= p <= 10; 0 <ui;vi;di <= n <= 1000; 0 <= m <= 3000; 0 <= wi <=
20000。
Source
求连通关键点的生成树,自然是斯坦纳树了。
可以先构建关于这p个重要情报站的斯坦纳树。。。。构建方法之前的博客提过= =
然后此题只要求相同频道连通,再定义状态g[o]:重要情报站集合为o,且相同频道互相连通的最小代价
枚举频道集合给g初值,然后对g数组dp即可。O(3^n*n)
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<bitset>
using namespace std;
const int maxn = (1<<10);
const int INF = ~0U>>1;
struct E{
int to,w; E(){}
E(int to,int w): to(to),w(w){}
};
int n,m,p,tot,f[maxn][maxn],g[maxn],Mark[maxn];
bool vis[maxn];
vector <E> v[maxn];
queue <int> Q;
void Clear()
{
for (int o = 1; o < (1<<p); o++)
{
for (int i = 1; i <= n; i++) f[o][i] = INF;
g[o] = INF;
}
}
void SPFA(int o)
{
int *F = f[o];
while (!Q.empty())
{
int k = Q.front(); Q.pop(); vis[k] = 0;
for (int i = 0; i < v[k].size(); i++)
{
E e = v[k][i];
if (F[e.to] > F[k] + e.w)
{
F[e.to] = F[k] + e.w;
if (!vis[e.to]) vis[e.to] = 1,Q.push(e.to);
}
}
}
}
int getint()
{
char ch = getchar(); int ret = 0;
while (ch < '0' || '9' < ch) ch = getchar();
while ('0' <= ch && ch <= '9')
ret = ret*10 + ch - '0',ch = getchar();
return ret;
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
n = getint(); m = getint();
p = getint(); Clear();
while (m--)
{
int x = getint(),y,z;
y = getint(); z = getint();
v[x].push_back(E(y,z));
v[y].push_back(E(x,z));
}
for (int i = 0; i < p; i++)
{
int c = getint(),d = getint();
tot = max(tot,c); --c;
f[1<<i][d] = 0; Mark[c] |= (1<<i);
}
for (int o = 1; o < (1<<p); o++)
{
for (int j = 1; j <= n; j++)
{
for (int op = o-1&o; op; op = op-1&o)
if (f[op][j] != INF && f[o-op][j]!=INF)
f[o][j] = min(f[o][j],f[op][j] + f[o-op][j]);
if (f[o][j] != INF) Q.push(j),vis[j] = 1;
}
SPFA(o);
}
for (int i = 1; i < (1<<tot); i++)
{
int now = 0;
for (int j = 0; j < tot; j++)
if (i&(1<<j)) now |= Mark[j];
for (int j = 1; j <= n; j++)
g[now] = min(g[now],f[now][j]);
}
for (int o = 1; o < (1<<p); o++)
for (int op = o-1&o; op; op = op-1&o)
if (g[op] != INF && g[o-op] != INF)
g[o] = min(g[o],g[op]+g[o-op]);
cout << g[(1<<p)-1];
return 0;
}