【POJ】3592 Instantaneous Transference 强连通+最长路

Instantaneous Transference
Time Limit: 5000MS
Memory Limit: 65536K
Total Submissions: 5581
Accepted: 1237

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

Source

South Central China 2008 hosted by NUDT

传送门:【POJ】3592 Instantaneous Transference

题目大意:给你一个n行m列的网格图,每个格子有一个权值,且第一次路过该格子可以获得该权值(第二次或之后便得不到权值),一开始你在最左上角位置,且只能向右或下行进,' # '表示障碍物不可到达,' . '及' * '均可到达。其中 ' * '是传送门,当你走到它所在的格子上时可以传送到它对应的坐标或者不传送,传送门永久有效。现在问你从左上角出发能得到的最多的权值是多少。
注意' * '从上到下,从左到右,依次编号增加。

题目分析:其实题目意思挺裸的,就是强连通缩点+最长路。
那么我的做法是BFS出哪些点可达,哪些点不可达,然后以此建立新图,同时将一个点拆成两个,中间连一条边,该边的权值就是原来的点权,其余边的边权均为0,然后只对0点做强连通(其他的做了也白做),得到分量后建立DAG图,每个分量的点权就是之前的边权 = =||(真作死,转化成边权又转化回点权),然后分量之间的就是边权,在DAG上跑最长路就行了。。。主要是一开始BFS里面少写了一个vis[ ][ ] = 1。。导致极限情况会错。。。

其实我做了很多没必要的工作。。。。。。。只怪我脑洞太小,为什么非要把点权添加到边里面。。。。。发现了我也懒得改了,反正我觉得这样做以后感觉思路还是很清晰的。。

就这样吧。。。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;

#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define clear( a , x ) memset ( a , x , sizeof a )

const int MAXG = 50 ;
const int MAXN = 3300 ;
const int MAXE = 10000 ;
const int MAXQ = 10000 ;
const int INF = 0x3f3f3f3f ;

struct Edge {
	int v , n , c ;
	Edge ( int var = 0 , int cost = 0 , int next = 0 ) :
		v ( var ) , c ( cost ) , n ( next ) {}
} ;

struct SCC {
	Edge edge[MAXE] ;
	int adj[MAXN] , cntE ;
	int Dfn[MAXN] , Low[MAXN] , ins[MAXN] , scc[MAXN] ;
	int dfs_clock , scc_cnt ;
	int S[MAXN] , top ;
	
	void init () {
		top = 0 ;
		cntE = 0 ;
		scc_cnt = 0 ;
		dfs_clock = 0 ;
		clear ( scc , 0 ) ;
		clear ( Dfn , 0 ) ;
		clear ( ins , 0 ) ;
		clear ( adj , -1 ) ;
	}
	
	void addedge ( int u , int v , int c ) {
		edge[cntE] = Edge ( v , c , adj[u] ) ;
		adj[u] = cntE ++ ;
	}
	
	void Tarjan ( int u ) {
		Dfn[u] = Low[u] = ++ dfs_clock ;
		S[top ++] = u ;
		ins[u] = 1 ;
		for ( int i = adj[u] ; ~i ; i = edge[i].n ) {
			int v = edge[i].v ;
			if ( !Dfn[v] ) {
				Tarjan ( v ) ;
				Low[u] = min ( Low[u] , Low[v] ) ;
			}
			else if ( ins[v] )
				Low[u] = min ( Low[u] , Dfn[v] ) ;
		}
		if ( Low[u] == Dfn[u] ) {
			++ scc_cnt ;
			while ( 1 ) {
				int v = S[-- top] ;
				ins[v] = 0 ;
				scc[v] = scc_cnt ;
				if ( v == u )
					break ;
			}
		}
	}
} ;

struct DAG {
	Edge edge[MAXE] ;
	int adj[MAXN] , cntE ;
	int d[MAXN] , in[MAXN] ;
	int Q[MAXQ] ;
	int head , tail ;
	int ores[MAXN] ;
	
	void init () {
		cntE = 0 ;
		clear ( ores , 0 ) ;
		clear ( in , 0 ) ;
		clear ( adj , -1 ) ;
	}
	
	void addedge ( int u , int v , int c ) {
		edge[cntE] = Edge ( v , c , adj[u] ) ;
		adj[u] = cntE ++ ;
	}
	
