steiner tree的几道题目


hdu 4085

http://acm.hdu.edu.cn/showproblem.php?pid=4085

注意森林情况转移时要注意保证每个子森林的房间数等于避难所数

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>   
#include <map>
#include <string>  
#include <climits> 
#include <set>
#include <string>    
#include <sstream>
#include <utility>   
#include <ctime>
#include <bitset>
#include <iomanip>
//#pragma comment(linker, "/STACK:102400000,102400000")

using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::stringstream;
using std::make_pair;
using std::getline;
using std::greater;
using std::endl;
using std::multimap;
using std::deque;
using std::unique;
using std::lower_bound;
using std::random_shuffle;
using std::bitset;
using std::upper_bound;
using std::multiset;
using std::ios;
using std::make_heap;
using std::push_heap;
using std::pop_heap;

typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned UN;
typedef pair<ULL, ULL> PAIR;
typedef multimap<int, int> MMAP;
typedef long double LF;

const int MAXN(60);
const int MAXM(100010);
const int MAXE(2010);
const int MAXK(6);
const int HSIZE(13131);
const int SIGMA_SIZE(26+11);
const int MAXH(18);
const int INFI((INT_MAX-1) >> 1);
const ULL BASE(31);
const LL LIM(1e13);
const int INV(-10000);
const int MOD(1000000007);
const double EPS(1e-7);
const LF PI(acos(-1.0));

template<typename T> inline bool checkmax(T &a, T b){if(b > a) { a = b; return true;} return false;}
template<typename T> inline bool checkmin(T &a, T b){if(b < a) { a = b; return true;} return false;}
template<typename T> inline T ABS(const T &a){return a < 0? -a: a;}

struct EDGE
{
    int v, w, next;
    EDGE(){}
    EDGE(int v_, int w_, int n_): v(v_), w(w_), next(n_){}
} edge[MAXE];
int first[MAXN], rear;

void init(int n)
{
    memset(first, -1, sizeof(first[0])*(n+1));
    rear = 0;
}

void insert(int u, int v, int w)
{
    edge[rear] = EDGE(v, w, first[u]);
    first[u] = rear++;
}

int dp[MAXN][1 << 10], id[MAXN];
bool inque[MAXN][1 << 10];
int ans[1 << 10];
int que[MAXM], front, back;

void spfa()
{
    while(front != back)
    {
        if(front == MAXM) front = 0;
        int no = que[front++];
        int st = no&0x3ff;
        no >>= 10;
        inque[no][st] = false;
        for(int i = first[no]; ~i; i = edge[i].next)
        {
            int v = edge[i].v;
            if(checkmin(dp[v][st|id[v]], dp[no][st]+edge[i].w) && st == (st|id[v]) && !inque[v][st])
            {
                if(back == MAXM) back = 0;
                que[back++] = (v << 10)|st;
                inque[v][st] = true;
            }
        }
    }
}

