• POJ 3469.Dual Core CPU 最大流dinic算法模板


    Time Limit: 15000MS   Memory Limit: 131072K
    Total Submissions: 24830   Accepted: 10756
    Case Time Limit: 5000MS

    Description

    As more and more computers are equipped with dual core CPU, SetagLilb, the Chief Technology Officer of TinySoft Corporation, decided to update their famous product - SWODNIW.

    The routine consists of N modules, and each of them should run in a certain core. The costs for all the routines to execute on two cores has been estimated. Let's define them as Ai and Bi. Meanwhile, M pairs of modules need to do some data-exchange. If they are running on the same core, then the cost of this action can be ignored. Otherwise, some extra cost are needed. You should arrange wisely to minimize the total cost.

    Input

    There are two integers in the first line of input data, N and M (1 ≤ N ≤ 20000, 1 ≤ M ≤ 200000) .
    The next N lines, each contains two integer, Ai and Bi.
    In the following M lines, each contains three integers: abw. The meaning is that if module a and module b don't execute on the same core, you should pay extra w dollars for the data-exchange between them.

    Output

    Output only one integer, the minimum total cost.

    Sample Input

    3 1
    1 10
    2 10
    10 3
    2 3 1000
    

    Sample Output

    13

    Source

     
    题意:每个问题在a,b处理器上面处理有不同的代价,有一些问题在不同的处理器上面处理要增加额外的代价,求处理完所有问题需要的最小代价。
    思路:将问题分别于a,b相连,边权为代价。将它们分成两个集合,与a在一个集合的表示在a上面处理,与b在一个集合的表示在b上面处理,如果要代价最小的话,那就是最小割。但是有一些物品在不同的处理器上面处理需要额外的代价,,那就在它们之间建立边权,这样的话将它们分开就会增加代价。最小割问题即求最大流。这题的边权值较大,注意如果用一般增广路算法(即在残留网络中,每次任意寻找一条增广路)的话时间复杂度为O(FVE),与最大流有关,最短增广路算法(在层次网络中,用dfs实现多次增广,即dinic算法)的时间复杂度为O(VVE)。
    代码:
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    #include<set>
    #include<map>
    #include<queue>
    #include<stack>
    #include<vector>
    using namespace std;
    #define PI acos(-1.0)
    typedef long long ll;
    typedef pair<int,int> P;
    const int  maxn=1e5+100,maxm=1e5+100,inf=0x3f3f3f3f,mod=1e9+7;
    const ll INF=1e18+7;
    priority_queue<P,vector<P>,greater<P> >que;
    struct edge
    {
        int from,to;
        int cap;
        int rev;///方向边的编号
    };
    vector<edge>G[maxn];///邻接表存图
    int iter[maxn];///当前弧,在其之前的边已经没有了作用
    int level[maxn];///层次
    void addedge(int u,int v,int c)
    {
        edge e;
        e.from=u,e.to=v,e.cap=c,e.rev=G[v].size();
        G[u].push_back(e);
        e.from=v,e.to=u,e.cap=0,e.rev=G[u].size()-1;
        G[v].push_back(e);
    }
    int bfs(int s,int t)
    {
        memset(level,-1,sizeof(level));
        queue<int>q;
        level[s]=0;
        q.push(s);
        while(!q.empty())
        {
            int u=q.front();
            q.pop();
            for(int i=0; i<G[u].size(); i++)
            {
                edge e=G[u][i];
                if(e.cap>0&&level[e.to]<0)
                {
                    level[e.to]=level[u]+1;
                    q.push(e.to);
                }
            }
        }
        if(level[t]<0) return 0;
        else return 1;
    }
    int dfs(int u,int t,int f)
    {
        if(u==t||f==0) return f;
        int flow=0;
        for(int &i=iter[u]; i<G[u].size(); i++)
        {
            edge e=G[u][i];
            if(e.cap>0&&level[u]+1==level[e.to])
            {
                int d=dfs(e.to,t,min(f,e.cap));
                if(d>0)
                {
                    G[u][i].cap-=d;
                    G[e.to][e.rev].cap+=d;
                    flow+=d;
                    f-=d;
                    if(f==0) break;
                }
            }
        }
        return flow;
    }
    int max_flow(int s,int t)
    {
        int flow=0;
        while(bfs(s,t))
        {
            memset(iter,0,sizeof(iter));
            flow+=dfs(s,t,inf);
        }
        return flow;
    }
    int main()
    {
        int n,m;
        scanf("%d%d",&n,&m);
        int s=0,t=n+1;
        for(int i=1; i<=n; i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            addedge(s,i,a);
            addedge(i,t,b);
        }
        for(int i=1; i<=m; i++)
        {
            int a,b,w;
            scanf("%d%d%d",&a,&b,&w);
            addedge(a,b,w);
            addedge(b,a,w);
        }
        cout<<max_flow(s,t)<<endl;
        return 0;
    }
    最大流模板
  • 相关阅读:
    CodeFirst进行数据迁移之添加字段
    .NET程序优化
    DataRead 和DataSet区别
    WCF、WebAPI、WCFREST、WebService之间的区别
    centos6.7下安装配置vnc
    Centos 6.5 优化 一些基础优化和安全设置
    Elasticsearch 检索
    ElasticSearch 5.0.1 java API操作
    Elasticsearch5.0.1 + Kibana5.0.1 + IK 5.0.1安装记录
    Spring的注解@Qualifier小结
  • 原文地址:https://www.cnblogs.com/GeekZRF/p/7256873.html
Copyright © 2020-2023  润新知