Dual Core CPU POJ - 3469

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

题解:用最小的费用将对象划分成两个集合的问题,常常可以转化成最小割后顺利解决。------摘自《挑战程序设计竞赛》
 1 #include<queue>
 2 #include<vector>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<iostream>
 6 #include<algorithm>
 7 using namespace std;
 8 
 9 const int INF=1e8;
10 const int maxe=1e6;
11 const int maxn=30000;
12 
13 struct edge{
14     int to,w;
15     int nxt,rev;
16 }e[maxe];
17 
18 int n,m,tot;
19 int head[maxn],d[maxn],iter[maxn];
20 
21 void Init(){
22     tot=0;
23     memset(head,-1,sizeof(head));
24 }
25 
26 void addedge(int u,int v,int w){
27     e[tot].to=v,e[tot].w=w,e[tot].rev=tot+1,e[tot].nxt=head[u],head[u]=tot++;
28     e[tot].to=u,e[tot].w=0,e[tot].rev=tot-1,e[tot].nxt=head[v],head[v]=tot++;
29 }
30 
31 void Read(){
32     int s=0,t=n+1;
33     for(int i=1;i<=n;i++){
34         int u,v;
35         scanf("%d%d",&u,&v);
36         addedge(s,i,u);
37         addedge(i,t,v);
38     }
39     for(int i=1;i<=m;i++){
40         int u,v,c;
41         scanf("%d%d%d",&u,&v,&c);
42         addedge(u,v,c);
43         addedge(v,u,c);
44     }
45 }
46 
47 void BFS(int S){
48     memset(d,-1,sizeof(d));
49     d[S]=0;
50     queue<int> q;
51     q.push(S);
52     while(!q.empty()){
53         int v=q.front();q.pop();
54         for(int i=head[v];i!=-1;i=e[i].nxt){
55             if(e[i].w>0&&d[e[i].to]<0){
56                 d[e[i].to]=d[v]+1;
57                 q.push(e[i].to);
58             }
59         } 
60     }
61 }
62 
63 int DFS(int v,int t,int f){
64     if(v==t) return f;
65     for(int &i=iter[v];i!=-1;i=e[i].nxt){
66         if(d[v]<d[e[i].to]&&e[i].w>0){
67             int md=DFS(e[i].to,t,min(f,e[i].w));
68             if(md<=0) continue;
69             e[i].w-=md;
70             e[e[i].rev].w+=md;
71             return md;
72         }
73     }
74     return 0;
75 }
76 
77 void max_Flow(int s,int t){
78     int ans=0;
79     while(true){
80         BFS(s);
81         if(d[t]<0) break;
82         for(int i=0;i<=n+1;i++) iter[i]=head[i];
83         int temp=0;
84         while((temp=DFS(s,t,INF))>0) ans=ans+temp;
85     }
86     cout<<ans<<endl;
87 }
88 
89 int main()
90 {   while(~scanf("%d%d",&n,&m)){
91         Init();
92         Read();
93         max_Flow(0,n+1);
94     }
95     return 0;
96 }

 

posted @ 2017-08-21 15:33  天之道,利而不害  阅读(208)  评论(0)    收藏  举报