hdu 3549 Flow Problem

In this problem ,I get RE so many times,In end ,I know that I write edge[MAXN],It should be write as edge[MAXE].This problem has some problem I don't know.It's so freak.

The portal:http://acm.hdu.edu.cn/showproblem.php?pid=3549

/*************************************************************************
	> File Name: 3549.cpp
	> Author: nmfloat
	> Mail: nmfloat@foxmail.com
	> Created Time: 2015年05月20日 星期三 13时05分01秒
 ************************************************************************/

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <memory.h>

#define MAXN 10005
#define MAXE 200000
#define INF 0x3f3f3f3f

using namespace std;

int tmp,src,des,cnt;
int n,m;

struct Edge{
    int from,to;
    int next,cap;
}edge[MAXE];

int head[MAXN];
int gap[MAXN],dep[MAXN],cur[MAXN],stack[MAXN],top;

int isap(){
	int cur_flow,max_flow,u,insert,i;
	memset(dep,0,sizeof(dep));
	memset(gap,0,sizeof(gap));
	memset(cur,-1,sizeof(cur));
	memset(stack,0,sizeof(stack));
	max_flow = 0;
	u = src;
	top = 0;
	while(dep[src] < n){
		if(u == des){
			cur_flow = INF;
			for(i = 0;i < top;i++){
				if(cur_flow > edge[stack[i]].cap){
					insert = i;
					cur_flow = edge[stack[i]].cap;
				}
			}
			for(i = 0;i < top;i++){
				edge[stack[i]].cap -= cur_flow;
				edge[stack[i]^1].cap += cur_flow;
			}
			max_flow += cur_flow;
			u = edge[stack[top = insert]].from;
		}
		for(i = cur[u];i != -1;i=edge[i].next){
			if((edge[i].cap > 0)&&(dep[u] == dep[edge[i].to]+1))
				break;
		}
		if(i != -1){
			stack[top++] = i;
			u = edge[i].to;
		}
		else{
			if(0 == --gap[dep[u]])break;
			int minn = n;
			for(i = head[u];i!=-1;i=edge[i].next){
				if(edge[i].cap > 0)
					minn = (minn > dep[edge[i].to]) ? (cur[u] = i,dep[edge[i].to]) : minn;
			}
			++gap[dep[u] = minn + 1];
			if(u != src) u = edge[stack[--top]].from;
		}
	}
	return max_flow;
}

void addedge(int u,int v,int f){
    edge[cnt].next = head[u];
    edge[cnt].from = u;
    edge[cnt].to = v;
    edge[cnt].cap = f;
    head[u] = cnt ++;
    edge[cnt].next = head[v];
    edge[cnt].from = v;
    edge[cnt].to = u;
    edge[cnt].cap = 0;
    head[v] = cnt ++;
}

void Init(){
    cnt = 0;
    src = 0;
    des = n - 1;
    memset(head,-1,sizeof(head));
}

void Input(){
    int T,t=1,ans;
    scanf("%d",&T);
    while(T--){
        scanf("%d %d",&n,&m);
        Init();
        int tempa,tempb,tempf;
        for(int i=0;i<m;i++){
            scanf("%d %d %d",&tempa,&tempb,&tempf);
            -- tempa; -- tempb;
            addedge(tempa,tempb,tempf);
        }
        ans = isap();
        printf("Case %d: %d\n",t++,ans);
    }
}

int main(void){
    freopen("3549.in","r",stdin);
    Input();
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值