hdu 6124 Smallest Minimum Cut

来源:互联网 发布:js mouseover 编辑:程序博客网 时间:2024/05/22 12:53

Smallest Minimum Cut

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 83    Accepted Submission(s): 21


Problem Description
Consider a network G=(V,E) with source s and sink t. An s-t cut is a partition of nodes set V into two parts such that s and t belong to different parts. The cut set is the subset of E with all edges connecting nodes in different parts. A minimum cut is the one whose cut set has the minimum summation of capacities. The size of a cut is the number of edges in the cut set. Please calculate the smallest size of all minimum cuts.
 

Input
The input contains several test cases and the first line is the total number of casesT (1T300).
Each case describes a network
G, and the first line contains two integers n (2n200) and m (0m1000) indicating the sizes of nodes and edges. All nodes in the network are labelled from1 to n.
The second line contains two different integers
s and t (1s,tn) corresponding to the source and sink.
Each of the next
m lines contains three integers u,v and w (1w255) describing a directed edge from node u to v with capacity w.
 

Output
For each test case, output the smallest size of all minimum cuts in a line.
 

Sample Input
24 51 41 2 31 3 12 3 12 4 13 4 24 51 41 2 31 3 12 3 12 4 13 4 3
 

Sample Output
23
 

Source
2017 ACM/ICPC Asia Regional Qingdao Online
 

/*题意:求最小割中边数最少的边数题解:在建图时,每个边权乘以一个大的数E,然后加1,求出最大流后对E取模,就可以得到边数。*/#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <queue>using namespace std;typedef long long ll;const ll maxv = 2000+10;const ll INF = 0x3f3f3f3f;struct edge{    ll to,cap,rev;};vector <edge> G[maxv];ll step[maxv];//从源点到点x的距离ll iter[maxv];//定点x的第几条边开始有用void add_edge(ll from, ll to, ll cap){    G[from].push_back((edge){to,cap,G[to].size()});    G[to].push_back((edge){from,0,G[from].size()-1});}void bfs(ll s){    memset(step,-1,sizeof(step));    step[s]=0;    queue <ll> q;    q.push(s);    while(!q.empty())    {        ll v=q.front();        q.pop();        for(ll i=0;i<G[v].size();++i)        {            edge &e = G[v][i];            if(e.cap>0 && step[e.to]<0)            {                step[e.to]=step[v]+1;                q.push(e.to);            }        }    }}ll dfs(ll v,ll t, ll f){    if(v==t) return f;    for(ll &i=iter[v];i<G[v].size();++i)//这里是引用,i++的同时iter也++,其实相当于上个的used,不过不用判断了    {        edge &e = G[v][i];        if(e.cap>0 && step[e.to]>step[v])        {            ll d=dfs(e.to,t,min(e.cap,f));            if(d>0)            {                e.cap-=d;                G[e.to][e.rev].cap+=d;                return d;            }        }    }    return 0;}ll max_flow(ll s,ll t){    ll flow =0;    for(;;)    {        bfs(s);        if(step[t]<0) return flow;        memset(iter,0,sizeof(iter));        ll f;        while((f = dfs(s,t,INF))>0)            flow += f;    }}ll n,m;ll s,t;ll w;int main(){    ll T;    scanf("%lld",&T);    while(T--){        scanf("%lld%lld",&n,&m);        scanf("%lld%lld",&s,&t);        for(ll i = 0;i<maxv;++i) G[i].clear();        for(ll i=0;i<m;++i){            ll c,d;            scanf("%lld%lld%lld",&c,&d,&w);            add_edge(c,d,w*1000+1);        }        printf("%lld\n",max_flow(s,t)%1000);    }    return 0;}


原创粉丝点击