Dual Core CPU

题目描述  (传送门

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

 

AC代码

#include<iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <cstdio>
using namespace std;
#define IO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mset(a, n) memset(a, n, sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int,int> PII;
typedef pair<int,PII> PPI;
typedef pair<ll, ll> PLL;
const int INF = 0x3f3f3f3f;
const int MOD = 998244353;
const int N_max = 40000+100;

int V, E, S, T, K;
int level[N_max];
int iter[N_max];
struct edge{
    int to,cap,rev;
    edge(){}
    edge(int _t, int _c, int _r):
        to(_t), cap(_c), rev(_r){}
};
vector<vector<edge> >G;

void add_edge(int from,int to, int cap)
{
    G[from].pb(edge(to,cap,G[to].size()));
    G[to].pb(edge(from,0,G[from].size()-1));
}

/// 计算从源点到每个点的距离
bool bfs(int s){
    mset(level,-1);
    queue<int> Q;
    level[s] = 0;
    Q.push(s);
    while(!Q.empty()){
        int u = Q.front(); Q.pop();
        for(int i = 0; i<G[u].size();i++){
            edge &e = G[u][i];
            if(e.cap>0 && level[e.to]<0){
                level[e.to] = level[u]+1;
                Q.push(e.to);
            }
        }
    }
    if(level[T]==-1) return false;
    else return true;
}
int dfs(int v,int t, int f){
    if(v == t) return f;
    for(int &i = iter[v]; i<G[v].size();i++){
        edge &e = G[v][i];
        if(e.cap>0&&level[v]<level[e.to]){
            int d = dfs(e.to, t, min(f, e.cap));
            if(d>0){
                e.cap -= d;
                G[e.to][e.rev].cap += d;
                return d;
            }
        }
    }
    return 0;
}

int max_flow(int s, int t){
    int flow = 0;
    while(bfs(s)){
        mset(iter, 0);
        int f;
        while ((f = dfs(s,t,INF))>0) flow+=f;
    }
    return flow;
}

int main()
{
    /// 建图
    scanf("%d%d", &V, &E);
    G.clear();  G.resize(V+3);
    S = V+1, T = V+2;

    for(int i=1;i<=V;i++){
        int a, b;
        scanf("%d%d", &a, &b);
        add_edge(S, i, a);
        add_edge(i, T, b);
    }
    for(int i=1;i<=E;i++){
        int a, b, val;
        scanf("%d%d%d", &a, &b, &val);
        add_edge(a, b, val);
        add_edge(b, a, val);
    }
    int ans = max_flow(S, T);
    cout<<ans<<'\n';
    return 0;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值