zoj 2332 Gems

Wealthy alsomagic!
Supernatural alsomagic!
But also poor alsomagic!
Because he is now puzzled by a problem, and will go crazy if you can��t help him.

alsomagic has a lot of gems with different colors and shapes. His supernatural power provides him the ability of transforming some gems' shape and color! Now he wants to give some of gems to his darling girlfriend as gift. But a problem came with him. His girlfriend dislikes too many gems with the same color! And alsomagic, will also be disgusted with lots of gems with the same shape.

So he attempts to change some gems to solve this problem.

Input

The first line of a multiple input is an integer T, followed by T input blocks. The first line of each input block are two integers N, M (1 =< N,M <= 10) indicating alsomagic has N different shapes of gems with M different colors, then N lines of integers follow, each line contains M integers. The integer K in row R column C states that alsomagic has K number of gems with shape R and color C.

Follow this is integer D, then D lines of integers. Each line contains four integers R1��C1��R2��C2(0 =< R1, R2 < N, 0 <= C1, C2 < M) indicating one kind of transfor way, by which he can change some of shape R1, color C1 gems to shape R2, color C2, and VICE VERSA. But each way of transform can only be used ONCE!

Then N integers, the K-th integer is the number of gems with SHAPE K that alsomagic can tolerance.
Then M integers. The K-th integer is the number of gems with COLOR K that alsomagic��s girlfriend can tolerance!

Output

One line percase, with ��Yes�� if there is a way to solve this problem, otherwise ��No��.

Sample Input

2

1 3
3 2 6
1
0 1 0 2
5
1 3 2

1 3
3 2 6
1
0 1 0 2
4
1 3 2

Sample Output

Yes
No

 //

 

/*
alsomagic 想给女朋友送宝石,但是gf不希望某种颜色太多,alsomagic 也
不希望留在手中的宝石某种形状的太多,还好alsomagic 有超能力,能
让A形状B颜色的转换成C形状D颜色的,问能不能产生合适的分配。
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<map>
using namespace std;
const int N=4010;
const int M=550000;
const int inf=(1<<28);
int head[N];
struct Edge
{
    int u,v,next,w;
} edge[M];
int cnt,n,s,t;//n从0开始  0->n-1
void addedge(int u,int v,int w)
{
    edge[cnt].u=u;
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt++;
    edge[cnt].v=u;
    edge[cnt].u=v;
    edge[cnt].w=0;
    edge[cnt].next=head[v];
    head[v]=cnt++;
}
int sap()
{
    int pre[N],cur[N],dis[N],gap[N];
    int flow=0,aug=inf,u;
    bool flag;
    for(int i=0; i<n; i++)
    {
        cur[i]=head[i];
        gap[i]=dis[i]=0;
    }
    gap[s]=n;
    u=pre[s]=s;
    while(dis[s]<n)
    {
        flag=0;
        for(int &j=cur[u]; j!=-1; j=edge[j].next)
        {
            int v=edge[j].v;
            if(edge[j].w>0&&dis[u]==dis[v]+1)
            {
                flag=1;
                if(edge[j].w<aug) aug=edge[j].w;
                pre[v]=u;
                u=v;
                if(u==t)
                {
                    flow+=aug;
                    while(u!=s)
                    {
                        u=pre[u];
                        edge[cur[u]].w-=aug;
                        edge[cur[u]^1].w+=aug;
                    }
                    aug=inf;
                }
                break;
            }
        }
        if(flag) continue;
        int mindis=n;
        for(int j=head[u]; j!=-1; j=edge[j].next)
        {
            int v=edge[j].v;
            if(edge[j].w>0&&dis[v]<mindis)
            {
                mindis=dis[v];
                cur[u]=j;
            }
        }
        if((--gap[dis[u]])==0)
            break;
        gap[dis[u]=mindis+1]++;
        u=pre[u];
    }
    return flow;
}

//初始化  cnt=0;memset(head,-1,sizeof(head));

int main()
{
    int ci;scanf("%d",&ci);
    while(ci--)
    {
        int total=0;
        int m,p;scanf("%d%d",&m,&p);
        n=m*p+m+p+4;
        int leave=n-3,send=n-2;//留在手中的加上送出去的=total
        s=0,t=n-1;
        cnt=0;
        memset(head,-1,sizeof(head));
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=p;j++)
            {
                int x;scanf("%d",&x);
                total+=x;
                addedge(0,(i-1)*p+j,x);
                addedge((i-1)*p+j,m*p+i,inf);//leave
                addedge((i-1)*p+j,m*p+m+j,inf);//send
            }
        }
        int q;scanf("%d",&q);
        while(q--)
        {
            int x,y;
            scanf("%d%d",&x,&y);x++,y++;
            int frm=(x-1)*p+y;
            scanf("%d%d",&x,&y);x++,y++;
            int to=(x-1)*p+y;
            //But each way of transform can only be used ONCE
            //这句话真坑爹  我原来按照最多转换一次 addedge(frm,to,1)
            //结果wrong了 ,然后改成inf就过了  不解释
            addedge(frm,to,inf);
            addedge(to,frm,inf);
        }
        for(int i=1;i<=m;i++)
        {
            int x;scanf("%d",&x);
            addedge(m*p+i,leave,x);
        }
        for(int i=1;i<=p;i++)
        {
            int x;scanf("%d",&x);
            addedge(m*p+m+i,send,x);
        }
        addedge(leave,t,inf);
        addedge(send,t,inf);
        int tmp=sap();
        if(tmp==total) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值