bool legal(int st, int K)
{
    int c1 = 0, c2 = 0;
    for(int i = 0; i < K; ++i) if((st >> i)&1) ++c1;
    for(int i = 0; i < K; ++i) if((st >> (i+K))&1) ++c2;
    return c1 == c2;
}
int main()
{
    int TC;
    scanf("%d", &TC);
    while(TC--)
    {
        int n, m, K;
        scanf("%d%d%d", &n, &m, &K);
        init(n);
        int u, v, w;
        for(int i = 0; i < m; ++i)
        {
            scanf("%d%d%d", &u, &v, &w);
            --u;
            --v;
            insert(u, v, w);
            insert(v, u, w);
        }
        int lim = (1 << (2*K))-1;
        for(int i = 0; i < n; ++i)
        {
            id[i] = 0;
            for(int j = 0; j <= lim; ++j)
            {
                dp[i][j] = INFI;
                inque[i][j] = false;
            }
        }
        for(int i = 0; i < K; ++i)
        {
            id[i] = (1 << i);
            id[n-K+i] = (1 << (i+K));
            dp[i][id[i]] = 0;
            dp[n-K+i][id[n-K+i]] = 0;
        }
        for(int i = 0; i <= lim; ++i)
        {
            for(int j = 0; j < n; ++j)
            {
                if(id[j] && !(id[j]&i)) continue;
                for(int k = i; k; k = (k-1)&i)
                    checkmin(dp[j][i], dp[j][(i^k)|id[j]]+dp[j][k|id[j]]);
                if(dp[j][i] < INFI)
                {
                    if(back == MAXM) back = 0;
                    que[back++] = (j << 10)|i;
                    inque[j][i] = true;
                }
            }
            spfa();
        }
        for(int i = 0; i <= lim; ++i) ans[i] = dp[0][i];
        for(int i = 1; i < n; ++i)
            for(int j = 0; j <= lim; ++j)
                checkmin(ans[j], dp[i][j]);
        for(int i = 0; i <= lim; ++i)
            if(legal(i, K))
                for(int j = i; j; j = (j-1)&i)
                    if(legal(j, K))
                        checkmin(ans[i], ans[j]+ans[i^j]);
        if(ans[lim] < INFI) printf("%d\n", ans[lim]);
        else printf("No solution\n");
    }
    return 0;
}



http://www.lydsy.com/JudgeOnline/problem.php?id=2595

插头dp可以搞,用spfa+dp搞起来更容易,效率也更高

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>   
#include <map>
#include <string>  
#include <climits> 
#include <set>
#include <string>    
#include <sstream>
#include <utility>   
#include <ctime>
#include <bitset>
#include <iomanip>
//#pragma comment(linker, "/STACK:102400000,102400000")

using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::stringstream;
using std::make_pair;
using std::getline;
using std::greater;
using std::endl;
using std::multimap;
using std::deque;
using std::unique;
using std::lower_bound;
using std::random_shuffle;
using std::bitset;
using std::upper_bound;
using std::multiset;
using std::ios;
using std::make_heap;
using std::push_heap;
using std::pop_heap;

typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned UN;
typedef pair<ULL, ULL> PAIR;
typedef multimap<int, int> MMAP;
typedef long double LF;

const int MAXN(60);
const int MAXM(150010);
const int MAXE(2010);
const int MAXK(6);
const int HSIZE(13131);
const int SIGMA_SIZE(26+11);
const int MAXH(18);
const int INFI((INT_MAX-1) >> 1);
const ULL BASE(31);
const LL LIM(1e13);
const int INV(-10000);
const int MOD(1000000007);
const double EPS(1e-7);
const LF PI(acos(-1.0));

template<typename T> inline bool checkmax(T &a, T b){if(b > a) { a = b; return true;} return false;}
template<typename T> inline bool checkmin(T &a, T b){if(b < a) { a = b; return true;} return false;}
template<typename T> inline T ABS(const T &a){return a < 0? -a: a;}

int mx[4] = {0, -1, 0, 1};
int my[4] = {-1, 0, 1, 0};
int bit[11][11], mp[11][11];
int dp[11][11][1 << 10], pre[11][11][1 << 10][2], opt[11][11][1 << 10];
int que[MAXM], front, back;
bool inque[11][11][1 << 10];
int N, M;
char rec[11][11];

inline int code(int x, int y, int st){ return (((x << 4)|y) << 10)|st;}
inline void decode(int &x, int &y, int &st, int t)
{
	st = t&0x3ff; t >>= 10;
	y = t&0xf; t >>= 4;
	x = t;
}
inline void FIX(int &x){if(x >= MAXM) x = 0;}
void spfa()
{
	while(front != back)
	{
		int x, y, st;
		FIX(front);
		decode(x, y, st, que[front++]);
		inque[x][y][st] = false;
		for(int i = 0; i < 4; ++i)
		{
			int nx = x+mx[i], ny = y+my[i];
			if(nx >= 0 && nx < N && ny >= 0 && ny < M)
			{
				if(checkmin(dp[nx][ny][st|bit[nx][ny]], dp[x][y][st]+mp[nx][ny]))
				{
					opt[nx][ny][st|bit[nx][ny]] = 0;
					pre[nx][ny][st|bit[nx][ny]][0] = code(x, y, st);
					if(st == (st|bit[nx][ny]) && !inque[nx][ny][st])
					{
						FIX(back);
						que[back++] = code(nx, ny, st);
						inque[nx][ny][st] = true;
					}
				}
			}
		}
	}
}

