传送门:【ZOJ】2676 Network Wars
题目分析:01分数规划+最小割。因为要求r = sigma(x*c/x*k)(x取0或1)最小,那么不妨设g(r) = x*c - r*x*k。由01规划的性质,我们可知:g(r)=0时是最优解,当g(r)<0时r可以继续减小,调整二分上界,当g(r)>0时的r不符合要求,调整二分下界。我不知道网络流中出现负权边会怎么样,不过还是最好不要有,那么每条边变成c-r以后,如果c-r<=0,那么直接加到res里,否则建到图中跑最小割。跑完最小割以后res+=flow,如果res<=0,那么调整上界,否则调整下界。最后要求的边就是最后一次二分时的负权边加上割边,割边可以dfs求出。
表示我的语文已经挂了。。。没听懂的可以看看最小割那啥论文,当然最好看看01分数规划的相关资料。。。
代码如下:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std ; #define REP( i , a , b ) for ( int i = a ; i < b ; ++ i ) #define FOR( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REV( i , a , b ) for ( int i = a - 1 ; 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 ) const int MAXN = 110 ; const int MAXQ = 110 ; const int MAXE = 1000 ; const int INF = 0x3f3f3f3f ; const double eps = 1e-8 ; struct Edge { int v , n ; double c ; Edge () {} Edge ( int v , double c , int n ) : v ( v ) , c ( c ) , n ( n ) {} } ; struct Line { int u , v , c ; void input () { scanf ( "%d%d%d" , &u , &v , &c ) ; } } ; struct NetWork { Edge E[MAXE] ; int H[MAXN] , cntE ; int d[MAXN] , num[MAXN] , pre[MAXN] , cur[MAXN] ; int Q[MAXN] , head , tail ; int s , t , nv ; double flow ; int n , m ; Line L[MAXE] ; int ans[MAXE] ; bool vis[MAXN] ; void init () { cntE = 0 ; CLR ( H , -1 ) ; } void addedge ( int u , int v , double c ) { E[cntE] = Edge ( v , c , H[u] ) ; H[u] = cntE ++ ; E[cntE] = Edge ( u , c , 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] ) continue ; d[v] = d[u] + 1 ; num[d[v]] ++ ; Q[tail ++] = v ; } } } double ISAP () { CPY ( cur , H ) ; rev_bfs () ; flow = 0 ; int u = pre[s] = s ; while ( d[s] < nv ) { if ( u == t ) { double f = INF ; int pos ; for ( int i = s ; i != t ; i = E[cur[i]].v ) if ( f > E[cur[i]].c ) { f = E[cur[i]].c ; pos = i ; } for ( int 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 ( int &i = cur[u] ; ~i ; i = E[i].n ) if ( E[i].c && d[u] == d[E[i].v] + 1 ) break ; if ( ~cur[u] ) { pre[E[cur[u]].v] = u ; u = E[cur[u]].v ; } else { if ( 0 == ( -- num[d[u]] ) ) break ; int mmin = nv ; for ( int 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 ; } int sgn ( double x ) { return ( x > eps ) - ( x < -eps ) ; } int ok ( double mid ) { double res = 0 ; init () ; REP ( i , 0 , m ) { double c = L[i].c - mid ; if ( sgn ( c ) <= 0 ) res += c ; else addedge ( L[i].u , L[i].v , c ) ; } res += ISAP () ; return sgn ( res ) <= 0 ; } void dfs ( int u ) { vis[u] = 1 ; for ( int i = H[u] ; ~i ; i = E[i].n ) if ( sgn ( E[i].c ) && !vis[E[i].v] ) dfs ( E[i].v ) ; } void solve () { s = 1 ; t = n ; nv = t + 1 ; REP ( i , 0 , m ) L[i].input () ; double l = 0 , r = 1e9 , mid ; while ( r - l > eps ) { mid = ( r + l ) / 2 ; if ( ok ( mid ) ) r = mid ; else l = mid ; } int cnt = 0 ; CLR ( vis , 0 ) ; dfs ( 1 ) ; REP ( i , 0 , m ) { if ( sgn ( L[i].c - r ) <= 0 ) ans[cnt ++] = i ; else if ( vis[L[i].u] != vis[L[i].v] ) ans[cnt ++] = i ; } printf ( "%d\n" , cnt ) ; REP ( i , 0 , cnt ) printf ( "%d%c" , ans[i] + 1 , i < cnt - 1 ? ' ' : '\n' ) ; } } x ; int main () { while ( ~scanf ( "%d%d" , &x.n , &x.m ) ) x.solve () ; return 0 ; }