首页 技术 正文
技术 2022年11月20日
0 收藏 948 点赞 3,523 浏览 2887 个字

问题: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;  
    }

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,967
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,487
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,332
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,115
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,748
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,783