poj 3469 Dual Core CPU(最小费用最大流)

来源:互联网 发布:河南昭大网络靠谱吗 编辑:程序博客网 时间:2024/05/18 16:15
Dual Core CPU
Time Limit:15000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu
Submit Status Practice POJ 3469
Appoint description: 

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: 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 11 102 1010 32 3 1000

Sample Output

13
不是说好的1 ≤ N ≤ 20000, 1 ≤ M ≤ 200000  wa了好多遍尴尬   dinic算法模板,简单易懂
#include<stdio.h>#include <vector>#include <string.h>#include<iostream>#include <queue>#define max_V 321000using namespace std;const int INF = 0x3f3f3f3f;const int max_N=320000,max_M=1000000;struct edge{    int to,cap,rev;//终点 容量 反向边};int level[max_V];//顶点到源点的距离标号int iter[max_V];//当前弧,在其之前的边已经没有用了vector<edge> G[max_V];void add_edge(int from ,int to, int  cap){    G[from].push_back((edge){to,cap,G[to].size()});//to返回from时的反向边,G[e.to][e.cap]即to返回from    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;     while(1)     {         bfs(s);         if(level[t]<0)return flow;         memset(iter,0,sizeof(iter));         int f;        while((f=dfs(s,t,INF))>0)        {            flow+=f;        }     } }int N,M;int A[max_N],B[max_N];int a[max_N],b[max_N],w[max_M]; void solve() {    int s=N;    int 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,w[i]);        add_edge(b[i]-1,a[i]-1,w[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],&w[i]);}solve();}    return 0;}


0 0
原创粉丝点击