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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章