POJ 3469 —— 最大流

Dual Core CPU
Time Limit: 15000MS   Memory Limit: 131072K
Total Submissions: 17034   Accepted: 7350
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 andBi. 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

POJ Monthly--2007.11.25, Zhou Dong

题意是有两个CPU,A和B,有N个任务要完成,n[i]在A上完成的花费是A[i],在B上完成的花费是B[i],其中有M个关联事件,如果a任务和b任务在同一个CPU上完成则不要额外花费,否则额外花c元,求完成所有工作的最小花费

思路是建一个超级源点S,一个超级汇点T,权值分别为A[i],B[i],关联事件任务的结点之间建一条边,然后用Dinic求最大流(图的规模比较大用Dinic比较好)。

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 20000 + 50;
const int MAXS = 200000 + 50;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
const int inf = 1 << 30;
#define eps 1e-10
const long long MOD = 1000000000 + 7;
const int mod = 10007;
typedef long long LL;
const double PI = acos(-1.0);
//typedef double D;
typedef pair<int , int> pii;
typedef vector<int> vec;
typedef vector<vec> mat;

#define Bug(s) cout << "s = " << s << endl;
///#pragma comment(linker, "/STACK:102400000,102400000";
int N , M;
int A[MAXN] , B[MAXN];
int a[MAXS] , b[MAXS] , c[MAXS];
struct edge{int to , cap , rev;};
vector<edge>G[MAXN];
int level[MAXN];
int iter[MAXN];
void add_edge(int from , int to , int cap)
{
    G[from].push_back((edge){to , cap , G[to].size()});
    G[to].push_back((edge){from , 0 , G[from].size() - 1});
}
void bfs(int s)
{
    memset(level , -1 , sizeof(level));
    queue<int>que;
    level[s] = 0;
    que.push(s);
    while(!que.empty())
    {
        int v = que.front();que.pop();
        for(int i = 0 ; i < G[v].size() ; i++)
        {
            edge &e = G[v][i];
            if(e.cap > 0 && level[e.to] < 0)
            {
                level[e.to] = level[v] + 1;
                que.push(e.to);
            }
        }
    }
}
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;
    for(;;)
    {
        bfs(s);
        if(level[t] < 0)return flow;
        memset(iter , 0 , sizeof(iter));
        int f;
        while((f = dfs(s , t , INF)) > 0)flow += f;
    }
}
void solve()
{
    int s = N , t = s + 1;
    for(int  i = 0 ; i < N ; i++)
    {
        add_edge(i , t , A[i]);
        add_edge(s , i , B[i]);
    }
    for(int i = 0 ; i < M ; i++)
    {
        add_edge(a[i] - 1 , b[i] - 1 , c[i]);
        add_edge(b[i] - 1 , a[i] - 1 , c[i]);
    }
    printf("%d\n" , max_flow(s , t));
}
int main()
{
    while(~scanf("%d%d" , &N , &M))
    {
        for(int i = 0 ; i <N ; i++)scanf("%d%d" , &A[i] , &B[i]);
        for(int i = 0 ; i < M;  i++)scanf("%d%d%d" , &a[i] , &b[i] , &c[i]);
        solve();
    }
    return 0;
}


你可能感兴趣的:(ACM,图论,最大流)