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 );
    }

}
Beispiel #2
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 );
    }

}
int main(int argc, char* argv[]) {

    MaxFlow obj;
    if(obj.Parser(argv[1])) return EXIT_FAILURE; //convenzione UNIX: 0 significa ok, 1 significa male
    //cout<<" V "<<obj.getV()<<" E "<<obj.getE()<<" s "<<obj.gets()<<" t "<<obj.gett()<<endl;
    //ogg.debugger();
    obj.calculatemaxflow();
    //ogg.debugger();
    //cout << "Max flow s -> t  : " << obj.getflow() << endl;  
    cout << obj.getflow() << endl;
    return EXIT_SUCCESS;
}
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" );
    }
    
}
Beispiel #6
0
void dfs(int u, MaxFlow& mf) {
	visited[u] = true;
	numbers.insert(u);
	vector<int> next = next_numbers(u);
	for(int v : next) {
		if(!visited[v])
			dfs(v,mf);
		mf.addEdge(u,v+L,1);
	}
}
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;
        }
    }
}
int main() {
    
    while( scanf( "%d%d%d%d", &N, &S, &T, &M ) != EOF ) {
        
        build();
        
        printf( "%d\n", mf.maxFlow( N, N + 1 ) );
  
    }
    
}
bool check(double lamda) {
    double ans;
    for(int i = 0; i < M; ++i) {
        mf.buf[i].c -= lamda;
        if(mf.buf[i].c < 0) ans += mf.buf[i].c;
    }
    ans += mf.maxFlow(1, N);
    for(int i = 0; i < M; ++i)
        mf.buf[i].c += lamda;
    return ans < 0;
}
int main() {
    
    while( cin >> N >> S >> T >> M ) {
        
        build();
        
        cout << mf.maxFlow( N, N + 1 )<< endl;
  
    }
    
}
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() {
    
    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 );
        
    }
    
} 
int main()
{
    Init();
    int t, n, cas = 0;
    scanf("%d", &t);
    while (t--){
        scanf("%d", &n);
        for (int i = 0; i < MAX; ++i) in[i] = -1;
        for (int i = 0; i < n; ++i){
            scanf("%d", &arr[i]);
            in[arr[i]] = i;
        }
        g.Init(n+2);
        int S = n, T = n+1;
        for (int i = 0; i < n; ++i){
            int x = arr[i];
            if (cnt[x]&1) g.Addedge(S, i, 1);
            else g.Addedge(i, T, 1);
//            else{
//                printf("Not Prime %d\n", x);
                for (int j = 0; j < tot && prime[j] <= x; ++j){
                    if (x%prime[j]) continue;
//                    printf("x = %d factor = %d\n", x, prime[j]);
                    int y = x/prime[j];
                    if (in[y] != -1){
                        int v = in[y];
                        if (cnt[x]&1) g.Addedge(i, v, 1);
                        else g.Addedge(v, i, 1);
                    }
                    if (y > prime[tot-1] && cnt[y] == 1){
                        y = prime[j];
                        if (in[y] != -1){
                            int v = in[y];
                            if (cnt[x]&1) g.Addedge(i, v, 1);
                            else g.Addedge(v, i, 1);
                        }
                    }
                }
//            }
//            if (!flag && in[1] != -1) g.Addedge(i, in[1], 1);
        }
        printf("Case %d: %d\n", ++cas, n-g.Maxflow(S, T));
    }
    return 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( "" );

    }

}
bool check( int flow ) {
    for( int i = st; i < ed; ++i )
        mf.buf[i].c = flow;
    return  mf.maxFlow( 0, N + M + 1 ) == N;
} 
Beispiel #16
0
void proc() {
    scanf("%d %d %d", &n, &m, &c);
    for (int i = 0; i < m; ++i) {
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        graph[a].push_back(Edge{ c, b });
        graph[b].push_back(Edge{ c, a });
    }
 
    for (int i = 1; i <= n; ++i) {
        dist[i] = 100000000;
    }
    priority_queue<Edge> pq;
    dist[1] = 0;
    pq.push(Edge{ 0, 1 });
    while (!pq.empty()) {
        Edge cur = pq.top();
        pq.pop();
        if (cur.w > dist[cur.to]) {
            continue;
        }
        for (const auto& edge : graph[cur.to]) {
            if (dist[edge.to] > dist[cur.to] + edge.w) {
                dist[edge.to] = dist[cur.to] + edge.w;
                pq.push(Edge{ dist[edge.to], edge.to });
                via[edge.to].clear();
                via[edge.to].push_back(cur.to);
            }
            else if (dist[edge.to] == dist[cur.to] + edge.w) {
                via[edge.to].push_back(cur.to);
            }
        }
    }
 
    for (int i = 0; i < c; ++i) {
        int v;
        scanf("%d", &v);
        carInit.push_back({ dist[v], v });
    }
    sort(carInit.begin(), carInit.end());
 
    MaxFlow mf;
    mf.Init(n + c + 2);
    for (int k = 1; k <= n; ++k) {
        for (int v : via[k]) {
            mf.AddEdge(k, v, 1);
        }
    }
    int ans = 0;
    for (int i = 0; i < carInit.size(); ) {
        mf.ClearFlow();
        int ss = n + 1 + i;
 
        int j = i;
        while (j < carInit.size() && carInit[i].first == carInit[j].first) {
            int k = j, cnt = 0;
            while (k < carInit.size() && carInit[j].second == carInit[k].second) {
                ++k;
                ++cnt;
            }
            mf.AddEdge(ss, carInit[j].second, cnt);
            j = k;
        }
        ans += mf.Solve(ss, 1);
 
        i = j;
    }
    printf("%d", ans);
}