vector 的注意事项 (poj2195 小人进房子 最小费用最大流)

下图分别是用50w和20wsize的vector跑出来的结果,vector.clear()耗时太长!尽量开小

附poj2195ac代码

    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <cstring>
    #include <queue>
    #include <string>
    #include <map>
    using namespace std;
    #define Del(a,b) memset(a,b,sizeof(a))
    const int inf = 0x3f3f3f3f;
    const int N=50000;
    struct Point{
        int x, y;
        Point():x(0), y(0){};
        Point(int x0, int y0): x(x0),y(y0){};
    }fih[50000], fim[50000];
    struct Node
    {
        int from,to,cap,flow,cost;
    };
    vector<Node> e;
    vector<int> v[N];
    int vis[N],dis[N];
    int p[N],a[N];  //p保存father,a保存cap
    void Clear(int x)
    {
        for(int i=0;i<=x;i++)
            v[i].clear();
        e.clear();
    }
    void add_Node(int from,int to,int cap,int cost)
    {
        e.push_back((Node){from,to,cap,0,cost});
        e.push_back((Node){to,from,0,0,-cost});
        int len = e.size()-1;
        v[to].push_back(len);
        v[from].push_back(len-1);
    }
    bool BellmanFord(int s,int t,int& flow,int& cost)
    {
        Del(dis,inf);
        Del(vis,0);
        dis[s] = 0;
        vis[s] = 1;
        p[s] = 0;
        a[s] = inf;
        queue<int> q;
        q.push(s);
        while(!q.empty())
        {
            int u = q.front();
            q.pop();
            vis[u] = 0;
            for(int i=0; i<v[u].size(); i++)
            {
                Node& g = e[v[u][i]];
                if(g.cap>g.flow && dis[g.to] > dis[u]+g.cost)
                {
                    dis[g.to] = dis[u] + g.cost;
                    p[g.to] = v[u][i];  //保存前驱
                    a[g.to] = min(a[u],g.cap-g.flow);
                    if(!vis[g.to])
                    {
                        q.push(g.to);
                        vis[g.to]=1;
                    }

                }
            }
        }
        if(dis[t] == inf)
            return false;
        flow += a[t];
        cost += dis[t]*a[t];///可能需要修改,本题下同一条路径上的每个单位流量都要消耗
        int u = t;
        while(u!=s)
        {
            e[p[u]].flow += a[t];
            e[p[u]^1].flow -= a[t];
            u = e[p[u]].from;
        }
        //cout<<"BF Done"<<endl;
        return true;
    }
    int Min_Cost(int s,int t)
    {
        int flow=0,cost = 0;
        while(BellmanFord(s,t,flow,cost));
        //cout<<"MC Done"<<endl;
        return cost;
    }

    int main()
    {
    int n,m,k;  //n 店主 k 物品 M 供应商
    while((~scanf("%d%d",&n,&m)) && (m+n))  //多组测试数据,mnk均=0时输入结束
    {
        int s = 0, totm=0, toth=0;

        for (int i=0; i<n; i++){
            string rubbish;getline(cin, rubbish);
        for (int j=0; j<m; j++)
        {
            char c;
            scanf("%c", &c);
            //cout<<"c="<<c<<endl;
            if (c=='H')
                fih[++toth]=Point(i,j);
            else if(c=='m')
                fim[++totm]=Point(i,j);
        }
        }



        //s=0, 1..totm, totm+1..totm+toth, t=toth+totm+1;
        for (int i=1; i<=totm; i++)
            add_Node(0, i, 1, 0);

        for (int i=1; i<=totm; i++)
        for (int j=1; j<=toth; j++)
            add_Node(i,j+totm,1, abs(fih[i].x-fim[j].x)+abs(fih[i].y-fim[j].y));

        for (int i=totm+1; i<=totm+toth; i++)
            add_Node(i, totm+toth+1, 1, 0);

        cout<<Min_Cost(0, totm+toth+1)<<endl;
        Clear(totm+toth+1);
    }
    return 0;
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值