soj 3254: Rain and Fgj 最小割

Description

There are N citys in RAIN's country.And there are M directed road between the N citys.Now she is in the city 0.And she want to go to city N-1.But the bad FGJ donot want RAIN go to city N-1.He can destroy some city to make it.RAIN cannot go to the city which be destroyed.Eeah city has a weight Wi to destroy.FGJ cannot destroy the city 0.Can you tell FGJ the minimal totle weight he must destroy?

Input

The first line of the input is the number of test cases.For each case there is M+1 lines.The first line contains two integers N M.The second line contains N-1 integers Wi ( 1 <= i < N ).The next M lines each contains two integers P and Q,represent there is a road from city P to city Q.( 2 <= N <= 1000 , 0 <= M <= 10^5 , 0 <= P,Q < N , 0 <= Wi <= 10^5 )There is a blank line before each test case.

Output

For each test case output the answer on a line:The minimal totle weight FGJ must destroy.

Sample Input

52 01002 11000 12 11001 02 51000 10 11 00 01 13 21 1000 11 2

Sample Output

010001001

 //

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=55010;
const int M=950000;
const int inf=(1<<28);
int head[N];
struct Edge
{
    int 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].v=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt++;
    edge[cnt].v=u;
    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 m,p;scanf("%d%d",&m,&p);
        cnt=0;
        memset(head,-1,sizeof(head));
        n=m*2+2;
        s=0,t=n-1;
        addedge(0,1,inf);
        addedge(1,1+m,inf);
        addedge(m*2,t,inf);
        for(int i=2;i<=m;i++)
        {
            int x;scanf("%d",&x);
            addedge(i,i+m,x);
        }
        while(p--)
        {
            int u,v;scanf("%d%d",&u,&v);u++,v++;
            addedge(u+m,v,inf);
        }
        int mc=sap();
        printf("%d\n",mc);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值