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 yujingping at 2014-12-07 09:36:36 on Problem 3469
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<math.h>
#define INF 214748364
using namespace std;

inline bool minimize(int &p, int q){if(p<=q)return 0; p =q;return 1;}
const int MAXN = 20009 , MAXE = 4000001 ;
int n,m;
struct Arclist{
	struct Edge{int v,cap,next;}E[MAXN+MAXE<<1];
	int head[MAXN],cur;
	void init(){fill_n(head,MAXN,-1);cur=0;}
	void add(int u,int v,int cap=1){
	    E[cur].v=v;E[cur].cap=cap;
	    E[cur].next=head[u];head[u]=cur++;
	    E[cur].v=u;E[cur].cap=0;
	    E[cur].next=head[v];head[v]=cur++;
	}
    int SAP(int S,int T,int N){
        int maxflow=0,pre[MAXN],dis[MAXN]={},gap[MAXN]={};
        int cur[MAXN];copy(head,head+N,cur);
        gap[0]=N+1;++gap[dis[S]=1];
        for(int u=pre[S]=S;dis[S]<=N;++gap[++dis[u]],u=pre[u]){
            for(bool flag=true;flag;){flag=false;
                for(int&p=cur[u];~p;p=E[p].next){
                    if(!E[p].cap||dis[u]!=dis[E[p].v]+1)continue;
                    flag=true;pre[E[p].v]=u;u=E[p].v;
                    if(u==T){
                        int aug=INF;
                        for(int i=S;i!=T;i=E[cur[i]].v)
                            if(minimize(aug,E[cur[i]].cap))u=i;
                        for(int i=S;i!=T;i=E[cur[i]].v){
                            E[cur[i]].cap-=aug;
                            E[cur[i]^1].cap+=aug;
                        }
                        maxflow+=aug;
                    }
                    break;
                }
            }
            if(--gap[dis[u]]==0)break;
            dis[u]=N;
            for(int p=head[u];~p;p=E[p].next)
                if(E[p].cap&&minimize(dis[u],dis[E[p].v]))cur[u]=p;
        }
        return maxflow;
    }
}G;


int main()
{
	cin>>n>>m;
	int t1,t2,t3;
	G.init();
	for(int i = 1;i<=n;i++)
	{
		scanf("%d%d",&t1,&t2);
		G.add(1,i+1,t2);
		G.add(i+1,n+2,t1);
	}
	for(int i = 1;i<=m;i++)
	{
		scanf("%d%d%d",&t1,&t2,&t3);
		G.add(t1+1,t2+1,t3);
		G.add(t2+1,t1+1,t3);
	}
	cout<<G.SAP(1,n+2,n+2)<<endl;
}

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