HDU 4280 Island Transport 网络流裸题

非递归版好像被卡掉了,其他2个板子都能过。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
#define ll int
const int MAXN = 100100;//点数的最大值
const int MAXM = 400010;//边数的最大值
const int INF = 0x3f3f3f3f;
struct Edge
{
    int to,next,cap,flow;
}edge[MAXM];//注意是MAXM
int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],cur[MAXN];
void add(int u,int v,int w,int rw = 0)
{
    edge[tol].to = v; edge[tol].cap = w; edge[tol].flow = 0;
    edge[tol].next = head[u]; head[u] = tol++;
    edge[tol].to = u; edge[tol].cap = rw; edge[tol].flow = 0;
    edge[tol].next = head[v]; head[v] = tol++;
}
int Q[MAXN];
void BFS(int start,int end)
{
    memset(dep,-1,sizeof(dep));
    memset(gap,0,sizeof(gap));
    gap[0] = 1;
    int front = 0, rear = 0;
    dep[end] = 0;
    Q[rear++] = end;
    while(front != rear)
    {
        int u = Q[front++];
        for(int i = head[u]; i != -1; i = edge[i].next)
        {
            int v = edge[i].to;
            if(dep[v] != -1)continue;
            Q[rear++] = v;
            dep[v] = dep[u] + 1;
            gap[dep[v]]++;
        }
    }
}
int S[MAXN];
int sap(int start,int end,int N)
{
    BFS(start,end);
    memcpy(cur,head,sizeof(head));
    int top = 0;
    int u = start;
    int ans = 0;
    while(dep[start] < N)
    {
        if(u == end)
        {
            int Min = INF;
            int inser;
            for(int i = 0;i < top;i++)
                if(Min > edge[S[i]].cap - edge[S[i]].flow)
                {
                    Min = edge[S[i]].cap - edge[S[i]].flow;
                    inser = i;
                }
            for(int i = 0;i < top;i++)
            {
                edge[S[i]].flow += Min;
                edge[S[i]^1].flow -= Min;
            }
            ans += Min;
            top = inser;
            u = edge[S[top]^1].to;
            continue;
        }
        bool flag = false;
        int v;
        for(int i = cur[u]; i != -1; i = edge[i].next)
        {
            v = edge[i].to;
            if(edge[i].cap - edge[i].flow && dep[v]+1 == dep[u])
            {
                flag = true;
                cur[u] = i;
                break;
            }
        }
        if(flag)
        {
            S[top++] = cur[u];
            u = v;
            continue;
        }
        int Min = N;
        for(int i = head[u]; i != -1; i = edge[i].next)
            if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min)
            {
                Min = dep[edge[i].to];
                cur[u] = i;
            }
        gap[dep[u]]--;
        if(!gap[dep[u]])return ans;
        dep[u] = Min + 1;
        gap[dep[u]]++;
        if(u != start)u = edge[S[--top]^1].to;
    }
    return ans;
}
void init(){ tol = 0; memset(head,-1,sizeof(head)); }

int n, m;
struct point{
	int x,y;
	int num;
	bool operator<(const point&a)const{
		return a.x>x;
	}
}p[MAXN];
int main()
{
	int i, T, j, u, v, cap;scanf("%d",&T);
	while(T--){
		init();
		scanf("%d %d",&n,&m);
		for(i = 1; i <= n; i++)scanf("%d %d",&p[i].x,&p[i].y), p[i].num = i;
		while(m--){
			scanf("%d %d %d",&u,&v,&cap);
			add(u,v,cap);
			add(v,u,cap);
		}
		sort(p+1,p+1+n);
		printf("%d\n",sap(p[1].num, p[n].num, n));
	}
	return 0;
}

HDU 4280 Island Transport 网络流裸题,布布扣,bubuko.com

时间: 2024-10-04 07:31:45

HDU 4280 Island Transport 网络流裸题的相关文章

HDU 4280 Island Transport(网络流模板)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4280 Problem Description In the vast waters far far away, there are many islands. People are living on the islands, and all the transport among the islands relies

HDU 4280 Island Transport(网络流,SAP)

解题思路: 建模很简单,不过以前一直用dinic,而这个题目数据偏大,用dinic超时了,据说没有可以卡住SAP的网络流,于是搞到了一套SAP的模版,过了,保存一下模版. #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <cstdlib> #include <vector> #include <queue>

HDU 4280 Island Transport(无向图最大流)

HDU 4280:http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意: 比较裸的最大流题目,就是这是个无向图,并且比较卡时间. 思路: 是这样的,由于是无向图,所以addedge 的反边容量直接设为原始流量.然后还可以优化搜索的方向,bfs可以从t到s跑,dfs可以从s到t跑,这样快. //#pragma GCC optimize(3) //#pragma comment(linker, "/STACK:102400000,102400000&qu

Hdu 4280 Island Transport(最大流)

Island Transport Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 6217    Accepted Submission(s): 1965 Problem Description In the vast waters far far away, there are many islands. People are l

HDU - 4280 Island Transport(ISAP)

题目大意:要求你将游客从最东边送到最西边,反正边是双向的,没有差别了 解题思路:裸流 ISAP 8080MS #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <queue> using namespace std; #define N 100010 #define INF 0x3f3f3f3f struct Edge { in

hdu - 1269 迷宫城堡 (强连通裸题)

http://acm.hdu.edu.cn/showproblem.php?pid=1269 判断一个图是不是强连通,缩点之后判断顶点数是不是为1即可. 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <vector> 5 #include <cstring> 6 #include <algorithm> 7 #include <st

hdu 1800 字符串哈希 裸题

题意:意思是有若干个飞行员,需要在扫帚上练习飞行,每个飞行员具有不同的等级,且等级高的飞行员可以当等级低的飞行员的老师,且每个飞行员至多有且只有一个老师和学生.具有老师和学生关系的飞行员可以在同一把扫帚上练习,并且这个性质具有传递性.即比如有A,B,C,D,E五个飞行员,且等级是A>B>C>D>E,那么可以使A当B的老师,B当C的老师,E当D的老师,那么A,B,C可以在同一扫帚上练习,D,E在同一把扫帚上练习,这样需要2把扫帚,而如果是A当B的老师,B当C的老师,C当D的老师,D当

HDU 3376 &amp;&amp; 2686 方格取数 最大和 费用流裸题

题意: 1.一个人从[1,1] ->[n,n] ->[1,1] 2.只能走最短路 3.走过的点不能再走 问最大和. 对每个点拆点限流为1即可满足3. 费用流流量为2满足1 最大费用流,先给图取负,结果再取负,满足2 #include <stdio.h> #include <string.h> #include <iostream> #include <math.h> #include <queue> #include <set&

HDU 4893 线段树裸题

Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2512    Accepted Submission(s): 751 Problem Description Recently, Doge got a funny birthday present from his new friend, Pro