Catch the Theves HDU - 3870(s - t平面图最小割)

题意:

  板题。。。建个图。。跑一遍spfa就好了。。。嘻嘻。。。

注意。。数组大小就好啦。。400 * 400 = 1600 我也是抑郁了。。沙雕的我。。

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define pd(a) printf("%d\n", a);
#define plld(a) printf("%lld\n", a);
#define pc(a) printf("%c\n", a);
#define ps(a) printf("%s\n", a);
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 1e6 + 10, INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff;
int n, s, t;
int head[maxn], d[maxn], vis[maxn];
int cnt;

struct node
{
    int u, v, w, next;
}Node[maxn << 1];

void add_(int u, int v, int w)
{
    Node[cnt].u = u;
    Node[cnt].v = v;
    Node[cnt].w = w;
    Node[cnt].next = head[u];
    head[u] = cnt++;
}

void add(int u, int v, int w)
{
    add_(u, v, w);
    add_(v, u, w);

}

void spfa()
{
    queue<int> Q;
    for(int i = 0; i < maxn; i++) d[i] = INF;
    mem(vis, 0);
    Q.push(s);
    vis[s] = 1;
    d[s] = 0;
    while(!Q.empty())
    {
        int u = Q.front(); Q.pop();
        vis[u] = 0;
        for(int i = head[u]; i != -1; i = Node[i].next)
        {
            node e = Node[i];
            if(d[e.v] > d[u] + e.w)
            {
                d[e.v] = d[u] + e.w;
                if(!vis[e.v])
                {
                    vis[e.v] = 1;
                    Q.push(e.v);
                }
            }
        }
    }
}

void init()
{
    mem(head, -1);
    cnt = 0;
}

int main()
{
    int T, w;
    rd(T);
    while(T--)
    {
        init();
        rd(n);
        s = 0, t = n * n + 1;
        for(int i = 0; i < n; i++)
            for(int j = 1; j <= n; j++)
            {
                rd(w);
                if(i == 0)
                {
                    if(j == 1)
                        add(s, i * (n - 1) + j, w), add(i * (n - 1) + j, t, w);
                    else if(j == n)
                        add(i * (n - 1) + (j - 1), t, w);
                    else
                        add(i * (n - 1) + j, i * (n - 1) + (j - 1), w), add(i * (n - 1) + j, t, w);
                }
                else if(i == n - 1)
                {
                    if(j < n)
                        add(s, (i - 1) * (n - 1) + j, w);
                }
                else
                {
                    if(j == 1)
                        add(s, i * (n - 1) + j, w), add(i * (n - 1) + j, (i - 1) * (n - 1) + j, w);
                    else if(j == n)
                        add(i * (n - 1) + (j - 1), t, w);
                    else
                        add(i * (n - 1) + j, i * (n - 1) + (j - 1), w), add(i * (n - 1) + j, (i - 1) * (n - 1) + j, w);
                }
            }
        spfa();
        printf("%d\n", d[t]);
    }

    return 0;
}

 

posted @ 2018-10-13 22:18  WTSRUVF  阅读(171)  评论(0编辑  收藏  举报