zoukankan      html  css  js  c++  java
  • POJ 3469 Dual Core CPU

    Dual Core CPU

    Time Limit: 15000ms
    Memory Limit: 131072KB
    This problem will be judged on PKU. Original ID: 3469
    64-bit integer IO format: %lld      Java class name: Main
     

    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, Aiand 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

     
    解题:比较经典的最小割模型。
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 using namespace std;
    17 const int maxn = 20100;
    18 struct arc{
    19     int to,flow,next;
    20     arc(int x = 0,int y = 0,int z = -1){
    21         to = x;
    22         flow = y;
    23         next = z;
    24     }
    25 };
    26 arc e[1000000];
    27 int head[maxn],d[maxn],cur[maxn],tot,S,T;
    28 int q[maxn<<2],hd,tl;
    29 void add(int u,int v,int w){
    30     e[tot] = arc(v,w,head[u]);
    31     head[u] = tot++;
    32     e[tot] = arc(u,0,head[v]);
    33     head[v] = tot++;
    34 }
    35 bool bfs(){
    36     memset(d,-1,sizeof(d));
    37     hd = tl = 0;
    38     q[tl++] = S;
    39     d[S] = 1;
    40     while(hd < tl){
    41         int u = q[hd++];
    42         for(int i = head[u]; ~i; i = e[i].next){
    43             if(e[i].flow && d[e[i].to] == -1){
    44                 d[e[i].to] = d[u] + 1;
    45                 q[tl++] = e[i].to;
    46             }
    47         }
    48     }
    49     return d[T] > -1;
    50 }
    51 int dfs(int u,int low){
    52     if(u == T) return low;
    53     int tmp = 0,a;
    54     for(int &i = cur[u]; ~i; i = e[i].next){
    55         if(e[i].flow && d[e[i].to] == d[u] + 1 && (a = dfs(e[i].to,min(e[i].flow,low)))){
    56             tmp += a;
    57             low -= a;
    58             e[i].flow -= a;
    59             e[i^1].flow += a;
    60             if(!low) break;
    61         }
    62     }
    63     if(!tmp) d[u] = -1;
    64     return tmp;
    65 }
    66 int main() {
    67     int u,v,w,ans,n,m;
    68     while(~scanf("%d %d",&n,&m)){
    69         memset(head,-1,sizeof(head));
    70         ans = S = tot = 0;
    71         T = n + 1;
    72         for(int i = 1; i <= n; i++){
    73             scanf("%d %d",&u,&v);
    74             add(S,i,u);
    75             add(i,T,v);
    76         }
    77         for(int i = 0; i < m; i++){
    78             scanf("%d %d %d",&u,&v,&w);
    79             add(u,v,w);
    80             add(v,u,w);
    81         }
    82         while(bfs()){
    83             memcpy(cur,head,sizeof(head));
    84             ans += dfs(S,INF);
    85         }
    86         printf("%d
    ",ans);
    87     }
    88     return 0;
    89 }
    View Code

     递归SAP

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 using namespace std;
     6 const int INF = ~0U>>2;
     7 const int maxn = 20010;
     8 struct arc {
     9     int to,flow,next;
    10     arc(int x = 0,int y = 0,int z = -1) {
    11         to = x;
    12         flow = y;
    13         next = z;
    14     }
    15 } e[1000010];
    16 int head[maxn],d[maxn],gap[maxn],tot,S,T,n;
    17 void add(int u,int v,int flow) {
    18     e[tot] = arc(v,flow,head[u]);
    19     head[u] = tot++;
    20     e[tot] = arc(u,0,head[v]);
    21     head[v] = tot++;
    22 }
    23 int sap(int u,int low) {
    24     if(u == T) return low;
    25     int tmp = 0,a,minh = n - 1;
    26     for(int i = head[u]; ~i; i = e[i].next) {
    27         if(e[i].flow) {
    28             if(d[u] == d[e[i].to] + 1 &&(a = sap(e[i].to,min(low,e[i].flow)))) {
    29                 e[i].flow -= a;
    30                 e[i^1].flow += a;
    31                 tmp += a;
    32                 low -= a;
    33                 if(!low) break;
    34             }
    35             minh = min(minh,d[e[i].to]);
    36             if(d[S] >= n) return tmp;
    37         }
    38     }
    39     if(!tmp) {
    40         if(--gap[d[u]] == 0) d[S] = n;
    41         d[u] = minh + 1;
    42         ++gap[d[u]];
    43     }
    44     return tmp;
    45 }
    46 int main() {
    47     int u,v,w,a,b;
    48     while(~scanf("%d%d",&a,&b)) {
    49         memset(head,-1,sizeof head);
    50         memset(gap,0,sizeof gap);
    51         memset(d,0,sizeof d);
    52         S = tot = 0;
    53         T = a + 1;
    54         n = T + 1;
    55         for(int i = 1; i <= a; ++i) {
    56             scanf("%d%d",&u,&v);
    57             add(S,i,u);
    58             add(i,T,v);
    59         }
    60         for(int i = 0; i < b; ++i) {
    61             scanf("%d%d%d",&u,&v,&w);
    62             add(u,v,w);
    63             add(v,u,w);
    64         }
    65         gap[S] = n;
    66         int ret = 0;
    67         while(d[S] < n) ret += sap(S,INF);
    68         printf("%d
    ",ret);
    69     }
    70     return 0;
    71 }
    View Code

    加输入挂的ISAP在g++下表现不错

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <cmath>
      5 #include <algorithm>
      6 #include <climits>
      7 #include <vector>
      8 #include <queue>
      9 #include <cstdlib>
     10 #include <string>
     11 #include <set>
     12 #include <stack>
     13 #define LL long long
     14 #define pii pair<int,int>
     15 #define INF 0x3f3f3f3f
     16 using namespace std;
     17 const int maxn = 20100;
     18 struct arc {
     19     int to,flow,next;
     20     arc(int x = 0,int y = 0,int z = -1) {
     21         to = x;
     22         flow = y;
     23         next = z;
     24     }
     25 };
     26 arc e[1000000];
     27 int head[maxn],gap[maxn],d[maxn],cur[maxn];
     28 int S,T,tot,hd,tl,q[maxn],p[maxn];
     29 int getnum() {
     30     char ch;
     31     int num = 0,flag = 0;
     32     while(((ch = getchar()) < '0' || ch > '9') && ch != '-')
     33         if(ch == EOF)  exit(0);
     34     if(ch == '-')   flag = 1;
     35     else num = ch - '0';
     36     while((ch = getchar()) >= '0' && ch <= '9')
     37         num = num * 10 + ch - '0';
     38     if(flag)   num *= -1;
     39     return num;
     40 }
     41 void add(int u,int v,int w) {
     42     e[tot] = arc(v,w,head[u]);
     43     head[u] = tot++;
     44     e[tot] = arc(u,0,head[v]);
     45     head[v] = tot++;
     46 }
     47 void bfs() {
     48     hd = tl = 0;
     49     memset(d,-1,sizeof(d));
     50     memset(gap,0,sizeof(gap));
     51     q[tl++] = T;
     52     d[T] = 0;
     53     while(hd < tl) {
     54         int u = q[hd++];
     55         ++gap[d[u]];
     56         for(int i = head[u]; ~i; i = e[i].next) {
     57             if(d[e[i].to] == -1) {
     58                 d[e[i].to] = d[u] + 1;
     59                 q[tl++] = e[i].to;
     60             }
     61         }
     62     }
     63 }
     64 int isap() {
     65     int maxFlow = 0,flow = INF,u = S;
     66     memcpy(cur,head,sizeof(head));
     67     bfs();
     68     while(d[S] < T) {
     69         int &i = cur[u];
     70         for(; ~i; i = e[i].next)
     71             if(e[i].flow && d[e[i].to] + 1 == d[u]) break;
     72         if(i > -1) {
     73             flow = min(flow,e[i].flow);
     74             p[u = e[i].to] = i;
     75             if(u == T) {
     76                 do {
     77                     int v = p[u];
     78                     e[v].flow -= flow;
     79                     e[v^1].flow += flow;
     80                     u = e[v^1].to;
     81                 } while(u != S);
     82                 maxFlow += flow;
     83                 flow = INF;
     84             }
     85         } else {
     86             if(--gap[d[u]] == 0) break;
     87             d[u] = T;
     88             cur[u] = head[u];
     89             for(int k = head[u]; ~k; k = e[k].next)
     90                 if(e[k].flow && d[e[k].to] + 1 < d[u])
     91                     d[u] = d[e[k].to] + 1;
     92                 ++gap[d[u]];
     93                 if(u != S) u = e[p[u]^1].to;
     94         }
     95     }
     96     return maxFlow;
     97 }
     98 int main() {
     99     int n,m,u,v,w;
    100     while(~scanf("%d %d",&n,&m)) {
    101         memset(head,-1,sizeof(head));
    102         S = tot = 0;
    103         T = n + 1;
    104         for(int i = 1; i <= n; i++) {
    105             //scanf("%d %d",&u,&v);
    106             u = getnum();
    107             v = getnum();
    108             add(S,i,u);
    109             add(i,T,v);
    110         }
    111         for(int i = 0; i < m; i++) {
    112             //scanf("%d %d %d",&u,&v,&w);
    113             u = getnum();
    114             v = getnum();
    115             w = getnum();
    116             add(u,v,w);
    117             add(v,u,w);
    118         }
    119         printf("%d
    ",isap());
    120     }
    121     return 0;
    122 }
    View Code
  • 相关阅读:
    数据流控制
    转:简单的Mysql主从复制设置
    转:CentOS---网络配置详解
    Linux-vim学习入门
    Linux图形界面与字符界面切换
    转:MySQL表名不区分大小写
    CentOS6.5_x86安装Mysql5.5.49
    Linux的环境变量设置和查看
    Linux防火墙的关闭和开启
    Linux command not found 问题解释
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3992689.html
Copyright © 2011-2022 走看看