POJ3469_Dual Core CPU(網路流/最小割=最大流/模版)----Dinic模版2.0

來源:互聯網
上載者:User

標籤:des   style   http   color   os   io   strong   資料   

解題報告

題目傳送門

題意:

雙核CPU,n個模組,每個模組必須運行在某個CPU核心上,每個模組在cpu單核的消耗A和B,M對模組要共用資料,如果在同一個核心上不用消耗,否則需要耗費。安排N個模組,使得總耗費最小

思路:

將兩個cpu核心看成源點和匯點,其他模組分別與源點匯點連線(表示每個模組可以在任意cpu上運行),m對模組分別連雙向邊,要使得模組只能在一個cpu上運行,就是找到一個割,源點和匯點必不聯通,耗費最少就是最小割,最小割最大流原理轉換成求最大流。

這題資料大,沒最佳化TLE了,加了兩最佳化就過了。

更新模版

#include <iostream>#include <cstring>#include <cstdio>#include <queue>#define M 6050000#define N 320000#define inf 0x3f3f3f3fusing namespace std;struct node {    int v,w,next;} edge[M];int head[N],cnt,l[N],n,m,s,t;void add(int u,int v,int w) {    edge[cnt].v=v;    edge[cnt].w=w;    edge[cnt].next=head[u];    head[u]=cnt++;    edge[cnt].v=u;    edge[cnt].w=0;    edge[cnt].next=head[v];    head[v]=cnt++;}void add2(int u,int v,int w) {    edge[cnt].v=v;    edge[cnt].w=w;    edge[cnt].next=head[u];    head[u]=cnt++;    edge[cnt].v=u;    edge[cnt].w=w;    edge[cnt].next=head[v];    head[v]=cnt++;}int bfs() {    memset(l,-1,sizeof(l));    l[s]=0;    int i,u,v;    queue<int >Q;    Q.push(s);    while(!Q.empty()) {        u=Q.front();        Q.pop();        for(i=head[u]; i!=-1; i=edge[i].next) {            v=edge[i].v;            if(l[v]==-1&&edge[i].w) {                l[v]=l[u]+1;                Q.push(v);            }        }    }    return l[t]>0;}int dfs(int u,int f) {    int a,flow=0;    if(u==t)return f;    for(int i=head[u]; i!=-1; i=edge[i].next) {        int v=edge[i].v;        if(l[v]==l[u]+1&&edge[i].w&&(a=dfs(v,min(f,edge[i].w)))) {            edge[i].w-=a;            edge[i^1].w+=a;            flow+=a;//多路增廣            f-=a;            if(!f)break;        }    }    if(!flow)l[u]=-1;//當前弧最佳化    return flow;}int dinic() {    int a,ans=0;    while(bfs())        while(a=dfs(s,inf))            ans+=a;    return ans;}int main() {    int i,j,u,v,w;    while(~scanf("%d%d",&n,&m)) {        memset(head,-1,sizeof(head));        cnt=0;        s=0;        t=n+1;        for(i=1; i<=n; i++) {            scanf("%d%d",&u,&v);            add(s,i,u);            add(i,t,v);        }        for(i=1; i<=m; i++) {            scanf("%d%d%d",&u,&v,&w);            add2(u,v,w);        }        printf("%d\n",dinic());    }}

Dual Core CPU
Time Limit: 15000MS   Memory Limit: 131072K
Total Submissions: 18883   Accepted: 8144
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, Mpairs 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 11 102 1010 32 3 1000

Sample Output

13

Source

POJ Monthly--2007.11.25, Zhou Dong


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.