Hdu 6214 Smallest Minimum Cut(最小割)

来源:互联网 发布:剑三抢激活码软件 编辑:程序博客网 时间:2024/06/05 19:24

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6214

思路:

1.要求在最小割的情况下割集边数最小,对于两种约束条件,可通过一定方法将其转化为单约束。

2.令边权值为w*MAX+1,MAX为一较大数,使得当割不同时,边权值作用最大;当割相同时,边数起作用。

3.求最大流,边数即为maxFlow%MAX。

#include<cmath>#include<queue>#include<cstdio>#include<vector>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef long long LL;const int maxn = 400+50;const int INF = 0x3f3f3f3f;struct Edge{    int from, to;    LL cap, flow;    Edge(int a,int b,LL c,LL d):from(a),to(b),cap(c),flow(d) {}};struct Dinic{    int n, m, s, t;    vector<Edge> edges;    vector<int> G[maxn];    bool vis[maxn];    LL d[maxn];    int cur[maxn];    void init(int n)    {        this->n=n;        for(int i = 0; i < n; i++) G[i].clear();        edges.clear();    }    void addEdge(int from, int to, LL cap)    {        edges.push_back(Edge(from,to,cap,0));        edges.push_back(Edge(to,from,0,0));        m = edges.size();        G[from].push_back(m-2);        G[to].push_back(m-1);    }    bool BFS()    {        memset(vis, 0, sizeof(vis));        queue<int> Q;        Q.push(s);        vis[s] = 1;        d[s] = 0;        while(!Q.empty())        {            int x = Q.front();            Q.pop();            for(int i = 0; i < G[x].size(); i++)            {                Edge& e = edges[G[x][i]];                if(!vis[e.to] && e.cap > e.flow)                {                    vis[e.to] = 1;                    d[e.to] = d[x] + 1;                    Q.push(e.to);                }            }        }        return vis[t];    }    int DFS(int x, LL a)    {        if(x == t || a == 0) return a;        LL flow = 0, f;        for(int& i = cur[x]; i < G[x].size(); i++)        {            Edge& e = edges[G[x][i]];            if(d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap-e.flow))) > 0)            {                e.flow += f;                edges[G[x][i]^1].flow -= f;                flow += f;                a -= f;                if(a == 0) break;            }        }        return flow;    }    LL MaxFlow(int s, int t)    {        this->s = s;        this->t = t;        LL flow = 0;        while(BFS())        {            memset(cur, 0, sizeof(cur));            flow += DFS(s, INF);        }        return flow;    }};Dinic g;int n,m,s,t;int main(){#ifdef debu    freopen("in.txt","r",stdin);#endif // debug    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&n,&m);        scanf("%d%d",&s,&t);        g.init(n+1);        for(int i=0;i<m;i++)        {            int x,y,w;            scanf("%d%d%d",&x,&y,&w);            g.addEdge(x,y,(LL)w*1001+1);        }        printf("%lld\n",g.MaxFlow(s,t)%1001);    }    return 0;}



阅读全文
0 0
原创粉丝点击