void print(int x, int y, int st)
{
	if(mp[x][y]) rec[x][y] = 'o';
	else rec[x][y] = 'x';
	int tx, ty, tst;
	if(opt[x][y][st] != -1) 
	{
		decode(tx, ty, tst, pre[x][y][st][0]);
		print(tx, ty, tst);
	}
	if(opt[x][y][st] == 1)
	{
		decode(tx, ty, tst, pre[x][y][st][1]);
		print(tx, ty, tst);
	}
}

int main()
{
	int K = 0;
	scanf("%d%d", &N, &M);
	for(int i = 0; i < N; ++i)
		for(int j = 0; j < M; ++j)
		{
			scanf("%d", mp[i]+j);
			bit[i][j] = 0;
			if(!mp[i][j]) bit[i][j] = 1 << K++;
		};
	int lim = (1 << K)-1;
	for(int i = 0; i < N; ++i)
		for(int j = 0; j < M; ++j)
		{
			for(int k = 0; k <= lim; ++k)
			{
				dp[i][j][k] = INFI;
				opt[i][j][k] = -1;
			}
			if(!mp[i][j]) dp[i][j][bit[i][j]] = 0;
		}
	for(int i = 0; i <= lim; ++i)
	{
		for(int j = 0; j < N; ++j)
			for(int k = 0; k < M; ++k)
			{
				if(bit[j][k] && !(bit[j][k]&i)) continue;
				for(int z = i; z; z = (z-1)&i)
					if(checkmin(dp[j][k][i], dp[j][k][(i^z)|bit[j][k]]+dp[j][k][z|bit[j][k]]-mp[j][k]))
					{
						opt[j][k][i] = 1;
						pre[j][k][i][0] = code(j, k, (i^z)|bit[j][k]);
						pre[j][k][i][1] = code(j, k, z|bit[j][k]);
					}
				if(dp[j][k][i] < INFI)
				{
					FIX(back);
					que[back++] = code(j, k, i);
					inque[j][k][i] = true;
				}
			}
		spfa();
	}
	for(int i = 0; i < N; ++i)
		for(int j = 0; j < M; ++j)
			rec[i][j] = '_';
	int x = 0, y = 0, ans = INFI;
	for(int i = 0; i < N; ++i)
		for(int j = 0; j < M; ++j)
			if(checkmin(ans, dp[i][j][lim]))
			{
				x = i;
				y = j;
			}
	if(lim) print(x, y, lim);
	else ans = 0;
	printf("%d\n", ans);
	for(int i = 0; i < N; ++i)
	{
		for(int j = 0; j < M; ++j)
			printf("%c", rec[i][j]);
		printf("\n");
	}
	return 0;
}


zoj 3613

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3613

注意到一个星球只能给一个工厂供给

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>   
#include <map>
#include <string>  
#include <climits> 
#include <set>
#include <string>    
#include <sstream>
#include <utility>   
#include <ctime>
#include <bitset>
#include <iomanip>
//#pragma comment(linker, "/STACK:102400000,102400000")

using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::stringstream;
using std::make_pair;
using std::getline;
using std::greater;
using std::endl;
using std::multimap;
using std::deque;
using std::unique;
using std::lower_bound;
using std::random_shuffle;
using std::bitset;
using std::upper_bound;
using std::multiset;
using std::ios;
using std::make_heap;
using std::push_heap;
using std::pop_heap;

typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned UN;
typedef pair<ULL, ULL> PAIR;
typedef multimap<int, int> MMAP;
typedef long double LF;

