标签:des style blog class code tar
Time Limit: 15000MS | Memory Limit: 131072K | |
Total Submissions: 18120 | Accepted: 7818 | |
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: 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
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 8 using namespace std; 9 10 const int MAX_N = 20005; 11 const int INF = (1 << 30); 12 int N,M; 13 struct Edge{ int from, to, cap, flow; }; 14 vector<Edge> edges; 15 int cur[MAX_N],d[MAX_N]; 16 vector<int> G[MAX_N]; 17 bool vis[MAX_N]; 18 19 void add_edge(int from, int to, int cap) { 20 edges.push_back(Edge {from, to, cap, 0}); 21 edges.push_back(Edge {to, from, 0, 0}); 22 int m = edges.size(); 23 G[from].push_back(m - 2); 24 G[to].push_back(m - 1); 25 } 26 27 bool bfs(int s,int t) { 28 29 memset(vis,0,sizeof(vis)); 30 queue<int> q; 31 q.push(s); 32 d[s] = 0; 33 vis[s] = 1; 34 while (!q.empty()) { 35 int x = q.front(); q.pop(); 36 for (int i = 0; i < G[x].size(); ++i) { 37 Edge& e = edges[ G[x][i] ]; 38 if (!vis[e.to] && e.cap > e.flow) { 39 d[e.to] = d[x] + 1; 40 vis[e.to] = 1; 41 q.push(e.to); 42 } 43 } 44 } 45 46 return vis[t]; 47 } 48 49 int dfs(int x, int a, int t) { 50 if(x == t || a == 0) return a; 51 int flow = 0,f; 52 for(int& i = cur[x]; i < G[x].size(); ++i) { 53 Edge& e = edges[ G[x][i] ]; 54 if(d[x] + 1 == d[e.to] && (f = dfs(e.to,min(a,e.cap - e.flow),t)) > 0) { 55 e.flow += f; 56 edges[ G[x][i] ^ 1].flow -= f; 57 a -= f; 58 flow += f; 59 if(a == 0) break; 60 61 } 62 63 } 64 65 return flow; 66 } 67 68 int Maxflow(int s, int t) { 69 70 int flow = 0; 71 while (bfs(s,t)) { 72 //printf("fuck\n"); 73 memset(cur,0,sizeof(cur)); 74 flow += dfs(s, INF, t); 75 } 76 return flow; 77 } 78 int main() 79 { 80 //freopen("sw.in","r",stdin); 81 scanf("%d%d", &N, &M); 82 for (int i = 1; i <= N; ++i) { 83 int a,b; 84 scanf("%d%d", &a, &b); 85 add_edge(0,i,a); 86 add_edge(i,N + 1,b); 87 } 88 89 for (int i = 1; i <= M; ++i) { 90 int a,b,w; 91 scanf("%d%d%d",&a,&b,&w); 92 add_edge(a,b,w); 93 add_edge(b,a,w); 94 } 95 96 printf("%d\n",Maxflow(0,N + 1)); 97 //cout << "Hello world!" << endl; 98 return 0; 99 }
标签:des style blog class code tar
原文地址:http://www.cnblogs.com/hyxsolitude/p/3712805.html