网络流-poj3469

来源:互联网 发布:蜂群算法 pid 编辑:程序博客网 时间:2024/04/29 04:34
Language:
Dual Core CPU
Time Limit: 15000MS Memory Limit: 131072KTotal Submissions: 16933 Accepted: 7292Case 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 extraw dollars for the data-exchange between them.

Output

Output only one integer, the minimum total cost.

Sample Input

3 11 102 1010 32 3 1000

Sample Output

13Dinic超时,去学一下SAP在做。。。下面是Dinic的代码:
#include<iostream>#include<set>#include<map>#include<vector>#include<queue>#include<cmath>#include<climits>#include<cstdio>#include<string>#include<cstring>#include<algorithm>typedef long long LL;using namespace std;const int MAX_X=200010*3;const int INF=1000000000;struct edge{    int to,cost,rev;};vector<edge> G[MAX_X];int d[20010];int cur[20010];void add_edge(int u,int v,int cost){    edge a,b;    a.to=v,a.cost=cost,a.rev=G[v].size();    b.to=u,b.cost=cost,b.rev=G[u].size()-1;    G[u].push_back(a);    G[v].push_back(b);}int bfs(int s,int t){    queue<int> q;    memset(d,-1,sizeof(d));    d[s]=0;    q.push(s);    while(!q.empty())    {        int top=q.front();        q.pop();        if(top==t)            return 1;        for(int i=0;i<G[top].size();i++)        {            if(d[G[top][i].to]<0&&G[top][i].cost>0)            {                d[G[top][i].to]=d[top]+1;                q.push(G[top][i].to);            }        }    }    return 0;}int dfs(int now,int t,int maxn){    int res=0,f;    if(now==t)        return maxn;    for(int &i=cur[now];i<G[now].size();i++)    {        if(G[now][i].cost>0&&d[now]<d[G[now][i].to])        {            f=dfs(G[now][i].to,t,min(maxn-res,G[now][i].cost));            G[now][i].cost-=f;            G[G[now][i].to][G[now][i].rev].cost+=f;            res+=f;            if(res==maxn)                return maxn;        }    }    return res;}void dinic(int s,int t){    int res=0;    memset(cur,0,sizeof(cur));    while(bfs(s,t))    {         res+=dfs(s,t,INF);    }    cout<<res<<endl;}int main(){    //freopen("in.txt","r",stdin);    int N,M;    int a,b,w;    cin>>N>>M;        int s=N,t=s+1;        for(int i=0;i<N;i++)        {            scanf("%d%d",&a,&b);            add_edge(i,t,a);            add_edge(s,i,b);        }        for(int i=0;i<M;i++)        {            scanf("%d%d%d",&a,&b,&w);            add_edge(a,b,w);        }        dinic(s,t);    return 0;}

 
原创粉丝点击