const int MAXN(210);
const int MAXM(200010);
const int MAXE(10010);
const int MAXK(6);
const int HSIZE(13131);
const int SIGMA_SIZE(26+11);
const int MAXH(18);
const int INFI((INT_MAX-1) >> 1);
const ULL BASE(31);
const LL LIM(1e13);
const int INV(-10000);
const int MOD(1000000007);
const double EPS(1e-7);
const LF PI(acos(-1.0));

template<typename T> inline bool checkmax(T &a, T b){if(b > a) { a = b; return true;} return false;}
template<typename T> inline bool checkmin(T &a, T b){if(b < a) { a = b; return true;} return false;}
template<typename T> inline T ABS(const T &a){return a < 0? -a: a;}

struct EDGE
{
	int v, w, next;
	EDGE(){}
	EDGE(int v_, int w_, int n_): v(v_), w(w_), next(n_){}
} edge[MAXE];
int first[MAXN], rear;

void init(int n)
{
	memset(first, -1, sizeof(first[0])*(n+1));
	rear = 0;
}

void insert(int u, int v, int w)
{
	edge[rear] = EDGE(v, w, first[u]);
	first[u] = rear++;
}

int bit[MAXN];
int dp[MAXN][1 << 8];
int P[MAXN], S[MAXN];
int que[MAXM], front, back;
bool inque[MAXN][1 << 8];
int ans[1 << 8][2];
int CNT;

inline void fix(int &x){if(x >= MAXM) x = 0;}
inline int code(int no, int st){ return (no << 8)|st;}
inline void decode(int &no, int &st, int t)
{
	st = t&0xff; t >>= 8;
	no = t;
}

int calc(int st)
{
	int r1 = 0, r2 = 0;
	for(int i = 0; i < CNT; ++i)
		if((st >> i)&1)
		{
			r1 += P[i];
			r2 += S[i];
		}
	return min(r1, r2);
}

void updata(int *a, int a0, int a1)
{
	if(a1 > a[1] || (a1 == a[1] && a0 < a[0]))
	{
		a[0] = a0;
		a[1] = a1;
	}
}

void spfa()
{
	while(front != back)
	{
		int no, st;
		fix(front);
		decode(no, st, que[front++]);
		inque[no][st] = false;
		for(int i = first[no]; ~i; i = edge[i].next)
		{
			int v = edge[i].v;
			if(checkmin(dp[v][st|bit[v]], dp[no][st]+edge[i].w) && (st|bit[v]) == st && !inque[v][st])
			{
				fix(back);
				que[back++] = code(v, st);
				inque[v][st] = true;
			}
		}
	}
}

int main()
{
	int n, m;
	while(~scanf("%d", &n))
	{
		CNT = 0;
		for(int i = 0; i < n; ++i) 
		{
			scanf("%d%d", P+CNT, S+CNT);
			bit[i] = 0;
			if(P[CNT] || S[CNT]) bit[i] = 1 << CNT++;
		}
		int lim = (1 << CNT)-1;
		for(int i = 0; i < n; ++i)
		{
			for(int j = 0; j <= lim; ++j) dp[i][j] = INFI;
			dp[i][bit[i]] = 0;
		}
		init(n);
		scanf("%d", &m);
		int u, v, w;
		for(int i = 0; i < m; ++i)
		{
			scanf("%d%d%d", &u, &v, &w);
			--u; --v;
			insert(u, v, w);
			insert(v, u, w);
		}
		for(int i = 0; i <= lim; ++i)
		{
			for(int j = 0; j < n; ++j)
			{
				if(bit[j] && (i&bit[j]) != bit[j]) continue;
				for(int k = i; k; k = (k-1)&i)
					checkmin(dp[j][i], dp[j][(i^k)|bit[j]]+dp[j][k|bit[j]]);
				if(dp[j][i] < INFI)
				{
					fix(back);
					que[back++] = code(j, i);
					inque[j][i] = true;
				}
			}
			spfa();
		}
		for(int i = 0; i <= lim; ++i) ans[i][0] = dp[0][i];
		for(int i = 1; i < n; ++i)
			for(int j = 0; j <= lim; ++j)
				checkmin(ans[j][0], dp[i][j]);
		for(int i = 0; i <= lim; ++i)
			if(ans[i][0] < INFI) ans[i][1] = calc(i);
			else ans[i][1] = 0;
		for(int i = 0; i <= lim; ++i)
			for(int j = i; j; j = (j-1)&i)
				if(ans[j][0] < INFI && ans[i^j][0] < INFI)
					updata(ans[i], ans[j][0]+ans[i^j][0], ans[j][1]+ans[i^j][1]);
		int val[2] = {0, 0};
		for(int i = 0; i <= lim; ++i) updata(val, ans[i][0], ans[i][1]);
		printf("%d %d\n", val[1], val[0]);
	}
	return 0;
}


