ZOJ3508_The War(网络流最大流)

这是一篇关于ZOJ3508题目的解题报告,题目要求确定在给定士兵和武器重量限制下,能武装的最大士兵数。通过使用网络流的方法,可以解决这个问题。文章介绍了如何构建网络流图,包括源点与士兵、士兵与武器重量、武器重量与汇点之间的连接,以及各边的容量设定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

解题报告

http://blog.youkuaiyun.com/juncoder/article/details/38235609

题目传送门

题意:

N个士兵,M个武器,每个士兵能接受的武器重量范围是[minw,maxw]

思路:

本来以为二分图可以的,(看错数据范围了,,,)贪心好像可以。

scf说网络流可以缩点。

建图方式:源点和士兵连一条线,每个士兵与[1,1000]的武器重量连边,[1,1000]与汇点连线,容量是武器i的数量

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define inf 99999999
int w,n,m,head[4010],l[4010],vis[4010],d[4010],cnt;
struct node1
{
    int maxx,minn;
}we[4000];
struct node
{
    int v,w,next;
}edge[2510000];
void add(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 bfs()
{
    queue<int>Q;
    Q.push(0);
    memset(l,-1,sizeof(l));
    l[0]=0;
    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(l[v]==-1&&edge[i].w)
            {
                l[v]=l[u]+1;
                Q.push(v);
            }
        }
    }
    if(l[n+1000+1]>0)
    return 1;
    else return 0;
}
int dfs(int x,int f)
{
    int i,a;
    if(x==n+1000+1)return f;
    for(i=head[x];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(edge[i].w&&l[v]==l[x]+1&&(a=dfs(v,min(f,edge[i].w))))
        {
            edge[i].w-=a;
            edge[i^1].w+=a;
            return a;
        }
    }
    l[x]=-1;
    return 0;
}
int main()
{
    int i,j,s,e;
    while(~scanf("%d%d",&n,&m))
    {
        cnt=0;
        memset(d,0,sizeof(d));
        memset(edge,0,sizeof(edge));
        memset(head,-1,sizeof(head));
        for(i=1;i<=n;i++)
        {
            add(0,i,1);
            scanf("%d%d",&we[i].minn,&we[i].maxx);
        }
        for(i=1;i<=m;i++)
        {
            scanf("%d",&w);
            d[w]++;
        }
        for(i=1;i<=1000;i++)
        {
            add(i+n,n+1000+1,d[i]);
            for(j=1;j<=n;j++)
            {
                if(i<=we[j].maxx&&i>=we[j].minn)
                {
                    add(j,i+n,1);
                }
            }
        }
        int ans=0,a;
        while(bfs())
            while(a=dfs(0,inf))
            ans+=a;
        printf("%d\n",ans);
    }
}

The War

Time Limit: 2 Seconds       Memory Limit: 65536 KB

A war had broken out because a sheep from your kingdom ate some grasses which belong to your neighboring kingdom. The counselor of your kingdom had to get prepared for this war. There are N (1 <= N <= 2500) unarmed soldier in your kingdom and there are M (1 <= M <= 40000) weapons in your arsenal. Each weapon has a weight W (1 <= W <= 1000), and for soldier i, he can only arm the weapon whose weight is between minWi and maxWi ( 1 <= minWi <= maxWi <= 1000). More armed soldier means higher success rate of this war, so the counselor wants to know the maximal armed soldier he can get, can you help him to win this war?

Input

There multiple test cases. The first line of each case are two integers N, M. Then the following N lines, each line contain two integers minWi, maxWi for each soldier. Next M lines, each line contain one integer W represents the weight of each weapon.

Output

For each case, output one integer represents the maximal number of armed soldier you can get.

Sample Input

3 3
1 5
3 7
5 10
4
8
9
2 2
5 10
10 20
4
21

Sample Output

2
0


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值