HDU

来源:互联网 发布:张一山 演技 知乎 编辑:程序博客网 时间:2024/05/17 22:43

2017 ACM/ICPC Asia Regional Qingdao Online 1009

Smallest Minimum Cut



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 cases T (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 from 1 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
 

题意:最小最小割。带起点和终点。


解题思路:套了份模板……也忘了是谁了的了……总之感谢该大佬的博客……



#include<iostream>#include<deque>#include<memory.h>#include<stdio.h>#include<map>#include<string.h>#include<algorithm>#include<vector>#include<math.h>#include<stack>#include<queue>#include<set>using namespace std;typedef long long LL;const LL INF = 100000000000000000;const int MAXN = 5010;const int MAXM = 1000010;struct Edge{    LL flow, cap;    int to, next, pair;}edge[MAXM<<1];int cnt, box[MAXN];void init(){    cnt = 1;    memset(box,-1,sizeof(box));}void addedge(int from, int to, LL cap1, LL cap2=0){    edge[cnt].to = to;    edge[cnt].next = box[from];    edge[cnt].cap = cap1;    edge[cnt].flow = 0;    edge[cnt].pair = cnt + 1;    box[from] = cnt++;    edge[cnt].to = from;    edge[cnt].next = box[to];    edge[cnt].cap = cap2;    edge[cnt].flow = 0;    edge[cnt].pair = cnt - 1;    box[to] = cnt++;}LL SAP(int st, int ed, int n){    int u, tmp, neck, i;    LL max_flow, cur_flow;    int numb[MAXN], dist[MAXN], curedge[MAXN], pre[MAXN];    memset(dist, 0, sizeof(dist));    memset(numb, 0, sizeof(numb));    memcpy(curedge, box, sizeof(box));    numb[n] = n;    max_flow = 0;    u = st;    while(dist[st] < n){        if(u == ed){            cur_flow = INF + 1;            for(i = st; i != ed; i = edge[curedge[i]].to)                if(cur_flow > edge[curedge[i]].cap){                    neck = i;                    cur_flow = edge[curedge[i]].cap;                }            for(i = st; i != ed; i = edge[curedge[i]].to){                tmp = curedge[i];                edge[tmp].cap -= cur_flow;                edge[tmp].flow += cur_flow;                tmp = edge[tmp].pair;                edge[tmp].cap += cur_flow;                edge[tmp].flow -= cur_flow;            }            max_flow += cur_flow;            u = neck;        }        for(i = curedge[u]; i != -1; i = edge[i].next)            if(edge[i].cap > 0 && dist[u] == dist[edge[i].to]+1)                break;        if(i != -1){            curedge[u] = i;            pre[edge[i].to] = u;            u = edge[i].to;        }else{            if(0 == --numb[dist[u]])                break;            curedge[u] = box[u];            for(tmp = n,i = box[u]; i != -1; i = edge[i].next)                if(edge[i].cap > 0)                    tmp = tmp<dist[edge[i].to]?tmp:dist[edge[i].to];            dist[u] = tmp + 1;            ++numb[dist[u]];            if(u != st)                u = pre[u];        }    }    return max_flow;}int main(){    int n, m, t;    int u, v;    LL c;    scanf("%d", &t);    while (t--){        scanf("%d%d", &n, &m);        init();        int S,T;        scanf("%d%d",&S,&T);        while (m--){            scanf("%d%d%lld", &u, &v, &c);            addedge(u, v, c*255001+1);        }        LL ans = SAP(S, T, n);        printf("%lld\n",ans % 255001);    }    return 0;}




原创粉丝点击