• POJ2584 TShirt Gumbo 最大流


    简单构图,构造一个S流向 S,M,L,X,T,赛事 T1,T2,T2...TN流向汇点T。

    代码如下:

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <queue>
    #define INF 0x3f3f3f3f
    #define MAXN 25
    using namespace std;

    int N, cap[40][40], flow[40][40], p[40], c[40], S = 0, T = 38;

    int hash[130], maxflow;

    void init()
    {
    maxflow = 0;
    memset(cap, 0, sizeof (cap));
    memset(flow, 0, sizeof (flow));
    memset(p, 0xff, sizeof (p));
    }

    inline int min(int x, int y)
    {
    return x < y ? x : y;
    }

    void bfs()
    {
    int finish = 0, pos;
    while (!finish) {
    memset(c, 0, sizeof (c));
    c[S] = INF;
    queue<int>q;
    q.push(S);
    while (!q.empty()) {
    if (c[T]) {
    break;
    }
    pos = q.front();
    q.pop();
    for (int i = 1; i <= T; ++i) {
    if (!c[i] && cap[pos][i] > flow[pos][i]) {
    c[i] = min(c[pos], cap[pos][i] - flow[pos][i]);
    p[i] = pos;
    q.push(i);
    }
    }
    }
    maxflow += c[T];
    if (!c[T]) {
    finish = 1;
    continue;
    }
    pos = T;
    while (pos != 0) {
    flow[p[pos]][pos] += c[T];
    flow[pos][p[pos]] -= c[T];
    pos = p[pos];
    }
    }
    }

    int main()
    {
    char op[20], detail[5];
    int x;
    hash['S'] = 1, hash['M'] = 2, hash['L'] = 3, hash['X'] = 4, hash['T'] = 5;
    while (scanf("%s", op), strcmp(op, "ENDOFINPUT")) {
    init();
    scanf("%d", &N);
    for (int i = 1; i <= N; ++i) {
    scanf("%s", detail);
    for (int j = hash[detail[0]]; j <= hash[detail[1]]; ++j) {
    cap[j][5+i] = INF; // 反向边已经清零
    cap[5+i][T] = 1;
    }
    }
    for (int i = 1; i <= 5; ++i) {
    scanf("%d", &x);
    cap[S][i] = x;
    }
    scanf("%s", op);
    bfs();
    if (maxflow == N) {
    puts("T-shirts rock!");
    }
    else {
    puts("I'd rather not wear a shirt anyway...");
    }
    }
    return 0;
    }



  • 相关阅读:
    【重构学习】12 重构学习感想
    【重构学习】11 大型重构
    【重构学习】10 继承关系的重构
    【重构学习】09 函数调用的重构
    【重构学习】08 条件表达式的重构
    嵊州D4T1 翻车 rollover 真的翻车了
    计算圆内格点数
    嵊州D3T2 福尔贝斯太太的快乐夏日 summer
    嵊州D3T3 light
    嵊州D3T1 山魔 烙饼问题
  • 原文地址:https://www.cnblogs.com/Lyush/p/2433329.html
Copyright © 2020-2023  润新知