Pipes hdu1964

每个点都走一次的一条回路问题,求得是最小代价,只要在选择插头时加上花费就可以了,还有就是处理好输入。

#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>

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::istringstream;
using std::make_pair;
using std::getline;
using std::greater;
using std::endl;
using std::multimap;
using std::deque;

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

const int MAXN(1000010);
const int MAXM(10010);
const int MAXE(10010);
const int HSIZE(13131);
const int SIGMA_SIZE(26);
const int MAXH(19);
const int INFI((INT_MAX-1) >> 1);
const int MOD(100000000);
const ULL BASE(31);
const LL LIM(10000000);
const int INV(-10000);

void checkmin(int &op1, int op2) {if(op2 < op1) op1 = op2;}

struct HASH_MAP
{
    int first[HSIZE], next[MAXN];
    LL state[MAXN];
    int value[MAXN];
    int  size;
    void init()
    {
        memset(first, -1, sizeof(first));
        size = 0;
    }

    void insert(LL ts, int tv)
    {
        int h = ts%HSIZE;
        for(int i = first[h]; ~i; i = next[i])
            if(state[i] == ts)
            {
                checkmin(value[i], tv);
                return;
            }
        state[size] = ts;
        value[size] = tv;
        next[size] = first[h];
        first[h] = size++;
    }
}hm[2];

HASH_MAP *cur, *last;

int cost[12][12][2];
char code[12];
int Num[9];
int N, M;

void decode(LL ts)
{
    for(int i = 0; i <= M; ++i)
    {
        code[i] = ts&7;
        ts >>= 3;
    }
}

LL encode()
{
    LL ret = 0;
    int cnt = 0;
    memset(Num, -1, sizeof(Num));
    for(int i = M; i >= 0; --i)
        if(code[i] == 0)
            ret <<= 3;
        else
        {
            if(Num[code[i]] < 0) Num[code[i]] = ++cnt;
            ret = (ret << 3)|Num[code[i]];
        }
    return ret;
}

void updata(int x, int y, int tv)
{
    int up = (x == 0)? 0: code[y+1];
    int left = (y == 0)? 0: code[y];
    if(up == 0 && left == 0)
    {
        if(x == N-1 || y == M-1) return;
        code[y] = code[y+1] = 7;
        cur->insert(encode(), tv+cost[x][y][0]+cost[x][y][1]);
    }
    else
        if(up == 0 || left == 0)
        {
            if(x < N-1)
            {
                code[y] = up+left;
                code[y+1] = 0;
                cur->insert(encode(), tv+cost[x][y][0]);
            }
            if(y < M-1)
            {
                code[y] = 0;
                code[y+1] = up+left;
                cur->insert(encode(), tv+cost[x][y][1]);
            }
        }
        else
            if(up != left)
            {
                if(x == N-1 && y == M-1) return;
                for(int i = 0; i <= M; ++i)
                    if(code[i] == up) code[i] = left;
                code[y] = code[y+1] = 0;
                cur->insert(encode(), tv);
            }
            else
                if(x == N-1 && y == M-1)
                {
                    code[y] = code[y+1] = 0;
                    cur->insert(encode(), tv);
                }
}

void solve()
{
    cur = hm;
    last = hm+1;
    last->init();
    last->insert(0, 0);
    for(int i = 0; i < N; ++i)
        for(int j = 0; j < M; ++j)
        {
            cur->init();
            int sz = last->size;
            for(int k = 0; k < sz; ++k)
            {
                decode(last->state[k]);
                if(j == 0)
                    for(int k2 = M; k2 >= 1; --k2)
                        code[k2] = code[k2-1];
                updata(i, j, last->value[k]);
            }
            swap(cur, last);
        }
    int ans = INFI;
    for(int i = 0; i < last->size; ++i)
        if(last->state[i] == 0)
        {
            ans = last->value[i];
            break;
        }
    printf("%d\n", ans);
}


char str[100];

int main()
{
    int TC;
    scanf("%d", &TC);
    while(TC--)
    {
        int n, m;
        scanf("%d%d", &n, &m);
        scanf("%s", str);
        for(int i = 0; i < n; ++i)
        {
            char temp;
            scanf(" %c", &temp);
            for(int j = 0; j < m-1; ++j)
                scanf("%d", cost[i][j]+1);
            scanf(" %c", &temp);
            for(int j = 0; j < m; ++j)
                scanf(" %c%d", &temp, cost[i][j]);
            scanf(" %c", &temp);
        }
        scanf("%s", str);
        N = n;
        M = m;
        solve();
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值