【POJ】2987 Firing 最大权闭合子图
2014-08-03 13:44
423 查看
传送门:【POJ】2987 Firing
题目分析:最大权闭合子图模板题。。。
正权点和源点建边,容量为权值,负权点和汇点建边,容量为权值的绝对值,然后对所有关系<i,j>,建边( i , j , INF ),最后跑一遍最小割就是第二个答案。那么第一个答案怎么解决?我们可以通过dfs一遍得到需要的答案!dfs一次标记从源点能到达的点,由于最大权闭合子图的性质,从源点能到达的点都是在最大权闭合子图中的点,dfs一次后,扫一遍所有点就能得到第一个答案。
PS:奇怪。。。bfs没有dfs快。。
代码如下:
题目分析:最大权闭合子图模板题。。。
正权点和源点建边,容量为权值,负权点和汇点建边,容量为权值的绝对值,然后对所有关系<i,j>,建边( i , j , INF ),最后跑一遍最小割就是第二个答案。那么第一个答案怎么解决?我们可以通过dfs一遍得到需要的答案!dfs一次标记从源点能到达的点,由于最大权闭合子图的性质,从源点能到达的点都是在最大权闭合子图中的点,dfs一次后,扫一遍所有点就能得到第一个答案。
PS:奇怪。。。bfs没有dfs快。。
代码如下:
#include <cmath> #include <cstdio> #include <cstring> #include <algorithm> using namespace std ; #define REP( i , a , b ) for ( int i = a ; i < b ; ++ i ) #define REV( i , a , b ) for ( int i = a - 1 ; i >= b ; -- i ) #define FOR( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define FOV( i , a , b ) for ( int i = a ; i >= b ; -- i ) #define CLR( a , x ) memset ( a , x , sizeof a ) #define CPY( a , x ) memcpy ( a , x , sizeof a ) typedef long long LL ; typedef int type_c ; typedef LL type_f ; const int MAXN = 5005 ; const int MAXQ = 5005 ; const int MAXE = 150000 ; const int INF = 0x3f3f3f3f ; struct Edge { int v , n ; type_c c , rc ; Edge () {} Edge ( int v , type_c c , int n ) : v ( v ) , c ( c ) , n ( n ) {} } ; struct Net { Edge E[MAXE] ; int H[MAXN] , cntE ; int d[MAXN] , cur[MAXN] , pre[MAXN] , num[MAXN] ; int Q[MAXQ] , head , tail ; int s , t , nv ; type_f flow ; int n , m ; bool vis[MAXN] ; void init () { cntE = 0 ; CLR ( H , -1 ) ; } void addedge ( int u , int v , type_c c , type_c rc = 0 ) { E[cntE] = Edge ( v , c , H[u] ) ; H[u] = cntE ++ ; E[cntE] = Edge ( u , rc , H[v] ) ; H[v] = cntE ++ ; } void rev_bfs () { CLR ( d , -1 ) ; CLR ( num , 0 ) ; head = tail = 0 ; Q[tail ++] = t ; d[t] = 0 ; num[d[t]] = 1 ; while ( head != tail ) { int u = Q[head ++] ; for ( int i = H[u] ; ~i ; i = E[i].n ) { int v = E[i].v ; if ( d[v] == -1 ) { Q[tail ++] = v ; d[v] = d[u] + 1 ; num[d[v]] ++ ; } } } } type_f ISAP () { CPY ( cur , H ) ; rev_bfs () ; flow = 0 ; int u = pre[s] = s , i , pos , mmin ; while ( d[s] < nv ) { if ( u == t ) { type_f f = INF ; for ( i = s ; i != t ; i = E[cur[i]].v ) if ( f > E[cur[i]].c ) { f = E[cur[i]].c ; pos = i ; } for ( i = s ; i != t ; i = E[cur[i]].v ) { E[cur[i]].c -= f ; E[cur[i] ^ 1].c += f ; } u = pos ; flow += f ; } for ( i = cur[u] ; ~i ; i = E[i].n ) if ( E[i].c && d[u] == d[E[i].v] + 1 ) break ; if ( ~i ) { cur[u] = i ; pre[E[i].v] = u ; u = E[i].v ; } else { if ( 0 == -- num[d[u]] ) break ; for ( mmin = nv , i = H[u] ; ~i ; i = E[i].n ) if ( E[i].c && mmin > d[E[i].v] ) { mmin = d[E[i].v] ; cur[u] = i ; } d[u] = mmin + 1 ; num[d[u]] ++ ; u = pre[u] ; } } return flow ; } void dfs ( int u ) { vis[u] = 1 ; for ( int i = H[u] ; ~i ; i = E[i].n ) if ( !vis[E[i].v] && E[i].c ) dfs ( E[i].v ) ; } void solve () { int u , v , c ; LL sum = 0 ; init () ; s = 0 , t = n + 1 , nv = t + 1 ; FOR ( i , 1 , n ) { scanf ( "%d" , &c ) ; if ( c > 0 ) sum += c , addedge ( s , i , c ) ; if ( c < 0 ) addedge ( i , t , -c ) ; } REP ( i , 0 , m ) { scanf ( "%d%d" , &u , &v ) ; addedge ( u , v , INF ) ; } ISAP () ; CLR ( vis , 0 ) ; dfs ( s ) ; int cnt = 0 ; FOR ( i , 1 , n ) if ( vis[i] ) ++ cnt ; printf ( "%d %I64d\n" , cnt , sum - flow ) ; } } e ; int main () { while ( ~scanf ( "%d%d" , &e.n , &e.m ) ) e.solve () ; return 0 ; }
相关文章推荐
- poj 2987 Firing【最大闭合子图】
- POJ 2987 Firing (最大权闭合子图Dinic)
- POJ 2987 Firing 最小割(最大权闭合子图)
- 【POJ 2987】Firing (最小割-最大权闭合子图)
- poj 2987 Firing 最大权闭合子图
- 最大权闭合子图(poj 2987 Firing)
- poj 2987 Firing 最大权闭合子图
- POJ 2987 Firing 最大权闭合子图
- poj 2987 Firing 最大权闭合子图
- POJ 2987 - Firing 比较完善的最大权闭合子图..维护两个最优值...
- poj 2987 Firing【最大权闭合子图+玄学计数 || BFS】
- POJ 2987 Firing 最大权闭合子图
- poj 2987 Firing (最大权闭合子图)
- POJ 2987 Firing(最大权闭合子图)
- POJ 2987 Firing(最大权闭合图)
- poj 2987 Firing 最大权闭合图
- Firing (poj 2987 最大权闭合图)
- POJ 2987 Firing 最大权闭合图
- POJ 2987 Firing(最大流最小割の最大权闭合图)
- poj 2987 Firing 最大权闭合图