poj3426 (network flow)

The meaning of problems: there are n machined computer, a p-computer parts, the n output the next machining computer case, 2 * p + 1 data, the first computer is a maximum number of units produced per hour, then the number of p , the computer can be produced from p to p states of parts of parts of the state behind the computer. Asked how many computers the production of up to one hour.

Of course, this state is defined as the part, 0 means no, 1 must, 2 represents optional (it can ignore 2)

 

Ideas:

Of course, all computer parts from 0 began . But you forget that there are two is also possible, as long as the parts other than 1 on the line. (A pit)

Think there are plenty of non-processed whole machine 1, and the whole process to machine 1, then it is a multi-source, multi-network flow Meeting Point, will use a super source, super Meeting Point.

Super source connected to the non-full can from Part 1 starts processing machines, but may be machined into the machine 1 are all connected to super sink ( the first step )

 

Then the next step is to connect the machine to see if the processing of semi-finished products to the next whether machining. As long as each part can not correspond to a 0, a 1, can. ( Second step )

 

The next step is seeking the maximum flow network flow, here dinic algorithm template on the line. ( Third step )

Finally, as long as a flow path and is calculated to output. ( The final step )

Finally, I would complicated, thought to be the output of each augmenting path, finally wa a few rounds. . .

Code:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
struct node{
    int c,f;
}mp[100][100];
int sx,ex,n,m,cnt;
int g[100][100];
int step[100],ans,li,outline[100][5];
//li record number of flow paths, outline the path where the recording 

BOOL BFS () // dinic-- hierarchical network seeking 
{ 
    Memset (STEP, 0 , the sizeof (STEP)); 
    STEP [SX] = . 1 ; 
    Queue < int > Q; 
    Q .push (SX); 
    the while (! q.empty ()) 
    { 
        int P = q.front (); 
        q.pop (); 
        for ( int I = 0 ; I <= m + . 1 ; I ++ ) 
        { 
            IF (! STEP [I] && MP [P] [I] .c- MP [P] [I] .F) 
            {
                STEP [I] = STEP [P] + . 1 ; 
                q.push (I); 
            } 
        } 
    } 
    return STEP [EX] =! 0 ; 
} 

int Dinic ( int POS, int Flow) // determined by all the network hierarchy wide path traffic 
{
     int F = flow;
     IF (POS == EX)
         return flow;
     for ( int I = 0 ; I <= m + . 1 ; I ++ ) 
    { 
        IF (MP [POS] [I] .c-MP [POS ] [I] && STEP .F [POS] + . 1 == STEP [I])
        {
            int a=mp[pos][i].c-mp[pos][i].f;
            int t=dinic(i,min(a,flow));
            mp[pos][i].f+=t;
            mp[i][pos].f-=t;
            flow-=t;
        }
    }
    return f-flow;
}

void solve()//最大流 
{
    while(bfs())
    {
        ans+=dinic(sx,inf);
    }
    return ;
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(mp,0,sizeof(mp));
        sx=0,ex=m+1;//起点sx,终点ex 
        ans=0,li=0;
        for(int i=1;i<=m;i++)
            for(int j=0;j<=2*n;j++)
                scanf("%d",&g[i][j]);
        for(int I = . 1 ; I <= m; I ++) // determines whether the connection super source or super sink 
        {
             int FLAG1 = 0 , FLAG2 = 0 ;
             for ( int J = . 1 ; J <= n-; J ++ ) 
            { 
                IF (G [I] [J] == . 1 ) 
                    FLAG1 = . 1 ;
                 IF (G [I] [J + n-] == 0 ) 
                    FLAG2 = . 1 ; 
            } 
            IF (! FLAG1) // connection super source 
                MP [ sx] [i] .c = g [i] [0 ];
             IF (FLAG2!) // connected sink super 
                MP [I] [EX] .c = G [I] [ 0 ]; 
        } 
        for ( int I = . 1 ; I <= m; I ++) // Analyzing can connected twenty-two plant 
        {
             for ( int J = . 1 ; J <= m; J ++ ) 
            { 
                iF (I == J)
                     Continue ;
                 int In Flag = 0 ;
                 for ( int K = . 1 ); K <= n-; K ++ 
                { 
                    IF (G [I] [K + n-] + G [J] [K] == . 1 ) 
                    { 
                        In Flag = . 1 ;
                         BREAK ; 
                    } 
                } 
                IF (In Flag)
                     Continue ; 
                MP [I] [J] .c = min (G [I] [ 0 ], G [J] [ 0 ]); 
            } 
        } 
        Solve (); // find the maximum flow 
        for ( int I = . 1 ; I <= m; I ++) // determine whether there is road traffic
         {
            for(int j=1;j<=m;j++)
            {
                if(mp[i][j].f>0)
                {
                    li++;
                    outline[li][1]=i;
                    outline[li][2]=j;
                    outline[li][3]=mp[i][j].f;
                }
            }
        }
        cout<<ans<<' '<<li<<endl;
        for(int i=1;i<=li;i++)
        {
            cout<<outline[i][1]<<' '<<outline[i][2]<<' '<<outline[i][3]<<endl;
        }
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/xiongtao/p/11140399.html