码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3469 Dual Core CPU 最大流

时间:2014-05-08 14:23:18      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   tar   

划分成两个集合使费用最小,可以转成最小割,既最大流。

bubuko.com,布布扣
bubuko.com,布布扣
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define pb(a) push(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define PI  3.1415926535898
template<class T> T min(const T& a,const T& b,const T& c)
{
    return min(min(a,b),min(a,c));
}
template<class T> T max(const T& a,const T& b,const T& c)
{
    return max(max(a,b),max(a,c));
}
void debug()
{
#ifdef ONLINE_JUDGE
#else
    freopen("data.in","r",stdin);
    // freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch()
{
    int ch;
    while((ch=getchar())!=EOF)
    {
        if(ch!= &&ch!=\n)return ch;
    }
    return EOF;
}

struct Edge
{
    int to,cap;
};
const int maxn = 20010;
vector<int> g[maxn];
vector<Edge> edge;
int n,m,s,t;
int d[maxn];
int vis[maxn];
int cur[maxn];

void add(int u,int v,int cap)
{
    //printf("%d -> %d : %d\n", u, v, cap);
    edge.push_back((Edge){v, cap});
    g[u].push_back(edge.size() - 1);
    edge.push_back((Edge){u, 0});
    g[v].push_back(edge.size() - 1);
}
void build()
{
    for(int i = 1; i <= n; i++)
        g[i].clear();
    edge.clear();
    s = n + 1;
    t = s + 1;
    for(int i = 1; i <= n; i++)
    {
        int a,b;
        scanf("%d%d", &a, &b);
        add(s, i, a);
        add(i, t, b);
    }
    for(int i = 1; i <= m; i++)
    {
        int a,b,w;
        scanf("%d%d%d", &a, &b, &w);
        add(a, b, w);
        add(b, a, w);
    }
}

bool bfs()
{
    memset(vis, 0, sizeof(vis));
    d[s] = 0;
    queue<int> q;
    q.push(s);
    vis[s] = true;
    while(!q.empty())
    {
        int x = q.front(); q.pop();
        for (int i = 0; i < g[x].size(); i++)
        {
            Edge &e = edge[g[x][i]];
            if(e.cap>0 && !vis[e.to])
            {
                vis[e.to] = true;
                q.push(e.to);
                d[e.to] = d[x] + 1;
            }
        }
    }
    return vis[t];
}

int dfs(int u,int f)
{
    if(u==t || f==0) return f;
    for(int &i = cur[u]; i < g[u].size(); i++)
    {
        Edge &e = edge[g[u][i]];
        if(e.cap>0 && d[e.to] == d[u] + 1)
        {
            int d = dfs(e.to, min(f,e.cap));
            if(d>0)
            {
                e.cap -= d;
                edge[g[u][i]^1].cap += d;
                return d;
            }
        }
    }
    return 0;
}

int max_flow()
{
    int res = 0;
    while(bfs())
    {
        memset(cur, 0, sizeof(cur));
        int d;
        while(d=dfs(s,INF))
        {
            res += d;
        }
    }
    return res;
}
int main()
{
    debug();
    while(scanf("%d%d", &n, &m) != EOF)
    {
        build();
        printf("%d\n", max_flow());
    }
    return 0;
}
View Code
bubuko.com,布布扣

 

POJ 3469 Dual Core CPU 最大流,布布扣,bubuko.com

POJ 3469 Dual Core CPU 最大流

标签:style   blog   class   code   java   tar   

原文地址:http://www.cnblogs.com/BMan/p/3713704.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!