poj 3469 網絡流最小割

http://poj.org/problem?id=3469

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

11607511 lanjiangzhou 3469 Accepted 20220K 3500MS C++ 3692B 2013-05-17 20:24:25
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int INF = 100000000;
const int nmax = 20000+100;
const int mmax = 1000000;

struct EDGE{
    int u,v,cap,flow;
    int next;
    EDGE(int a=0,int b=0,int c=0,int d=0):
        u(a),v(b),cap(c),flow(d){}
};

struct EDGEList{
    int start[nmax];
    int last[nmax];
    int t;
    EDGE arc[mmax];
    void clear(){//清除操作
        t=0;
        for(int i=0;i<nmax;i++){
            last[i]=-1;
        }
    }
    void Push_back(EDGE edge){//追加操作
        edge.next=-1; arc[t]=edge;
        if(last[edge.u]!=-1) arc[last[edge.u]].next=t;
        else start[edge.u]=t;
        last[edge.u]=t;  t++;
    }
    void add_arc(EDGE edge){//創建雙向弧
        Push_back(edge);
        Push_back(EDGE(edge.v,edge.u,edge.cap));
    }
}net;

int q[2][nmax];//數組模擬滾動數組
int q1[2],q2[2],qnow;//模擬隊列

void push_queue(int a){//入隊列
    q[qnow][q2[qnow]++]=a;
}

int pop_queue(){//出隊列
    return q[qnow^1][q1[qnow^1]++];
}

void switch_queue(){//滾動數組
    qnow^=1;
    q1[qnow]=0;
    q2[qnow]=0;
}

bool empty_queue(){//判斷隊列是否爲空
    return q1[qnow^1]>=q2[qnow^1];
}

int size_queue(){//隊列大小
    return q2[qnow^1]-q1[qnow^1];
}

int n,m;
int dis[nmax];//層次網絡(距離標號)
int path[nmax],deep;//路徑
int cur[nmax];

bool Bfs(){//Bfs構建層次網絡
    int l,u,v;
    for(int i=0;i<nmax;i++){
        dis[i]=-1; //初始化
    }
    dis[0]=0; qnow=0;
    switch_queue();
    push_queue(0);
    switch_queue();
    while(!empty_queue()){
        l=size_queue();
        while(l--){
            u=pop_queue();//取出隊列結點
            for(int i=net.start[u];i!=-1;i=net.arc[i].next){
                v=net.arc[i].v;
                if(dis[v]==-1&&net.arc[i].cap>net.arc[i].flow){
                    push_queue(v);
                    dis[v]=dis[u]+1;
                    if(v==n)  return true;//匯點在層次網絡中
                }
            }
        }
        switch_queue();//滾動隊列
    }
    return false;
}

int Dinic(){//Dinic求最大流
    int u,neck,pos,res,i;
    int maxflow=0;
    while(Bfs()){
        memcpy(cur,net.start,sizeof(cur));
        deep=0;
        u=0;
        while(1){//最短路徑增廣
            if(u==n){
                neck=INF;
                for( i=0;i<deep;i++){
                    res=net.arc[path[i]].cap-net.arc[path[i]].flow;
                    if(res<neck){
                        neck=res;
                        pos=i;
                    }
                }
                maxflow+=neck;
                for( i=0;i<deep;i++){
                    net.arc[path[i]].flow+=neck;
                    net.arc[path[i]^1].flow-=neck;
                }
                deep=pos;
                u=net.arc[path[deep]].u;
            }
            //在層次網絡中進行增廣
            for(i=cur[u];i!=-1;i=net.arc[i].next){
                if(net.arc[i].cap>net.arc[i].flow&&dis[u]+1==dis[net.arc[i].v])
                break;
            }
            cur[u]=i;
            if(i!=-1){
                path[deep++]=i;
                u=net.arc[i].v;
            }
            else {
                if(deep==0) break;
                dis[u]=-1;
                u=net.arc[path[--deep]].u;
            }
        }
    }
    return maxflow;//返回最大流
}

int main()
{
    int a,b,w;
    scanf("%d%d",&n,&m);
    net.clear();//初始化
    for(int i=1;i<=n;i++){
        scanf("%d%d",&a,&b);
        net.add_arc(EDGE(0,i,a));
        net.add_arc(EDGE(i,n+1,b));
    }
    for(int i=0;i<m;i++){
        scanf("%d%d%d",&a,&b,&w);
        net.add_arc(EDGE(a,b,w));
    }
    n++;
    printf("%d\n",Dinic());
    return 0;
}
View Code

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章