CF 152E

http://codeforces.com/problemset/problem/152/E

不得不说的确比插头dp好写

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>   
#include <map>
#include <string>  
#include <climits> 
#include <set>
#include <string>    
#include <sstream>
#include <utility>   
#include <ctime>
#include <bitset>
#include <iomanip>
//#pragma comment(linker, "/STACK:102400000,102400000")

using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::stringstream;
using std::make_pair;
using std::getline;
using std::greater;
using std::endl;
using std::multimap;
using std::deque;
using std::unique;
using std::lower_bound;
using std::random_shuffle;
using std::bitset;
using std::upper_bound;
using std::multiset;
using std::ios;
using std::make_heap;
using std::push_heap;
using std::pop_heap;

typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned UN;
typedef pair<ULL, ULL> PAIR;
typedef multimap<int, int> MMAP;
typedef long double LF;

const int MAXN(210);
const int MAXM(40010);
const int MAXE(10010);
const int MAXK(6);
const int HSIZE(13131);
const int SIGMA_SIZE(26+11);
const int MAXH(18);
const int INFI((INT_MAX-1) >> 1);
const ULL BASE(31);
const LL LIM(1e13);
const int INV(-10000);
const int MOD(1000000007);
const double EPS(1e-7);
const LF PI(acos(-1.0));

template<typename T> inline bool checkmax(T &a, T b){if(b > a) { a = b; return true;} return false;}
template<typename T> inline bool checkmin(T &a, T b){if(b < a) { a = b; return true;} return false;}
template<typename T> inline T ABS(const T &a){return a < 0? -a: a;}

int mx[4] = {0, -1, 0, 1};
int my[4] = {-1, 0, 1, 0};
int mp[110][110];
int rmp[210][2];
int dp[210][1 << 7]; 
int pre[210][1 << 7][2], opt[210][1 << 7];
int bit[210];
int val[210];
int que[MAXM], front, back;
bool inque[210][1 << 7];
char rec[110][110];
int N, M;
inline void fix(int &x){if(x >= MAXM) x = 0;}
inline int code(int no, int st){ return (no << 7)|st;}
inline void decode(int &no, int &st, int t)
{
	st = t&0x7f; t >>= 7;
	no = t;
}

void spfa()
{
	while(front != back)
	{
		int no, st;
		fix(front);
		decode(no, st, que[front++]);
		inque[no][st] = false;
		int x = rmp[no][0];
		int y = rmp[no][1];
		for(int i = 0; i < 4; ++i)
		{
			int nx = x+mx[i];
			int ny = y+my[i];
			if(nx >= 0 && nx < N && ny >= 0 && ny < M)
			{
				int tno = mp[nx][ny], tst = st|bit[tno];
				if(checkmin(dp[tno][tst], dp[no][st]+val[tno]))
				{
					opt[tno][tst] = 0;
					pre[tno][tst][0] = code(no, st);
					if(tst == st && !inque[tno][st])
					{
						fix(back);
						que[back++] = code(tno, st);
						inque[tno][st] = true;
					}
				}
			}
		}
	}
}

void print(int co)
{
	int no, st;
	decode(no, st, co);
	rec[rmp[no][0]][rmp[no][1]] = 'X';
	if(opt[no][st] != -1) print(pre[no][st][0]);
	if(opt[no][st] == 1) print(pre[no][st][1]);
}

