zoukankan      html  css  js  c++  java
  • POJ 3469 Dual Core CPU (Dinic)

    Dual Core CPU
    Time Limit: 15000MS   Memory Limit: 131072K
    Total Submissions: 15782   Accepted: 6813
    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

     
     
    题意:现在有n个任务,两个机器A和B,每个任务要么在A上完成,要么在B上完成,而且知道每个任务在A和B机器上完成所需要的费用。然后再给m行,每行 a,b,w三个数字。表示如果a任务和b任务不在同一个机器上工作的话,需要额外花费w。现在要求出完成所有任务最小的花费是多少。
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    const int INF=0x3f3f3f3f;
    const int VM=20010;
    const int EM=500000;
    
    struct Edge{
        int u,v;
        int cap,nxt;
    }edge[EM];
    
    int head[VM],dep[VM],cnt;
    int src,des;
    
    void addedge(int cu,int cv,int cw,int rw){   //无向边的逆边 cap = cw,有向边的cap = 0;
        edge[cnt].u=cu;
        edge[cnt].v=cv;
        edge[cnt].cap=cw;
        edge[cnt].nxt=head[cu];
        head[cu]=cnt++;
        edge[cnt].u=cv;
        edge[cnt].v=cu;
        edge[cnt].cap=rw;
        edge[cnt].nxt=head[cv];
        head[cv]=cnt++;
    }
    
    int BFS(){
        int q[VM],front=0,rear=0;
        memset(dep,-1,sizeof(dep));
        q[rear++]=src;
        dep[src]=0;
        while(front!=rear){
            int u=q[front++];
            front=front%VM;
            for(int i=head[u];i!=-1;i=edge[i].nxt){
                int v=edge[i].v;
                if(edge[i].cap>0 && dep[v]==-1){
                    dep[v]=dep[u]+1;
                    q[rear++]=v;
                    rear=rear%VM;
                    if(v==des)
                        return 1;
                }
            }
        }
        return 0;
    }
    
    int Dinic(){
        int i,top,res=0;
        int stack[VM],cur[VM];
        while(BFS()){
            memcpy(cur,head,sizeof(head));
            int u=src;
            top=0;
            while(1){
                if(u==des){
                    int min=INF,loc;
                    for(i=0;i<top;i++)
                        if(min>edge[stack[i]].cap){
                            min=edge[stack[i]].cap;
                            loc=i;
                        }
                    for(i=0;i<top;i++){
                        edge[stack[i]].cap-=min;
                        edge[stack[i]^1].cap+=min;
                    }
                    res+=min;
                    top=loc;
                    u=edge[stack[top]].u;
                }
                for(i=cur[u];i!=-1;cur[u]=i=edge[i].nxt)
                    if(edge[i].cap!=0 && dep[u]+1==dep[edge[i].v])
                        break;
                if(cur[u]!=-1){
                    stack[top++]=cur[u];
                    u=edge[cur[u]].v;
                }else{
                    if(top==0)
                        break;
                    dep[u]=-1;
                    u=edge[stack[--top]].u;
                }
            }
        }
        return res;
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int n,m;
        while(~scanf("%d%d",&n,&m)){
            memset(head,-1,sizeof(head));
            src=0;
            des=n+1;
            int a,b;
            for(int i=1;i<=n;i++){
                scanf("%d%d",&a,&b);
                addedge(src,i,a,0);
                addedge(i,des,b,0);
            }
            int u,v,w;
            while(m--){
                scanf("%d%d%d",&u,&v,&w);
                addedge(u,v,w,w);
            }
            printf("%d\n",Dinic());
        }
        return 0;
    }
  • 相关阅读:
    编译环境及编译器介绍
    linux下同步window的firefox
    DPDK pdump抓包说明
    linux TCP协议(1)---连接管理与状态机
    Linux用户态数据发送和接收
    DPDK之内存管理
    linux socket系统调用层
    linux网络栈结构
    DPDK mbuf何时释放回内存池?
    虚拟设备之linux网桥
  • 原文地址:https://www.cnblogs.com/jackge/p/3058557.html
Copyright © 2011-2022 走看看