天天看點

【POJ】3160 Father Christmas flymouse 強連通+最長路

Father Christmas flymouse
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 2513 Accepted: 838

Description

After retirement as contestant from WHU ACM Team, flymouse volunteered to do the odds and ends such as cleaning out the computer lab for training as extension of his contribution to the team. When Christmas came, flymouse played Father Christmas to give gifts to the team members. The team members lived in distinct rooms in different buildings on the campus. To save vigor, flymouse decided to choose only one of those rooms as the place to start his journey and follow directed paths to visit one room after another and give out gifts en passant until he could reach no more unvisited rooms.

During the days on the team, flymouse left different impressions on his teammates at the time. Some of them, like LiZhiXu, with whom flymouse shared a lot of candies, would surely sing flymouse’s deeds of generosity, while the others, like snoopy, would never let flymouse off for his idleness. flymouse was able to use some kind of comfort index to quantitize whether better or worse he would feel after hearing the words from the gift recipients (positive for better and negative for worse). When arriving at a room, he chould choose to enter and give out a gift and hear the words from the recipient, or bypass the room in silence. He could arrive at a room more than once but never enter it a second time. He wanted to maximize the the sum of comfort indices accumulated along his journey.

Input

The input contains several test cases. Each test cases start with two integersN and M not exceeding 30 000 and 150 000 respectively on the first line, meaning that there wereN team members living in N distinct rooms and M direct paths. On the nextN lines there are N integers, one on each line, the i-th of which gives the comfort index of the words of the team member in thei-th room. Then follow M lines, each containing two integers i and j indicating a directed path from the i-th room to thej-th one. Process to end of file.

Output

For each test case, output one line with only the maximized sum of accumulated comfort indices.

Sample Input

2 2
14
21
0 1
1 0      

Sample Output

35      

Hint

32-bit signed integer type is capable of doing all arithmetic.

Source

POJ Monthly--2006.12.31, Sempr

傳送門:【POJ】3160 Father Christmas flymouse

題目分析:點權最長路,一個分量裡面的權值可能有負,直接忽略。

代碼如下:

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

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

const int MAXN = 30005 ;
const int MAXE = 150005 ;
const int MAXQ = 150005 ;

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

struct SCC {
	Edge edge[MAXE] ;
	int adj[MAXN] , cntE ;
	int Dfn[MAXN] , Low[MAXN] , dfs_clock ;
	int scc[MAXN] , scc_cnt ;
	int S[MAXN] , top ;
	bool ins[MAXN] ;
	
	void init () {
		top = 0 ;
		cntE = 0 ;
		scc_cnt = 0 ;
		dfs_clock = 0 ;
		clear ( ins , 0 ) ;
		clear ( Dfn , 0 ) ;
		clear ( adj , -1 ) ;
	}
	
	void addedge ( int u , int v ) {
		edge[cntE] = Edge ( v , 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 ;
			}
		}
	}
	
	void find_scc ( int n ) {
		REP ( i , n )
			if ( !Dfn[i] )
				Tarjan ( i ) ;
	}
} ;

struct DAG {
	Edge edge[MAXE] ;
	int adj[MAXN] , cntE ;
	int in[MAXN] ;
	int d[MAXN] ;
	int cnt[MAXN] ;
	int Q[MAXQ] ;
	int head , tail ;
	
	void init () {
		cntE = 0 ;
		clear ( in , 0 ) ;
		clear ( cnt , 0 ) ;
		clear ( adj , -1 ) ;
	}
	
	void addedge ( int u , int v ) {
		edge[cntE] = Edge ( v , adj[u] ) ;
		adj[u] = cntE ++ ;
	}
	
	int topo ( int n ) {
		head = tail = 0 ;
		clear ( d , 0 ) ;
		REPF ( i , 1 , n )
			if ( !in[i] )
				d[Q[tail ++] = i] = cnt[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] + cnt[v] )
					d[v] = d[u] + cnt[v] ;
				if ( 0 == ( -- in[v] ) )
					Q[tail ++] = v ;
			}
		}
		int ans = 0 ;
		REPF ( i , 1 , n )
			if ( ans < d[i] )
				ans = d[i] ;
		return ans ;
	}
} ;	

SCC C ;
DAG D ;
int val[MAXN] ;

int read ( int &x ) {
	x = 0 ;
	int flag = 0 ;
	char c = ' ' ;
	while ( c != '-' && ( c < '0' || c > '9' ) )
		c = getchar () ;
	if ( c == '-' ) {
		flag = 1 ;
		c = getchar () ;
	}
	while ( c >= '0' && c <= '9' ) {
		x = x * 10 + c - '0' ;
		c = getchar () ;
	}
	if ( flag )
		x = -x ;
	return 1 ;
}

void work () {
	int n , m ;
	int u , v ;
	while ( ~scanf ( "%d%d" , &n , &m ) ) {
		C.init () ;
		D.init () ;
		REP ( i , n )
			read ( val[i] ) ;
		while ( m -- ) {
			read ( u ) , read ( v ) ;
			C.addedge ( u , v ) ;
		}
		C.find_scc ( n ) ;
		REP ( u , n )
			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] ) ;
					++ D.in[C.scc[v]] ;
				}
			}
		REP ( i , n )
			if ( val[i] > 0 )
				D.cnt[C.scc[i]] += val[i] ;
		printf ( "%d\n" , D.topo ( C.scc_cnt ) ) ;
	}
}

int main () {
	work () ;
	return 0 ;
}