标签:费用流
建图:新建一个temp节点,源点像temp连一条流量为k,费用为0的边,表示最多有k条路径。将原图每个点拆为两个点(i,i‘)表示流入该点和流出该点。temp像每个i连边,容量为1,费用为0,表示每个点都可以作为起点。为了保证每个点都被走到,每个的i向i’建边,容量为1,费用为-INF,表示费用非常非常小,它对流非常具有吸引力。每个i’向能去的j建边,容量为1,费用为题意所要求的费用,即i’对应的节点到j对应的节点的距离,相同的话,减去权值,注意要建负边。最后,每个i’向汇点建边,容量为1,费用为0,表示每个点都可以做一条路径的终点。
求解:跑费用流,但与往常的费用流不同的是,我们这里首先是要费用最小,而不是流量最大(因为不一定要把k次都用完)。所以这里判断spfa返回的时候,是看能否让费用继续减小,不能则返回0。因为如果有解的话,第一次spfa肯定就会把所有-INF的边都会被流满(因为它非常具有诱惑力),即所有的点都被走了一遍。最后的答案,先看cost<=n*m*INF? 若果成立,则ans=cost + n * m * INF,否则无解。
代码:
#include<stdio.h> #include<string.h> #include<algorithm> #include<queue> using namespace std ; const int N = 2222 ; const int M = 222222 ; const int INF = 11111111 ; struct Edge { int from , to , next , flow , cap , cost ; Edge () {} Edge (int a,int b,int c,int d,int e) { from = a , to = b , flow = c , cap = d , cost = e ; } } edge[M] ; int head[N] , tot ; void new_edge ( int from , int to , int flow , int cap , int cost ) { edge[tot] = Edge ( from , to , flow , cap , cost ); edge[tot].next = head[from] ; head[from] = tot ++ ; edge[tot] = Edge ( to , from , 0 , 0 , -cost ) ; edge[tot].next = head[to] ; head[to] = tot ++ ; } int vis[N] , dis[N] , pre[N] , add[N] ; queue<int> Q ; int spfa ( int s , int t , int& flow , int& cost , int n ) { int i , u , v ; for ( i = 1 ; i <= n ; i ++ ) dis[i] = INF ; dis[s] = 0 , add[s] = INF ; Q.push ( s ) ; vis[s] = 1 ; while ( !Q.empty () ) { u = Q.front () ; Q.pop () , vis[u] = 0 ; // printf ( "fuck head[%d] = %d\n" , u , head[u] ) ; for ( i = head[u] ; i != -1 ; i = edge[i].next ) { Edge e = edge[i] ; v = e.to ; // printf ( "v = %d\n" , v ) ; if ( e.cap > e.flow && dis[v] > dis[u] + edge[i].cost ) { // printf ( "u = %d , v = %d\n" , u , v ) ; dis[v] = dis[u] + edge[i].cost ; add[v] = min ( add[u] , e.cap - e.flow ) ; pre[v] = i ; if ( !vis[v] ) Q.push (v) , vis[v] = 1 ; } } } // printf ( "dis[%d] = %d\n" , t , dis[t] ) ; if ( dis[t] >= 0 ) return 0 ; flow += add[t] ; cost += add[t] * dis[t] ; // printf ( "flow = %d , cost = %d\n" , flow , cost ) ; u = t ; while ( u != s ) { edge[pre[u]].flow += add[t] ; edge[pre[u]^1].flow -= add[t] ; u = edge[pre[u]].from ; } return 1 ; } int mincost_maxflow ( int s , int t , int n ) { int flow = 0 , cost = 0 ; while ( spfa ( s , t , flow , cost , n ) ) ; // printf ( "flow = %d\n" , flow ) ; return cost ; } char mp[33][33] ; int cnt ; int c[33][33][2] ; int change ( int x , int y , int z ) { if ( !c[x][y][z] ) c[x][y][z] = ++ cnt ; // printf ( "c[%d][%d][%d] = %d\n" , x , y , z , c[x][y][z] ) ; return c[x][y][z] ; } int main () { int n , m , k ; int cas , ca = 0 ; scanf ( "%d" , &cas ) ; while ( cas -- ) { scanf ( "%d%d%d" , &n , &m , &k ) ; for ( int i = 1 ; i <= n ; i ++ ) scanf ( "%s" , mp[i]+1 ) ; cnt = tot = 0 ; memset ( c , 0 , sizeof (c) ) ; memset ( head , -1 , sizeof ( head ) ) ; int s , t , temp ; s = ++ cnt , t = ++ cnt , temp = ++ cnt ; new_edge ( s , temp , 0 , k , 0 ) ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 1 ; j <= m ; j ++ ) { new_edge ( change(i,j,0) , change(i,j,1) , 0 , 1 , -INF ) ; new_edge ( change(i,j,1) , t , 0 , 1 , 0 ) ; new_edge ( temp , change(i,j,0) , 0 , 1 , 0 ) ; for ( int k = j + 1 ; k <= m ; k ++ ) { int add = -(k - j - 1) ; if ( mp[i][j] == mp[i][k] ) add += mp[i][j] - '0' ; new_edge ( change(i,j,1) , change(i,k,0) , 0 , 1 , -add ) ; } for ( int k = i + 1 ; k <= n ; k ++ ) { int add = -(k - i - 1) ; if ( mp[i][j] == mp[k][j] ) add += mp[i][j] - '0' ; new_edge ( change(i,j,1) , change(k,j,0) , 0 , 1 , -add ) ; } } int fuck = mincost_maxflow ( s , t , cnt ) ; printf ( "Case %d : " , ++ ca ) ; if ( fuck > -INF * n * m ) puts ( "-1" ) ; else printf ( "%d\n" , -(fuck + n * m * INF ) ) ; } return 0 ; } /* 111 1 1 1 9 */
标签:费用流
原文地址:http://blog.csdn.net/no__stop/article/details/38064515