int main()
{
	int K;
	scanf("%d%d%d", &N, &M, &K);
	int tn = 0;
	for(int i = 0; i < N; ++i)
		for(int j = 0; j < M; ++j)
		{
			mp[i][j] = tn;
			rmp[tn][0] = i;
			rmp[tn++][1] = j;
		}
	for(int i = 0; i < tn; ++i) scanf("%d", val+i);
	int x, y;
	for(int i = 0; i < K; ++i)
	{
		scanf("%d%d", &x, &y);
		--x; --y;
		bit[mp[x][y]] = 1 << i;
	}
	int lim = (1 << K)-1;
	for(int i = 0; i < tn; ++i) 
	{
		for(int j = 0; j <= lim; ++j) dp[i][j] = INFI;
		dp[i][bit[i]] = val[i];
		opt[i][bit[i]] = -1;
	}
	for(int i = 0; i <= lim; ++i)
	{
		for(int j = 0; j < tn; ++j)
		{
			if(bit[j] && (i&bit[j]) != bit[j]) continue;
			for(int k = i; k; k = (k-1)&i)
				if(checkmin(dp[j][i], dp[j][k]+dp[j][k^i]-val[j]))
				{
					opt[j][i] = 1;
					pre[j][i][0] = code(j, k);
					pre[j][i][1] = code(j, k^i);
				}
			if(dp[j][i] < INFI)
			{
				fix(back);
				que[back++] = code(j, i);
				inque[j][i] = true;
			}
		}
		spfa();
	}
	for(int i = 0; i < N; ++i)
		for(int j = 0; j < M; ++j)
			rec[i][j] = '.';
	int ans = INFI, co;
	for(int i = 0; i < tn; ++i)
		if(checkmin(ans, dp[i][lim]))
			co = code(i, lim);
	print(co);
	printf("%d\n", ans);
	for(int i = 0; i < N; ++i)
	{
		for(int j = 0; j < M; ++j) putchar(rec[i][j]);
		putchar('\n');
	}
	return 0;
}

hdu 3311

http://acm.hdu.edu.cn/showproblem.php?pid=3311

描述太飘逸了,完全理解不能 ==1

n+m个点,给出在每个点挖井的花费和修路的费用,求使得前n个点或者自己打井,或者通过所修路径能够到达一个打井的点的最小花费方案

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>   
#include <map>
#include <string>  
#include <climits> 
#include <set>
#include <string>    
#include <sstream>
#include <utility>   
#include <ctime>
#include <bitset>
#include <iomanip>
//#pragma comment(linker, "/STACK:102400000,102400000")

using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::stringstream;
using std::make_pair;
using std::getline;
using std::greater;
using std::endl;
using std::multimap;
using std::deque;
using std::unique;
using std::lower_bound;
using std::random_shuffle;
using std::bitset;
using std::upper_bound;
using std::multiset;
using std::ios;
using std::make_heap;
using std::push_heap;
using std::pop_heap;

typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned UN;
typedef pair<ULL, ULL> PAIR;
typedef multimap<int, int> MMAP;
typedef long double LF;

const int MAXN(1010);
const int MAXM(100010);
const int MAXE(10010);
const int MAXK(6);
const int HSIZE(13131);
const int SIGMA_SIZE(26+11);
const int MAXH(18);
const int INFI((INT_MAX-1) >> 1);
const ULL BASE(31);
const LL LIM(1e13);
const int INV(-10000);
const int MOD(1000000007);
const double EPS(1e-6);
const LF PI(acos(-1.0));

template<typename T> inline bool checkmax(T &a, T b){if(b > a) { a = b; return true;} return false;}
template<typename T> inline bool checkmin(T &a, T b){if(b < a) { a = b; return true;} return false;}
template<typename T> inline T ABS(T a){return a < 0? -a: a;}
template<typename T> inline bool EZ(T a){return ABS(a) < EPS;}

