zoukankan      html  css  js  c++  java
  • Dinic问题

    问题: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: a, b, w. 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

    回答:题目大意是第i个moduel在两个核的运行时间分别为ai、bi,两个modue之间的数据交换要有花费。可以将其建成一个网络流模型,源点和汇点分别是两个核,记为0,n+1,然后建图,求最小割即可。

         #include<cstdio>  
        #include<cstring>  
        #include<iostream>  
        using namespace std;  
          
        const int N=20100;  
        const int M=200200;  
        const int inf=1<<29;  
        struct node  
        {  
         int v,f;  
         int next;  
        }edge[8*M];  
        int head[N],num;  
        int n,m;  
        int s,t,NN;  
          
        void init()  
        {  
         for(int i=0;i<=n+2;i++)  
          head[i]=-1;  
         num=0;  
        }  
          
        void addege(int u,int v,int f)  
        {  
         edge[num].v=v;  
         edge[num].f=f;  
         edge[num].next=head[u];  
         head[u]=num++;  
         edge[num].v=u;  
         edge[num].f=0;  
         edge[num].next=head[v];  
         head[v]=num++;  
        }  
          
        int sap()  
        {  
         int pre[N],cur[N],dis[N],gap[N];  
         int flow=0,aug=inf,u;  
         bool flag;  
         int i;  
         for(i=1;i<=NN;i++)  
         {  
          cur[i]=head[i];  
          gap[i]=dis[i]=0;  
         }  
         gap[s]=NN;  
         u=pre[s]=s;  
         while(dis[s]<NN)  
         {  
          flag=0;  
          for(int &j=cur[u];j!=-1;j=edge[j].next)  
          {  
           int v=edge[j].v;  
           if(edge[j].f>0 && dis[u]==dis[v]+1)  
           {  
            flag=1;  
            if(edge[j].f<aug) aug=edge[j].f;  
            pre[v]=u;  
            u=v;  
            if(u==t)  
            {  
             flow+=aug;  
             while(u!=s)  
             {  
              u=pre[u];  
              edge[cur[u]].f-=aug;  
              edge[cur[u]^1].f+=aug;  
             }  
             aug=inf;  
            }  
            break;  
           }  
          }  
          if(flag) continue;  
          int mindis=NN;  
          for(int j=head[u];j!=-1;j=edge[j].next)  
          {  
           int v=edge[j].v;  
           if(edge[j].f>0 && dis[v]<mindis)  
           {  
            mindis=dis[v];  
            cur[u]=j;  
           }  
          }  
          if((--gap[dis[u]])==0) break;  
          gap[dis[u]=mindis+1]++;  
          u=pre[u];  
         }  
         return flow;  
        }  
          
        int main()  
        {  
         scanf("%d%d",&n,&m);  
         int i,j;  
         int a,b,w;  
         s=0;t=n+1;NN=n+2;  
         init();  
         for(i=1;i<=n;i++)  
         {  
          scanf("%d%d",&a,&b);  
          addege(s,i,a);  
          addege(i,t,b);  
         }  
         for(i=1;i<=m;i++)  
         {  
          scanf("%d%d%d",&a,&b,&w);  
          addege(a,b,w);  
          addege(b,a,w);  
         }  
         printf("%d/n",sap());  
         return 0;  
        } 

  • 相关阅读:
    PHP数据库连接mysql与mysqli的区别与用法
    PHP自定义环境搭建(apache、php)
    2021-11-04 CCPC女生赛 ABCDGIK 题解
    Virtual Judge 20211026 日常训练 ABCDEFG题解
    Spring AOP:@DeclareParents 为对象添加方法
    Spring AOP:@Around 的 JavaConfig 写法
    Spring AOP:@Before、@After 的 JavaConfig 写法
    spring事务传播属性
    缓存与数据库的一致性问题怎么解决
    Java多线程之CyclicBarrier
  • 原文地址:https://www.cnblogs.com/benchao/p/4553664.html
Copyright © 2011-2022 走看看