首页 > 其他分享 >POJ 2584 T-Shirt Gumbo(网络流之最大流)

POJ 2584 T-Shirt Gumbo(网络流之最大流)

时间:2023-04-13 21:37:22浏览次数:55  
标签:cnt 2584 cur int 流之 cap head edge Shirt


题目地址:POJ 2584

大水题。。不多说。。上代码


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
int head[200], source, sink, nv, cnt, n;
int num[200], d[200], pre[200], cur[200];
int get(char c)
{
    if(c=='S')
        return 1;
    else if(c=='M')
        return 2;
    else if(c=='L')
        return 3;
    else if(c=='X')
        return 4;
    else if(c=='T')
        return 5;
}
struct node
{
    int u, v, cap, next;
}edge[10000];
void add(int u, int v, int cap)
{
    edge[cnt].v=v;
    edge[cnt].cap=cap;
    edge[cnt].next=head[u];
    head[u]=cnt++;

    edge[cnt].v=u;
    edge[cnt].cap=0;
    edge[cnt].next=head[v];
    head[v]=cnt++;
}
void bfs()
{
    memset(d,-1,sizeof(d));
    memset(num,0,sizeof(num));
    queue<int>q;
    q.push(sink);
    d[sink]=0;
    num[0]=1;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(d[v]==-1)
            {
                d[v]=d[u]+1;
                num[d[v]]++;
                q.push(v);
            }
        }
    }
}
void isap()
{
    memcpy(cur,head,sizeof(cur));
    int flow=0, u=pre[source]=source, i;
    bfs();
    while(d[source]<nv)
    {
        if(u==sink)
        {
            int f=INF, pos;
            for(i=source;i!=sink;i=edge[cur[i]].v)
            {
                if(f>edge[cur[i]].cap)
                {
                    f=edge[cur[i]].cap;
                    pos=i;
                }
            }
            for(i=source;i!=sink;i=edge[cur[i]].v)
            {
                edge[cur[i]].cap-=f;
                edge[cur[i]^1].cap+=f;
            }
            flow+=f;
            u=pos;
        }
        for(i=cur[u];i!=-1;i=edge[i].next)
        {
            if(d[edge[i].v]+1==d[u]&&edge[i].cap)
            {
                break;
            }
        }
        if(i!=-1)
        {
            cur[u]=i;
            pre[edge[i].v]=u;
            u=edge[i].v;
        }
        else
        {
            if(--num[d[u]]==0) break;
            int mind=nv;
            for(i=head[u];i!=-1;i=edge[i].next)
            {
                if(mind>d[edge[i].v]&&edge[i].cap)
                {
                    mind=d[edge[i].v];
                    cur[u]=i;
                }
            }
            d[u]=mind+1;
            num[d[u]]++;
            u=pre[u];
        }
    }
    if(flow==n)
        printf("T-shirts rock!\n");
    else
        printf("I'd rather not wear a shirt anyway...\n");
}
int main()
{
    char s[20], s1[20];
    int i, j, m, x, y, z;
    while(scanf("%s",s)!=EOF)
    {
        if(s[0]=='E')
        {
            break;
        }
        scanf("%d",&n);
        source=0;
        sink=n+5+1;
        nv=sink+1;
        memset(head,-1,sizeof(head));
        cnt=0;
        for(i=1; i<=n; i++)
        {
            scanf("%s",s1);
            add(source,i,1);
            x=get(s1[0]);
            y=get(s1[1]);
            for(j=x;j<=y;j++)
            {
                add(i,j+n,1);
            }
        }
        for(i=1;i<=5;i++)
        {
            scanf("%d",&z);
            add(n+i,sink,z);
        }
        getchar();
        gets(s);
        isap();
    }
    return 0;
}



标签:cnt,2584,cur,int,流之,cap,head,edge,Shirt
From: https://blog.51cto.com/u_16070138/6188412

相关文章

  • ZOJ 3348 Schedule(map运用+网络流之最大流)(竞赛问题升级版)
    题目地址:ZOJ3348仍然是一道竞赛问题的网络流问题,但是这道题再用上次的竞赛建图方法就不行了,5000场比赛,明显会超时,于是需要换种建图思路了。上一道经典竞赛问题戳这里上一道的胜负转换是利用专门给比赛建一个点,通过对比赛双方的流向来控制胜负关系,这里的建图方法更加巧妙(膜拜想出这......
  • [leetcode]2584. 分割数组使乘积互质 (质因数分解)
    题目链接:分割数组使乘积互质思路:指针循环从\([0,len-1)\)每次动态维护指针左边所有数与指针右边所有数质因数交集,第一次交集为0的地方为答案。首先将打表\(10^6\)之内的质......
  • AD82584F兼容替代TAS5707/TAS5711,用于智能音箱语音辨识的2x25W立体声数字音频功放,带I2
    AD82584F是一种数字音频放大器,能够将一对8Ω负载扬声器驱动到25W(BTL)和将4Ω负载扬声器驱动到50W(PBTL),播放音乐不需要外部散热器或风扇。AD82584F提供先进的音频处理功能,如音......
  • [CF702F] T-shirts
    T-Shirts题目描述Thebigconsignmentoft-shirtsgoesonsaleintheshopbeforethebeginningofthespring.Inall$n$typesoft-shirtsgoonsale.Thet......
  • CF - 807B. T-Shirt Hunt (贪心+模拟)
    DescriptionNotsolongagotheCodecraft-17contestwasheldonCodeforces.Thetop25participants,andadditionallyrandom25participantsoutofthosewhogo......
  • 网络流之广义切糕模型
    问题有\(n\)个整数变量\(x_i\)。\(x_i\)可以取\([1,m]\),取\(j\)需要\(a_{i,j}\)的代价。有若干个约束,形如\(x_{u_i}\lex_{v_i}+w_i\)。给变量赋值,最小化总......
  • IO流之节点流和处理流
    基本介绍节点流可以从一个特定的数据源读写数据,如FileReader、FileWriter处理流(也叫包装流)是“连接”在已存在的流(节点流或处理流)之上,为程序提供更为强大的读写功能......
  • IO流之FileReader和FileWriter
    IO流之FileReader和FileWriter的介绍FileReader和FileWriter是字符流,即按照字符来操作ioFileReader类图FileReader相关方法:newFileReader(File/String)re......
  • IO流之FileInputStream
    IO流之FileInputStreamInputStream:字节输入流InputStream抽象类是所有类字节输入流的超类InputStream常用的子类FileInputStream:文件输入流BufferedInputStream:缓......
  • Stream流之List、Integer[]、int[]相互转化
    一.int[]转化1.1、int[]转List<Integer>publicstaticvoidmain(String[]args){int[]arr={1,2,3,4,5};List<Integer>list=A......