struct EDGE{
	int v, w, next;
	EDGE(){}
	EDGE(int v_, int w_, int n_): v(v_), w(w_), next(n_){}
} edge[MAXE];
int first[MAXN], rear;
void init(int n){
	memset(first, -1, sizeof(first[0])*(n+1));
	rear = 0;
}
void insert(int u, int v, int w){
	edge[rear] = EDGE(v, w, first[u]);
	first[u] = rear++;
}
inline void fix(int &x){if(x >= MAXM) x = 0;}
inline int encode(int num, int st, int vi){
	return (num << 6)|(st << 1)|vi;
}
inline void decode(int &num, int &st, int &vi, int co){
	vi = co&1; co >>= 1;
	st = co&0x1f; co >>= 5;
	num = co;
}

int bit[MAXN], val[MAXN];
int dp[MAXN][1 << 5][2];
bool inque[MAXN][1 << 5][2];
int que[MAXM], front, back;
int ans[1 << 5];

void spfa(){
	while(front != back){
		int num, st, vi;
		fix(front);
		decode(num, st, vi, que[front++]);
		inque[num][st][vi] = false;
		for(int i = first[num]; ~i; i = edge[i].next){
			int v = edge[i].v, w = edge[i].w, nst = st|bit[v];
			if(checkmin(dp[v][nst][vi], dp[num][st][vi]+w) && nst == st && !inque[v][nst][vi]){
				fix(back);
				que[back++] = encode(v, nst, vi);
				inque[v][nst][vi] = true;
			}
			if(vi == 0 && checkmin(dp[v][nst][1], dp[num][st][0]+w+val[v]) && nst == st && !inque[v][nst][1]){
				fix(back);
				que[back++] = encode(v, nst, 1);
				inque[v][nst][vi] = true;
			}
		}
	}
}

int main(){
	int n, m, p;
	while(~scanf("%d%d%d", &n, &m, &p)){
		int tn = n+m;
		for(int i = 0; i < tn; ++i){
			bit[i] = 0;
			scanf("%d", val+i);
		}
		for(int i = 0; i < n; ++i) bit[i] = 1 << i;
		init(tn);
		int u, v, w;
		for(int i = 0; i < p; ++i){
			scanf("%d%d%d", &u, &v, &w);
			--u; --v;
			insert(u, v, w);
			insert(v, u, w);
		}
		int lim = (1 << n)-1;
		for(int i = 0; i < tn; ++i){
			for(int j = 0; j <= lim; ++j){
				inque[i][j][0] = inque[i][j][1] = false;
				dp[i][j][0] = dp[i][j][1] = INFI;
			}
			dp[i][bit[i]][0] = 0;
			dp[i][bit[i]][1] = val[i];
		}
		for(int i = 0; i <= lim; ++i){
			for(int j = 0; j < tn; ++j){
				if(bit[j] && (i&bit[j]) != bit[j]) continue;
				for(int k = i; k; k = (k-1)&i){
					checkmin(dp[j][i][0], dp[j][k|bit[j]][0]+dp[j][(i^k)|bit[j]][0]);
					checkmin(dp[j][i][1], dp[j][k|bit[j]][0]+dp[j][(i^k)|bit[j]][1]);
					checkmin(dp[j][i][1], dp[j][k|bit[j]][1]+dp[j][(i^k)|bit[j]][0]);
					checkmin(dp[j][i][1], dp[j][k|bit[j]][1]+dp[j][(i^k)|bit[j]][1]);
				}
				if(dp[j][i][0] < INFI){
					fix(back);
					que[back++] = encode(j, i, 0);
					inque[j][i][0] = true;
				}
				if(dp[j][i][1] < INFI){
					fix(back);
					que[back++] = encode(j, i, 1);
					inque[j][i][1] = true;
				}
			}
			spfa();
		}
		for(int i = 0; i <= lim; ++i) ans[i] = dp[0][i][1];
		for(int i = 1; i < tn; ++i)
			for(int j = 0; j <= lim; ++j)
				checkmin(ans[j], dp[i][j][1]);
		for(int i = 0; i <= lim; ++i)
			for(int j = i; j; j = (j-1)&i)
				checkmin(ans[i], ans[j]+ans[i^j]);
		printf("%d\n", ans[lim]);
	}
	return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值