【强连通分量】Instantaneous Transference

来源:互联网 发布:淘宝上怎么找人帮代卖 编辑:程序博客网 时间:2024/05/22 12:36
DescriptionIt was long ago when we played the game Red Alert. There is a magic function for the game objectswhich is called instantaneous transfer. When an object uses this magic function, it will betransferred to the specified point immediately, regardless of how far it is.Now there is a mining area, and you are driving an ore-miner truck. Your mission is to take themaximum ores in the field.The ore area is a rectangle region which is composed by n × m small squares, some of the squareshave numbers of ores, while some do not. The ores can't be regenerated after taken.The starting position of the ore-miner truck is the northwest corner of the field. It must move tothe eastern or southern adjacent square, while it can not move to the northern or western adjacentsquare. And some squares have magic power that can instantaneously transfer the truck to a certainsquare specified. However, as the captain of the ore-miner truck, you can decide whether to use thismagic power or to stay still. One magic power square will never lose its magic power; you can usethe magic power whenever you get there.InputThe first line of the input is an integer T which indicates the number of test cases.For each of the test case, the first will be two integers N, M (2 ≤ N, M ≤ 40).The next N lines will describe the map of the mine field. Each of the N lines will be a string thatcontains M characters. Each character will be an integer X (0 ≤ X ≤ 9) or a '*' or a '#'. Theinteger X indicates that square has X units of ores, which your truck could get them all. The '*'indicates this square has a magic power which can transfer truck within an instant. The '#' indicatesthis square is full of rock and the truck can't move on this square. You can assume that the startingposition of the truck will never be a '#' square.As the map indicates, there are K '*' on the map. Then there follows K lines after the map. The nextK lines describe the specified target coordinates for the squares with '*', in the order from northto south then west to east. (the original point is the northwest corner, the coordinate is formattedas north-south, west-east, all from 0 to N - 1,M - 1).OutputFor each test case output the maximum units of ores you can take.  Sample Input12 2111*0 0Sample Output3

先强连通分量缩点,然后SPFA之。

注意数组清零的问题。

Accode:

#include <cstdio>#include <cstdlib>#include <cmath>#include <algorithm>#include <string>#include <queue>const int maxR = 50;const int maxN = 2010;const int SIZE = 0xffff;struct vec{    int x, y;    vec() {}    vec(int x, int y): x(x), y(y) {}};struct Edge {int v; Edge *next;};Edge *edge[maxN];char mp[maxR][maxR];bool marked[maxR][maxR], visit[maxN];int belong[maxR][maxR];int DFN[maxR][maxR], Low[maxR][maxR];int ord[maxR][maxR], cnt[maxN];int dist[maxN], q[SIZE + 1];vec to[maxN], stack[maxN];int t, n, m, K, top, Index, Bcnt, ans, f, r;void tarjan(int x, int y){    if (mp[x][y] == '#') return;    DFN[x][y] = Low[x][y] = ++Index;    stack[++top] = vec(x, y);    marked[x][y] = 1;    if (y < m - 1 && mp[x][y + 1] != '#')    {        int u = x, v = y + 1;        if (!DFN[u][v])        {            tarjan(u, v);            if (Low[u][v] < Low[x][y])                Low[x][y] = Low[u][v];        }        else if (marked[u][v] && DFN[u][v] < Low[x][y])//开始在这里打错一个变量,//把marked[u][v]打成了marked[x][y],调了很久。            Low[x][y] = DFN[u][v];    }    if (x < n - 1 && mp[x + 1][y] != '#')    {        int u = x + 1, v = y;        if (!DFN[u][v])        {            tarjan(u, v);            if (Low[u][v] < Low[x][y])                Low[x][y] = Low[u][v];        }        else if (marked[u][v] && DFN[u][v] < Low[x][y])            Low[x][y] = DFN[u][v];    }    if (mp[x][y] == '*')    {        int u = to[ord[x][y]].x, v = to[ord[x][y]].y;        if (mp[u][v] != '#') //注意要有边才能枚举。        {            if (!DFN[u][v])            {                tarjan(u, v);                if (Low[u][v] < Low[x][y])                    Low[x][y] = Low[u][v];            }            else if (marked[u][v] &&                     DFN[u][v] < Low[x][y])                Low[x][y] = DFN[u][v];        }    }    if (DFN[x][y] == Low[x][y])    {        ++Bcnt;        vec tmp;        do        {            tmp = stack[top--];            marked[tmp.x][tmp.y] = 0;            belong[tmp.x][tmp.y] = Bcnt;            if (isdigit(mp[tmp.x][tmp.y]))                cnt[Bcnt] += mp[tmp.x][tmp.y] - '0';        } while (tmp.x != x || tmp.y != y);    }    return;}inline void init(){    memset(marked, 0, sizeof marked);    memset(visit, 0, sizeof visit);    memset(belong, 0, sizeof belong);    memset(cnt, 0, sizeof cnt);    memset(DFN, 0, sizeof DFN);    memset(edge, 0, sizeof edge);    K = n = m = top = Index = Bcnt = ans = f = r = 0;    return;}inline int Spfa(){    memset(dist, ~0x3f, sizeof dist);    int ans = cnt[belong[0][0]];    dist[belong[0][0]] = ans;    visit[belong[0][0]] = 1;    q[r++] = belong[0][0];    f &= SIZE;    while (f != r)    {        int u = q[f++];        f &= SIZE;        visit[u] = 0;        for (Edge *p = edge[u]; p; p = p -> next)        if (dist[u] + cnt[p -> v] > dist[p -> v])        {            int v = p -> v;            if ((dist[v] = dist[u] + cnt[v]) > ans)                ans = dist[v];            if (!visit[v])            {                visit[v] = 1;                q[r++] = v;                r &= SIZE;            }                        }    }    return ans;}inline void insert(int u, int v){    if (u == v) return;    for (Edge *p = edge[u]; p; p = p -> next)        if (p -> v == v) return;//注意重新建图时的判重,以减少枚举量。    Edge *p = new Edge;    p -> v = v;    p -> next = edge[u];    edge[u] = p;    return;}int main(){    freopen("Instantaneous_Transference.in", "r", stdin);    freopen("Instantaneous_Transference.out", "w", stdout);    scanf("%d", &t);    for (; t; --t)    {        init();        scanf("%d%d\n", &n, &m);        for (int i = 0; i < n; ++i)        {            gets(mp[i]);            for (int j = 0; j < m; ++j)            if (mp[i][j] == '*')                ord[i][j] = K++;        }        for (int i = 0; i < K; ++i)        {            int x, y;            scanf("%d%d", &x, &y);            to[i] = vec(x, y);        }        tarjan(0, 0);        for (int i = 0; i < n; ++i)        for (int j = 0; j < m; ++j)        {            if (j < m - 1 && mp[i][j + 1] != '#')                insert(belong[i][j], belong[i][j + 1]);            if (i < n - 1 && mp[i + 1][j] != '#')                insert(belong[i][j], belong[i + 1][j]);            if (mp[i][j] == '*' &&                mp[to[ord[i][j]].x][to[ord[i][j]].y] != '#')                insert(belong[i][j],                       belong[to[ord[i][j]].x][to[ord[i][j]].y]);//注意实际无边的情况。        }        printf("%d\n", Spfa());    }    return 0;}

原创粉丝点击