POJ 1459 Power Network(ISAP 裸最大流)

题目链接:http://poj.org/problem?id=1459

注意输入格式就行,还是ISAP

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
const int N = 210;
const int maxn = 300;
const int maxm = 40000;
#define MIN INT_MIN
#define MAX INT_MAX
#define LL long long
using namespace std;
int max(int a,int b){if(a>b)return a; else return b;}
int min(int a,int b){if(a<b)return a; else return b;}

int n,m;

int head[maxn], sum, bnum;
int dis[maxn];
int num[maxn];
int cur[maxn];
int pre[maxn];
struct node
{
    int v, cap;
    int next;
}edge[maxm];
void add(int u, int v, int cap)
{
    edge[bnum].v=v;
    edge[bnum].cap=cap;
    edge[bnum].next=head[u];
    head[u]=bnum++;

    edge[bnum].v=u;
    edge[bnum].cap=0;
    edge[bnum].next=head[v];
    head[v]=bnum++;
}
void BFS(int source,int sink)
{
    queue<int>q;
    while(q.empty()==false)
        q.pop();
    memset(num,0,sizeof(num));
    memset(dis,-1,sizeof(dis));
    q.push(sink);
    dis[sink]=0;
    num[0]=1;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v = edge[i].v;
            if(dis[v] == -1)
            {
                dis[v] = dis[u] + 1;
                num[dis[v]]++;
                q.push(v);
            }
        }
    }
}
int ISAP(int source,int sink,int n)
{
    memcpy(cur,head,sizeof(cur));

    int flow=0, u = pre[source] = source;
    BFS( source,sink);
    while( dis[source] < n )
    {
        if(u == sink)
        {
            int df = MAX, pos;
            for(int i = source;i != sink;i = edge[cur[i]].v)
            {
                if(df > edge[cur[i]].cap)
                {
                    df = edge[cur[i]].cap;
                    pos = i;
                }
            }
            for(int i = source;i != sink;i = edge[cur[i]].v)
            {
                edge[cur[i]].cap -= df;
                edge[cur[i]^1].cap += df;
            }
            flow += df;
            u = pos;
        }
        int st;
        for(st = cur[u];st != -1;st = edge[st].next)
        {
            if(dis[edge[st].v] + 1 == dis[u] && edge[st].cap)
            {
                break;
            }
        }
        if(st != -1)
        {
            cur[u] = st;
            pre[edge[st].v] = u;
            u = edge[st].v;
        }
        else
        {
            if( (--num[dis[u]])==0 ) break;
            int mind = n;
            for(int id = head[u];id != -1;id = edge[id].next)
            {
                if(mind > dis[edge[id].v] && edge[id].cap != 0)
                {
                    cur[u] = id;
                    mind = dis[edge[id].v];
                }
            }
            dis[u] = mind+1;
            num[dis[u]]++;
            if(u!=source)
            u = pre[u];
        }
    }
    return flow;
}
void initt()
{
       memset(head,-1,sizeof(head));
       bnum=0;
}
int main()
{
    int np,nc,u,v,w;
    while(scanf("%d%d%d%d",&n,&np,&nc,&m) != EOF)
    {
        initt();
        for(int i = 0;i<m;i++)
        {
            scanf(" (%d,%d)%d",&u,&v,&w);
            add(u+1,v+1,w);
        }
        for(int i = 0;i<np;i++)
        {
            scanf(" (%d)%d",&v,&w);

            add(0,v+1,w);
        }
        for(int i = 0;i<nc;i++)
        {
            scanf(" (%d)%d",&u,&w);

            add(u+1,n+1,w);
        }

       int ans = ISAP(0,n+1,n+2);
       cout<<ans<<endl;
    }
    return 0;
}

POJ 1459 Power Network(ISAP 裸最大流),布布扣,bubuko.com

时间: 2024-10-06 16:01:49

POJ 1459 Power Network(ISAP 裸最大流)的相关文章

POJ 1459 Power Network(网络流 最大流 多起点,多汇点)

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 22987   Accepted: 12039 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied

poj 1459 Power Network, 最大流,多源多汇

点击打开链接 多源多汇最大流,虚拟一个源点s'和一个汇点t',原来的源点.汇点向它们连边. #include<cstdiO> #include<cstring> #include<iostream> #include<algorithm> #include<queue> #include<vector> using namespace std; const int maxn = 500 + 5; const int INF = 100

POJ 1459 Power Network 最大流

建模不难,就读入有点麻烦,无脑拍完dinic 1A happy- #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #i

POJ 1459 Power Network 经典网络流构图问题 最大流,EK算法

题目链接:POJ 1459 Power Network Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 23347   Accepted: 12231 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport line

poj 1459 Power Network (dinic)

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 23059   Accepted: 12072 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied

初涉网络流 POJ 1459 Power Network

怒搞一下午网络流,又去我一块心病. 从2F到SAP再到Dinic终于过掉了.可是书上说Dinic的时间复杂度为v*v*e.感觉也应该超时的啊,可是过掉了,好诡异. 后两种算法都是在第一种的基础上进行优化.第一种方法就是不停的寻找增广路,后两种引进了层次网络的概念,第三种又改善了寻找增广路的方法. 现在只能理解到这里了... #include <algorithm> #include <iostream> #include <cstring> #include <c

POJ 1459 Power Network (网络流最大流基础 多源点多汇点 Edmonds_Karp算法)

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 24056   Accepted: 12564 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied

POJ 1459 Power Network(多源点/汇点最大流问题)

题目链接:http://poj.org/problem?id=1459 题目: Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied with an amount s(u) >= 0 of power, may produce an amount 0

POJ 1459 Power Network

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 27831   Accepted: 14469 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied