zoukankan      html  css  js  c++  java
  • [题解] poj 3592 Instantaneous Transference (tarjan强连通分量 + SPFA最长路径)

    - 传送门 -

     http://poj.org/problem?id=3592

    #Instantaneous Transference

    | Time Limit: 5000MS |   | Memory Limit: 65536K |
    | Total Submissions: 6863 |   | Accepted: 1552 |

    Description

    It was long ago when we played the game Red Alert. There is a magic function for the game objects which is called instantaneous transfer. When an object uses this magic function, it will be transferred 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 the maximum ores in the field.

    The ore area is a rectangle region which is composed by n × m small squares, some of the squares have 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 to the eastern or southern adjacent square, while it can not move to the northern or western adjacent square. And some squares have magic power that can instantaneously transfer the truck to a certain square specified. However, as the captain of the ore-miner truck, you can decide whether to use this magic power or to stay still. One magic power square will never lose its magic power; you can use the magic power whenever you get there.

    Input

    The 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 NM (2 ≤ NM ≤ 40).

    The next N lines will describe the map of the mine field. Each of the N lines will be a string that contains M characters. Each character will be an integer X (0 ≤ X ≤ 9) or a '' or a '#'. The integer 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 '#' indicates this square is full of rock and the truck can't move on this square. You can assume that the starting position 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 next K lines describe the specified target coordinates for the squares with '', in the order from north to south then west to east. (the original point is the northwest corner, the coordinate is formatted as north-south, west-east, all from 0 to N - 1,_M _- 1).

    Output

    For each test case output the maximum units of ores you can take.  

    Sample Input

    1
    2 2
    11
    1*
    0 0

    Sample Output

    3

    [[Submit](http://poj.org/submit?problem_id=3592)]  [[Status](http://poj.org/problemstatus?problem_id=3592)]   [[Discuss](http://poj.org/bbs?problem_id=3592)]
      ### - 题意 -  一个 n 行 m 列的图, 每次可以从其上任意一个格子向下或向右走一步, 格子中分别有:  0~9:表示该点的权值.  # : 表示该点不可到达.  * : 表示该点可以跳跃到另一个格子上(也可以不跳跃, 向下或向右走).  对于 * 的格子, 输入会在给出图后, 以此(先优先靠上的, 再优先靠左的)给出这些格子可以跳跃到的格子坐标(行列).  从左上角出发, 问能得到最大权值和是多少.  (多组数据, 注意题目默认行为[0, n - 1], 列[0, m - 1])   ### - 思路 -  tarjan缩点, SPFA求权值最长的路径.    细节见代码.    PS: * 所在的格子可能跳跃到 # 的格子上, 这种情况是不可行的.    卡了很久, 问题在于, 如果算出每个强连通分量的权值就把它赋给这个新点的ans数组的话(即SPFA中dis数组), 判断时要用ans[v] <= ans[i] + sz[i], 不能漏等于, (没有赋初值就可以这样过), 不过下面的代码没有这样写.    同时注意多组数据间的数据的初始化.  

    - 代码 -

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    using namespace std;
    
    const int N = 1600 + 5;
    
    vector<int> SCC[N];
    
    bool B[N];
    int QUE[N];
    int DFN[N], LOW[N], STK[N];
    int BL[N], SZ[N], ANS[N];
    int HD[N], NXT[N*3], TO[N*3];
    int A[N];
    int n, m, maxn, head, tail, T;
    int sz, cnt, tot, nu, tp;
    char Ch[45][45], ch;
    
    int pos (int x, int y) { return x*m + y; }
    
    void init() {
    	sz = cnt = tot = nu = tp = 0;
    	memset(HD, 0, sizeof (HD));
    	memset(BL, 0, sizeof (BL));
    	memset(DFN, 0, sizeof (DFN));
    	memset(ANS, 0, sizeof (ANS));
    	memset(B, false, sizeof (B));
    }
    
    void add(int x, int y) {
    	TO[++sz] = y; NXT[sz] = HD[x]; HD[x] = sz;
    }
    
    void get(int &x) {
    	char ch;
    	do {
    		ch = getchar();
    		if (ch == '*' || ch == '#' || (ch >= '0' && ch <= '9'))
    		break;
    	}while(true);
    	if (ch == '*') x = -2, nu ++;
    	else if (ch == '#') x = -1;
    	else x = ch - '0';
    }
    
    void tarjan(int x) {
    	int v;
    	LOW[x] = DFN[x] = ++tot;
    	STK[++tp] = x;
    	for (int i = HD[x]; i; i = NXT[i]) {
    		v = TO[i];
    		if (!DFN[v]) { tarjan(v); LOW[x] = min(LOW[x], LOW[v]); }
    		else if (!BL[v]) LOW[x] = min(LOW[x], DFN[v]);
    	}
    	if (DFN[x] == LOW[x]) {
    		cnt ++; SZ[cnt] = 0;
    		do {
    			v = STK[tp--];
    			BL[v] = cnt;
    			SCC[cnt].push_back(v);
    			if (A[v] != -2) SZ[cnt] += A[v]; //特判*的情况
    		}while (v != x);
    	}
    }
    
    void SPFA() {
    	QUE[tail = 1] = BL[0];
    	head = 0; ANS[BL[0]] = SZ[BL[0]];
    	while (head < tail) {
    		int i = QUE[++head], len = SCC[i].size();
    		maxn = max(maxn, ANS[i]); //对可以到达的点进行比较
    		B[i] = false;
    		for (int j = 0; j < len; ++j) {
    				for (int k = HD[SCC[i][j]]; k; k = NXT[k]) {
    					int v = BL[TO[k]];
    					if (v != i && ANS[i] + SZ[v] > ANS[v]) {
    							ANS[v] = ANS[i] + SZ[v];
    							if (!B[v]) { B[v] = true; QUE[++tail] = v; }
    					}
    				}
    		}
    	}
    }
    
    int main() {
    	scanf("%d", &T);
    	for (int i = 1; i <= T; ++i) {
    		init();
    		scanf("%d%d", &n, &m);
    		ch = getchar();
    		maxn = n * m;
    		for (int i = 0; i < n; ++i)
    			for (int j = 0; j < m; ++j) {
    				int p = pos(i, j);
    				get(A[p]);
    				if (A[p] == -1) continue;
    				if (p % m != 0 && A[p - 1] != -1) add(p - 1, p);
    				if (p >= m && A[p - m] != -1) add(p - m, p);
    			}
    		for (int i = 0, x, y; i < maxn; ++i)
    			if (A[i] == -2) {
    				scanf("%d%d", &x, &y);
    				int p = pos(x, y);
    				if (A[p] != -1 && p < maxn) add(i, p);
    			}
    		for (int i = 0; i < maxn; ++i)
    			if (!DFN[i] && A[i] != -1) tarjan(i); //特判#的情况
    		maxn = 0;
    		SPFA();
    		printf("%d
    ", maxn);
    		for (int i = 1; i <= cnt; ++i) SCC[i].clear();
    	}
    	return 0;
    }
    
    
  • 相关阅读:
    Android Interactive Animation
    Android 笔记
    java 从零开始 第三天
    RGB颜色查询对照表
    Android TextView文字过多时通过滚动条显示多余内容
    Android系统字体规范
    Android 动画之TranslateAnimation应用详解
    Python--day69--ORM的F查询和Q查询
    Python--day69--ORM聚合查询和分组查询
    Python--day69--ORM正反向查找(外键)
  • 原文地址:https://www.cnblogs.com/Anding-16/p/7345909.html
Copyright © 2011-2022 走看看