	void solve ( int n ) {
		clear ( d , 0 ) ;
		head = tail = 0 ;
		REPF ( i , 1 , n )
			if ( !in[i] )
				d[i] = ores[i] , Q[tail ++] = i ;
		while ( head != tail ) {
			int u = Q[head ++] ;
			for ( int i = adj[u] ; ~i ; i = edge[i].n ) {
				int v = edge[i].v ;
				if ( d[v] < d[u] + edge[i].c + ores[v] ) {
					d[v] = d[u] + edge[i].c + ores[v] ;
//					printf ( "d[%d] = %d = %d + %d\n" , v , d[v] , d[u] , edge[i].c ) ;
				}
				if ( 0 == ( --in[v] ) )
					Q[tail ++] = v ;
			}
		}
		int ans = 0 ;
		REPF ( i , 1 , n )
			if ( d[i] > ans )
				ans = d[i] ;
		printf ( "%d\n" , ans ) ;
	}
} ;

SCC C ;
DAG D ;
int Q[MAXQ] ;
int head , tail ;
int magic[MAXG][MAXG] ;
int X[MAXN] , Y[MAXN] ;
char G[MAXG][MAXG] ;
bool vis[MAXG][MAXG] ;
int path[2][2] = { { 1 , 0 } , { 0 , 1 } } ;
int n , m , nm ;

void read ( int &x ) {
	x = 0 ;
	char c = ' ' ;
	while ( c < '0' || c > '9' )
		c = getchar () ;
	while ( c >= '0' && c <= '9' )
		x = x * 10 + c - '0' , c = getchar () ;
}

void bfs () {
	head = tail = 0 ;
	Q[tail ++] = 0 ;
	vis[0][0] = 1 ;
	while ( head != tail ) {
		int u = Q[head ++] ;
		int x = u / m ;
		int y = u % m ;
		if ( ~magic[x][y] ) {
			int nx = X[magic[x][y]] ;
			int ny = Y[magic[x][y]] ;
			if ( !vis[nx][ny] && G[nx][ny] != '#' ) {
				vis[nx][ny] = 1 ;
				Q[tail ++] = X[magic[x][y]] * m + Y[magic[x][y]] ;
			}
		}
		REP ( k , 2 ) {
			int nx = x + path[k][0] ;
			int ny = y + path[k][1] ;
			if ( nx < n && ny < m && !vis[nx][ny] && G[nx][ny] != '#' ) {
				vis[nx][ny] = 1 ;
				Q[tail ++] = nx * m + ny ;
			}
		}
	}
}

void work () {
	int x , y ;
	int cnt = 0 ;
	read ( n ) , read ( m ) ;
	nm = n * m ;
	C.init () ;
	D.init () ;
	clear ( vis , 0 ) ;
	clear ( magic , -1 ) ;
	REP ( i , n ) {
		scanf ( "%s" , G[i] ) ;
		REP ( j , m )
			if ( G[i][j] == '*' )
				magic[i][j] = cnt ++ ;
	}
	REP ( i , cnt )
		read ( X[i] ) , read ( Y[i] ) ;
	bfs () ;	//proProcess
	REP ( i , n )
		REP ( j , m )
			if ( vis[i][j] ) {
				if ( ~magic[i][j] )	//if magic[i][j] != -1, it must be visit.
					C.addedge ( i * m + j + nm , X[magic[i][j]] * m + Y[magic[i][j]] , 0 ) ;
				if ( vis[i + 1][j] )
					C.addedge ( i * m + j + nm , i * m + j + m , 0 ) ;
				if ( vis[i][j + 1] )
					C.addedge ( i * m + j + nm , i * m + j + 1 , 0 ) ;
			}
	REP ( i , n )
		REP ( j , m )
			if ( vis[i][j] )
				C.addedge ( i * m + j , i * m + j + nm , G[i][j] == '*' ? 0 : G[i][j] - '0' ) ;
//	REP ( i , n )
//		REP ( j , m )
//			printf ( "%d%c" , vis[i][j] , j < m - 1 ? ' ' : '\n' ) ;
	C.Tarjan ( 0 ) ;
	REP ( u , nm << 1 )
		if ( u >= nm && vis[( u - nm ) / m][( u - nm ) % m] || u < nm && vis[u / m][u % m] )
			for ( int i = C.adj[u] ; ~i ; i = C.edge[i].n ) {
				int v = C.edge[i].v ;
				if ( C.scc[u] != C.scc[v] ) {
					D.addedge ( C.scc[u] , C.scc[v] , C.edge[i].c ) ;
					++ D.in[C.scc[v]] ;
				}
				else
					D.ores[C.scc[u]] += C.edge[i].c ;
			}
	REP ( u , nm )
		if ( !vis[u / m][u % m] )
			D.in[C.scc[u]] = D.in[C.scc[u + nm]] = INF ;
//	printf ( "scc_cnt = %d\n" , C.scc_cnt ) ;
//	REPF ( i , 1 , C.scc_cnt )
//		printf ( "ores[%d] = %d\n" , i , D.ores[i] ) ;
	D.solve ( C.scc_cnt ) ;
}

int main () {
	int T ;
	read ( T ) ;
	while ( T -- )
		work () ;
	return 0 ;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值