Steiner Tree。概念就不讲了,引入0号结点。[1, n+m]到0连一条边,权重表示挖井的费用。这样建图spfa求MST即满足所求解。
/* 3311 */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 typedef struct {
int v, w, nxt;
} edge_t; const int INF = 0x3f3f3f3f;
const int maxst = ;
const int maxn = ;
const int maxe = ;
edge_t E[maxe];
int head[maxn], l;
int dp[maxn][maxst];
bool visit[maxn][maxst];
int n, m;
queue<pii> Q; void init() {
l = ;
memset(head, -, sizeof(head));
memset(dp, INF, sizeof(dp));
memset(visit, false, sizeof(visit));
} void addEdge(int u, int v, int w) {
E[l].v = v;
E[l].w = w;
E[l].nxt = head[u];
head[u] = l++; E[l].v = u;
E[l].w = w;
E[l].nxt = head[v];
head[v] = l++;
} void spfa() {
int u, v, st, nst; while (!Q.empty()) {
u = Q.front().fir;
st = Q.front().sec;
visit[u][st] = false;
Q.pop();
for (int i=head[u]; i!=-; i=E[i].nxt) {
v = E[i].v;
nst = st;
if (v <= n)
nst |= (<<v);
if (dp[u][st]+E[i].w < dp[v][nst]) {
dp[v][nst] = dp[u][st]+E[i].w;
if (nst==st && !visit[v][nst]) {
visit[v][nst] = true;
Q.push(mp(v, nst));
}
}
}
}
} void solve() {
int mst = <<(n+);
int nn = n + m + ;
int kk, kk_; rep(i, , n+)
dp[i][<<i] = ; rep(i, , mst) {
rep(j, , nn) {
rep(k, , i) {
if ((k|i) == i) {
kk = k;
kk_ = i - k;
if (j <= n) {
kk |= (<<j);
kk_ |= (<<j);
}
dp[j][i] = min(dp[j][i], dp[j][kk]+dp[j][kk_]);
}
}
if (dp[j][i] != INF) {
Q.push(mp(j, i));
visit[j][i] = true;
}
}
spfa();
} int ans = INF;
rep(j, , nn)
ans = min(ans, dp[j][mst-]);
printf("%d\n", ans);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int p;
int u, v, w; while (scanf("%d %d %d",&n,&m,&p)!=EOF) {
init();
rep(i, , n+m+) {
scanf("%d", &w);
addEdge(, i, w);
}
rep(i, , p) {
scanf("%d %d %d", &u, &v, &w);
addEdge(u, v, w);
}
solve();
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}