Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

虚心求教!!!! 为何我的代码TLE!!!!

Posted by niuren at 2009-07-28 17:08:27 on Problem 1459 and last updated at 2009-07-28 17:14:45
#include <iostream>
#include <string>
using namespace std;

#define MAX_N 200+5
#define MAX_VALUE 11000

char temp[MAX_N];
int f[MAX_N][MAX_N];
int c[MAX_N][MAX_N];
int pre[MAX_N];
int Q[MAX_N*MAX_N];

int E_K( int a , int b ){
    int sum = 0;
    while( true ){
           int i , head , tail , min = MAX_VALUE;
           memset( pre , 0 , sizeof(pre) );
           head = tail = 0;
           Q[tail++] = a;
           pre[a] = 1;
           while( head < tail ){
                  i = Q[head++];
                  for( int j = 1 ; j <= b ; j++ ){
                       if( pre[j] == 0 ){
                           int value = c[i][j] - f[i][j];
                           if( value > 0 ){
                                 min = min < value ? min : value;
                                 pre[j] = i;
                                 Q[tail++] = j; 
                           }else if( f[j][i] > 0 ){
                                 min = min < f[j][i] ? min : f[j][i];
                                 pre[j] = -i;
                                 Q[tail++] = j;
                           }
                       }    
                  }    
           }  
           if( pre[b] == 0 ){
               break;    
           }      
           int l , k = b;
           while( k != a ){
                  if( pre[k] > 0 ){
		      l = pre[k];
                      f[l][k] += min;    
                  }else{
		      l = abs( pre[k] );
                      f[k][l] -= min;      
                  }  
		  k = l;
           }
           sum += min;
    }    
    return sum;
}
int main()
{   
    freopen( "in.txt" , "r" , stdin );
    int n , np , nc , m , a , b , value , sum;  
    while( scanf("%d%d%d%d",&n,&np,&nc,&m) != EOF ){
	   int h = n;
           memset( c , 0 , sizeof(c) );
           memset( f , 0 , sizeof(f) );
           while( m-- ){
                  scanf("%s",temp);
		  sscanf(temp,"(%d,%d)%d",&a,&b,&value);
                  c[a+2][b+2] = value;
           } 
           while( np-- ){
                  scanf("%s",temp);
	          sscanf(temp,"(%d)%d",&a,&value);
                  c[1][a+2] = value;
           }
           while( nc-- ){
                  scanf("%s",temp);
		  sscanf(temp,"(%d)%d",&a,&value);
                  c[a+2][h+2] = value;
           } 
           sum = E_K( 1  , h+2 );
           printf("%d\n",sum);       
    }
    return 0;      
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator