Exemplo n.º 1
0
LL calc(int M) {
	memset(ok, false, sizeof ok);
	for (int j = 0; j < n; ++j)
		if (g[j].second <= M)
			ok[j] = true;
	LL ret = 0;
	mf.init();
	mf.s = n + m + 1;
	mf.t = n + m + 2;
	for (int j = 0; j < m; ++j) {
		bool flg = true;
		for (int k = 0; k < (int)f[j].size(); ++k)
			if (!ok[f[j][k]]) {
				flg = false;
				break;
			}
		if (flg) {
			ret += pro[j];
			mf.addEdge(mf.s, j, pro[j]);
			for (int k = 0; k < (int)f[j].size(); ++k)
				mf.addEdge(j, m + f[j][k], INF);
		}
	}
	for (int j = m; j < n + m; ++j)
		if (ok[j-m])
			mf.addEdge(j, mf.t, g[j-m].first);
	return ret - mf.maxFlow();
}
void build() {
    
    int s = N,  t = N + 1;

    mf.init( N + 2 );
    
    int     v,  w,  c; 
        
    for( int i = 0; i < M; ++i ) {
        while( getchar() != '(' );
        scanf( "%d", &v );
        getchar();
        scanf( "%d", &w );
        getchar();
        scanf( "%d", &c );
        mf.insert( v, w, c );
    }

    for( int i = M; i < M + S; ++i ) {
        while( getchar() != '(' );
        scanf( "%d", &w );
        getchar();
        scanf( "%d", &c );
        mf.insert( s, w, c );
    }

    for( int i = M + S; i < M + S + T; ++i ) {
        while( getchar() != '(' );
        scanf( "%d", &v );
        getchar();
        scanf( "%d", &c );
        mf.insert( v, t, c );
    }

}
void build() {
    
    int s = N,  t = N + 1;

    mf.init( N + 2 );
        
    char    skip;
    
    int     v,  w,  c; 
        
    for( int i = 0; i < M; ++i ) {
        cin >> skip >> v >> skip >> w >> skip >> c;
        mf.insert( v, w, c );
    }

    for( int i = M; i < M + S; ++i ) {
        cin >> skip >> w >> skip >> c;
        mf.insert( s, w, c );
    }

    for( int i = M + S; i < M + S + T; ++i ) {
        cin >> skip >> v >> skip >> c;
        mf.insert( v, t, c );
    }

}
Exemplo n.º 4
0
int main() {
    
    while(scanf("%d%d%d%d", &N, &M, &A, &B) && N) {
        mf.init(N);
        for(int i = 0; i < M; ++i) {
            int x, y, c;
            scanf("%d%d%d", &x, &y, &c);
            x--;y--;
            mf.insert(x, y, c);
            mf.insert(y, x, c);
        }
        puts(mf.uniQ(A-1, B-1) ? "UNIQUE" : "AMBIGUOUS" );
    }
    
}
int main() {
    while(cin >> N >> M) {
        if(T++) puts("");
        int v, w;
        double c, m = 0;
        mf.init(N);
        for(int i = 0; i < M; ++i) {
            cin >> v >> w >> c;
            m = max(m, c);
            mf.insert(v, w, c);
//            mf.insert(w, v, c);
        }
        double l = 0, r = m;
        while(r - l > EPS) {
            m = (l + r) / 2;
            if(check(m)) r = m;
            else l = m;
        }
/*
for(int i = 0; i < M; ++i) {
    cout << mf.buf[i].v << ' ';
    cout << mf.buf[i].w << ' ';
    cout << mf.buf[i].c << ' ';
    cout << mf.buf[i].f << endl;
}
*/
//cout << m << endl;
    memset(B, 0, sizeof(B));
    EE.clear();
    for(int i = 0; i < M; ++i) {
        mf.buf[i].c -= m;
        if(mf.buf[i].c < 0) EE.push_back(i);
    }
    dfs(1);puts("");
    sort(EE.begin(), EE.end());
    EE.resize(unique(EE.begin(), EE.end()) - EE.begin());
        cout << EE.size() << endl;
        for(int i = 0; i < EE.size(); ++i) {
            if(i) cout << ' ';
            cout << EE[i] + 1;
        }
    }
}
bool minCut( double lamda ) {
    mf.init( V );
    now.clear();
    double tot = 0.0;
    for( int i = 0; i < EE.size(); ++i ) {
        if( EE[i].c > lamda )
            mf.insert( EE[i].v-1, EE[i].w-1, EE[i].c - lamda );
        else {
            now.push_back( i + 1 );
            tot += ( EE[i].c - lamda );
        }
    }
    mf.runCut( 0, V - 1 );
    eE = mf.getCutE();
    for( int i = 0; i < eE.size(); ++i ) {
        tot += eE[i] -> c;
        now.push_back( eE[i] - &mf.edges()[0] + 1 );
    }
    sort( now.begin(), now.end() );
    ans = now;
    return  tot < 0.0;
}
int main() {

    for( int T = 0; scanf( "%d%d", &V, &E ) != EOF; ) {

        if( T++ )   puts( "" );

        EE.resize( E );

        mf.init( V ); 

        double  mx = 0.0,   mn = INF; 

        for( int i = 0; i < E; ++i ) {
            scanf( "%d%d%lf", 
                &EE[i].v, &EE[i].w, &EE[i].c );
            mx >?= EE[i].c; 
            mn <?= EE[i].c;
        }

        double  l = mn, r = mx;

        while( r - l > EPS ) {
            double  m = ( l + r ) / 2.0;
            if( minCut( m ) )   r = m; 
            else                l = m; 
        }

        printf( "%d\n", ans.size() );

        for( int i = 0; i < ans.size(); ++i ) {
            if( i ) putchar( ' ' );
            printf( "%d", ans[i] );
        }
        puts( "" );

    }

}
Exemplo n.º 8
0
int main() {
    
    while( scanf( "%d%d", &N, &M ) && N ) {
        
        mf.init( N + M + 2 ); 
        
        for( int i = 1; i <= N; ++i ) {
            mf.insert( 0, i, 1 );
            scanf( "%s", skip );
            while( getchar() != '\n' ) {
                int x;
                scanf( "%d", &x );
                mf.insert( i, N + 1 + x, 1 ); 
            }
        }
        
        st = mf.edges; 
        
        for( int i = 1; i <= M; ++i )
            mf.insert( N + i, N + M + 1, N );
        
        ed = mf.edges; 
        
        int l = N / M,  r = N,  m; 
        
        while( l < r - 1 ) {
            m = ( l + r ) / 2;
            if( check( m ) )    r = m;
            else                l = m;
        } 
        
        printf( "%d\n", r );
        
    }
    
}