POJ 3469 Dual Core CPU Dual Core CPU

Time Limit: 15000MS   Memory Limit: 131072K
Total Submissions: 23780   Accepted: 10338
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

[Submit]   [Go Back]   [Status]   [Discuss]

 

解法:最小割。
源点S向每个任务连边,容量Ai。
每个任务向汇点T连边,容量Bi。
对于二元组关系(x,y),在x,y之间连双向边,容量C。
最小割就是最优的方案。
每个任务与S割开表示在A上完成,与T割开表示在B上完成。
——Lydrainbowcat
 
  1 #include <cstdio>
  2 #include <cstring>
  3 
  4 inline char nextChar(void)
  5 {
  6     static const int siz = 1 << 20;
  7     
  8     static char buf[siz];
  9     static char *hd = buf + siz;
 10     static char *tl = buf + siz;
 11     
 12     if (hd == tl)
 13         fread(hd = buf, 1, siz, stdin);
 14     
 15     return *hd++;
 16 }
 17 
 18 inline int nextInt(void)
 19 {
 20     register int ret = 0;
 21     register bool neg = false;
 22     register char bit = nextChar();
 23     
 24     for (; bit < 48; bit = nextChar())
 25         if (bit == '-')neg ^= true;
 26     
 27     for (; bit > 47; bit = nextChar())
 28         ret = ret * 10 + bit - '0';
 29     
 30     return neg ? -ret : ret;
 31 }
 32 
 33 const int inf = 2e9;
 34 
 35 const int maxn = 200005;
 36 const int maxm = 5000005;
 37 
 38 int n, m;
 39 int s, t;
 40 int hd[maxn];
 41 int to[maxm];
 42 int nt[maxm];
 43 int fl[maxm];
 44 
 45 inline void addEdge(int u, int v, int f)
 46 {
 47     static int tot = 0;
 48     static bool init = true;
 49     
 50     if (init)
 51         memset(hd, -1, sizeof(hd)), init = false;
 52     
 53     nt[tot] = hd[u]; to[tot] = v; fl[tot] = f; hd[u] = tot++;
 54     nt[tot] = hd[v]; to[tot] = u; fl[tot] = 0; hd[v] = tot++;
 55 }
 56 
 57 int dep[maxn];
 58 
 59 inline bool bfs(void)
 60 {
 61     static int que[maxn];
 62     static int head, tail;
 63     
 64     memset(dep, 0, sizeof(dep));
 65     que[head = 0] = s, dep[s] = tail = 1;
 66     
 67     while (head != tail)
 68     {
 69         int u = que[head++], v;
 70         
 71         for (int i = hd[u]; ~i; i = nt[i])
 72             if (fl[i] && !dep[v = to[i]])
 73                 dep[que[tail++] = v] = dep[u] + 1;
 74     }
 75     
 76     return dep[t];
 77 }
 78 
 79 int cur[maxn];
 80 
 81 inline int min(int a, int b)
 82 {
 83     return a < b ? a : b;
 84 }
 85 
 86 int dfs(int u, int f)
 87 {
 88     if (!f || u == t)
 89         return f;
 90     
 91     int used = 0, flow, v;
 92     
 93     for (int i = cur[u]; ~i; i = nt[i])
 94         if (fl[i] && dep[v = to[i]] == dep[u] + 1)
 95         {
 96             flow = dfs(v, min(fl[i], f - used));
 97             
 98             used += flow;
 99             fl[i] -= flow;
100             fl[i^1] += flow;
101             
102             if (fl[i])
103                 cur[u] = i;
104             
105             if (used == f)
106                 return f;
107         }
108     
109     if (!used)
110         dep[u] = 0;
111     
112     return used;
113 }
114 
115 inline int maxFlow(void)
116 {
117     int maxFlow = 0, newFlow;
118     
119     while (bfs())
120     {
121         memcpy(cur, hd, sizeof(hd));
122         
123         while (newFlow = dfs(s, inf))
124             maxFlow += newFlow;
125     }
126     
127     return maxFlow;
128 }
129 
130 signed main(void)
131 {
132     n = nextInt();
133     m = nextInt();
134     
135     s = 0, t = n + 1;
136     
137     for (int i = 1; i <= n; ++i)
138     {
139         int ai = nextInt();
140         int bi = nextInt();
141         
142         addEdge(s, i, ai);
143         addEdge(i, t, bi);
144     }
145     
146     for (int i = 1; i <= m; ++i)
147     {
148         int x = nextInt();
149         int y = nextInt();
150         int w = nextInt();
151         
152         addEdge(x, y, w);
153         addEdge(y, x, w);
154     }
155     
156     printf("%d\n", maxFlow());
157 }

 

@Author: YouSiki

 

 

posted @ 2017-01-22 14:13  YouSiki  阅读(231)  评论(0编辑  收藏  举报