HDU-4975-a simple Gaussian elimination problem. (maximum Stream)

Source: Internet
Author: User

Question: Give the rows and columns of a Number Matrix with N rows and M columns, and the size of each element cannot exceed 9. Ask if such a matrix exists, whether the unique N (1 ≤ n ≤ 500), m (1 ≤ m ≤ 500 ).

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4975

--> Method such as: http://blog.csdn.net/scnu_jiechao/article/details/40658221

Do HDU-4888 first. When I do this again, I feel that this question is sb. After I submit the code, I will be sb. Why ??

The time for this question is relatively strict .. You can optimize the code of 4888 in the judgment ring, because the row-to-column edge in the original selected graph will be judged (n-1) During the judgment ring) * (m-1) + 1. If you create a new graph for the residual network, you will only be judged once when creating the graph!

Note: If the input fails, the Re is displayed. It is assumed that there are negative numbers in the input data ..

#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using std::min;using std::queue;const int MAXN = 500 * 2 + 10;const int MAXM = 500 * 500 + 2 * MAXN;const int INF = 0x3f3f3f3f;struct EDGE{    int from;    int to;    int cap;    int flow;    int nxt;} edge[MAXM << 1];int N, M, kase;int sum;int S, T;int hed[MAXN], ecnt;int cur[MAXN], h[MAXN];bool impossible, bUnique;void Init(){    impossible = false;    bUnique = true;    ecnt = 0;    memset(hed, -1, sizeof(hed));}void AddEdge(int u, int v, int cap){    edge[ecnt].from = u;    edge[ecnt].to = v;    edge[ecnt].cap = cap;    edge[ecnt].flow = 0;    edge[ecnt].nxt = hed[u];    hed[u] = ecnt++;    edge[ecnt].from = v;    edge[ecnt].to = u;    edge[ecnt].cap = 0;    edge[ecnt].flow = 0;    edge[ecnt].nxt = hed[v];    hed[v] = ecnt++;}bool Bfs(){    memset(h, -1, sizeof(h));    queue<int> qu;    qu.push(S);    h[S] = 0;    while (!qu.empty())    {        int u = qu.front();        qu.pop();        for (int e = hed[u]; e != -1; e = edge[e].nxt)        {            int v = edge[e].to;            if (h[v] == -1 && edge[e].cap > edge[e].flow)            {                h[v] = h[u] + 1;                qu.push(v);            }        }    }    return h[T] != -1;}int Dfs(int u, int cap){    if (u == T || cap == 0) return cap;    int flow = 0, subFlow;    for (int e = cur[u]; e != -1; e = edge[e].nxt)    {        cur[u] = e;        int v = edge[e].to;        if (h[v] == h[u] + 1 && (subFlow = Dfs(v, min(cap, edge[e].cap - edge[e].flow))) > 0)        {            flow += subFlow;            edge[e].flow += subFlow;            edge[e ^ 1].flow -= subFlow;            cap -= subFlow;            if (cap == 0) break;        }    }    return flow;}int Dinic(){    int maxFlow = 0;    while (Bfs())    {        memcpy(cur, hed, sizeof(hed));        maxFlow += Dfs(S, INF);    }    return maxFlow;}int ReadInt(){    int ret = 0;    char ch;    while ((ch = getchar()) && ch >= '0' && ch <= '9')    {        ret = ret * 10 + ch - '0';    }    return ret;}void Read(){    int r, c;    int rsum = 0, csum = 0;    scanf("%d%d", &N, &M);    S = 0;    T = N + M + 1;    getchar();    for (int i = 1; i <= N; ++i)    {//        r = ReadInt();        scanf("%d", &r);        rsum += r;        AddEdge(S, i, r);    }    for (int i = 1; i <= M; ++i)    {//        c = ReadInt();        scanf("%d", &c);        csum += c;        AddEdge(i + N, T, c);    }    if (rsum != csum)    {        impossible = true;        return;    }    sum = rsum;    for (int i = 1; i <= N; ++i)    {        for (int j = M; j >= 1; --j)        {            AddEdge(i, j + N, 9);        }    }}void CheckPossible(){    if (impossible) return;    if (Dinic() != sum)    {        impossible = true;    }}void AddEdge(int u, int v){    edge[ecnt].to = v;    edge[ecnt].nxt = hed[u];    hed[u] = ecnt++;}void ReBuild(){    memset(hed, -1, sizeof(hed));    int total = ecnt;    ecnt = 0;    for (int e = 0; e < total; ++e)    {        if (edge[e].cap > edge[e].flow)        {            AddEdge(edge[e].from, edge[e].to);        }    }}bool vis[MAXN];bool CheckCircle(int x, int f){    vis[x] = true;    for (int e = hed[x]; e != -1; e = edge[e].nxt)    {        int v = edge[e].to;        if (v != f)        {            if (vis[v]) return true;            if (CheckCircle(v, x)) return true;        }    }    vis[x] = false;    return false;}void CheckUnique(){    if (impossible) return;    memset(vis, 0, sizeof(vis));    for (int i = 1; i <= N; ++i)    {        if (CheckCircle(i, -1))        {            bUnique = false;            return;        }    }}void Output(){    printf("Case #%d: ", ++kase);    if (impossible)    {        puts("So naive!");    }    else if (bUnique)    {        puts("So simple!");    }    else    {        puts("So young!");    }}int main(){    int T;    scanf("%d", &T);    while (T--)    {        Init();        Read();        CheckPossible();        ReBuild();        CheckUnique();        Output();    }    return 0;}


HDU-4975-a simple Gaussian elimination problem. (maximum Stream)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.