1 条题解

  • 0
    @ 2024-12-24 9:54:31

    C++ :

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define min(a,b) a<b?a:b
    #define MAXDATA 9999999
    
    typedef struct nd NODE ;
    
    typedef struct edge
    {
    	NODE *begin ;
    	NODE *end ;
    	long long f ;
    	long long c ;
    	struct edge *bnext ;
    	struct edge *enext ;
    } EDGE ;
    
    struct nd
    {
    	int data ;
    	long long e ;
    	int h ;
    	EDGE *in ;
    	EDGE *out ;
    	struct nd *next ;
    } ;
    
    NODE *head ;
    int n, m ;
    
    void ini ()
    {
    	EDGE *ep = head->out ;
    	
    	while (ep)
    	{
    		ep->f = ep->c ;
    		head->e -= ep->f ;
    		ep->end->e += ep->f ;
    		
    		ep = ep->bnext ;
    	}
    }
    
    void relabel (NODE *np)
    {
    	int minh = MAXDATA ;
    	int isin ;
    	EDGE *ep ;
    	
    	if (np->out != NULL)
    	{
    		ep = np->out ;
    		isin = 0 ;
    	}
    	else
    	{
    		ep = np->in ;
    		isin = 1 ;
    	}
    	
    	while (ep != NULL)
    	{
    		if (!isin)
    		{
    			if (ep->f < ep->c && ep->end->h < minh)
    				minh = ep->end->h ;
    			
    			if (ep->bnext != NULL)
    				ep = ep->bnext ;
    			else
    			{
    				ep = np->in ;
    				isin = 1 ;
    			}
    		}
    		else
    		{
    			if (ep->f > 0 && ep->begin->h < minh)
    				minh = ep->begin->h ;
    			
    			ep = ep->enext ;
    		}
    	}
    	
    	np->h = minh + 1 ;
    }
    
    void push (NODE *np1, NODE *np2, EDGE *ep)
    {
    	int changef ;
    	
    	if (ep->begin == np1 && ep->end == np2)
    	{
    		changef = min (ep->c - ep->f, np1->e) ;
    		ep->f += changef ;
    	}
    	else
    	{
    		changef = min (ep->f, np1->e) ;
    		ep->f -= changef ;
    	}
    	
    	np1->e -= changef ;
    	np2->e += changef ;
    }
    
    void disch (NODE *np)
    {
    	int isin ;
    	EDGE *ep ;
    	if (np->out != NULL)
    	{
    		ep = np->out ;
    		isin = 0 ;
    	}
    	else
    	{
    		ep = np->in ;
    		isin = 1 ;
    	}
    	
    	while (np->e > 0)
    	{
    		if (ep == NULL)
    		{
    			relabel (np) ;
    			
    			if (np->out != NULL)
    			{
    				ep = np->out ;
    				isin = 0 ;
    			}
    			else
    			{
    				ep = np->in ;
    				isin = 1 ;
    			}
    		}
    		else if (((ep->f < ep->c && !isin) && (np->h == ep->end->h + 1)) || ((ep->f > 0 && isin) && (np->h == ep->begin->h + 1)))
    		{
    			if (!isin)
    				push (np, ep->end, ep) ;
    			else
    				push (np, ep->begin, ep) ;
    		}
    		else
    		{
    			if (!isin)
    			{
    				if (ep->bnext != NULL)
    					ep = ep->bnext ;
    				else
    				{
    					ep = np->in ;
    					isin = 1 ;
    				}
    			}
    			else
    				ep = ep->enext ;
    		}
    	}
    }
    
    void movend (NODE *np, NODE *up)
    {
    	if (up == NULL)
    		return ;
    	
    	up->next = np->next ;
    	np->next = head->next ;
    	head->next = np ;
    }
    
    void getmaxf ()
    {
    	int i, oldh ;
    	NODE *up = NULL ;
    	head->h = n ;
    	NODE *np = head->next ;
    	ini () ;
    	
    	while (np->data != n)
    	{
    		oldh = np->h ;
    		disch (np) ;
    		
    		if (np->h > oldh)
    		{
    			movend (np, up) ;
    			up = NULL ;
    		}
    			
    		if (up == NULL)
    			up = head->next ;
    		else
    			up = up->next ;
    		np = np->next ;
    	}
    }
    
    NODE *getnode (int k)
    {
    	NODE *np = head ;
    	
    	while (np)
    	{
    		if (np->data == k)
    			return np ;
    		np = np->next ;
    	}
    	
    	return NULL ;
    }
    
    int main (void)
    {
    	int i, a, b ;
    	EDGE *ep ;	
    	NODE *np ;
    
    	while (scanf ("%d%d", &m, &n) != EOF)
    	{
    		head = NULL ;
    		
    		for (i=n; i>0; i--)
    		{
    			np = (NODE*) malloc (sizeof(NODE)) ;
    			np->data = i ;
    			np->next = head ;
    			np->e = 0 ;
    			np->h = 0 ;
    			np->in = NULL ;
    			np->out = NULL ;
    			
    			head = np ;
    		}
    		
    		for (i=0; i<m; i++)
    		{
    			ep = (EDGE*) malloc (sizeof(EDGE)) ;
    			ep->f = 0 ;
    			
    			scanf ("%d%d%lld", &a, &b, &ep->c) ;
    			ep->begin = getnode (a) ;
    			ep->end = getnode (b) ;
    			ep->bnext = ep->begin->out ;
    			ep->enext = ep->end->in ;
    			ep->begin->out = ep ;
    			ep->end->in = ep ;
    		}
    		
    		getmaxf () ;
    		np = getnode (n) ;
    		
    		printf ("%lld\n", np->e) ;
    	}
    	return 0 ;
    }
    
    • 1

    信息

    ID
    1187
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    (无)
    递交数
    0
    已通过
    0
    上传者