ZOJ3508_The War(网络流最大流)

解题报告

http://blog.csdn.net/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

ZOJ3508_The War(网络流最大流)

时间: 2024-10-22 07:06:17

ZOJ3508_The War(网络流最大流)的相关文章

HDU 4309 Seikimatsu Occult Tonneru(网络流-最大流)

Seikimatsu Occult Tonneru Problem Description During the world war, to avoid the upcoming Carpet-bombing from The Third Reich, people in Heaven Empire went to Great Tunnels for sheltering. There are N cities in Heaven Empire, where people live, with

POJ训练计划3422_Kaka&#39;s Matrix Travels(网络流/费用流)

解题报告 题目传送门 题意: 从n×n的矩阵的左上角走到右下角,每次只能向右和向下走,走到一个格子上加上格子的数,可以走k次.问最大的和是多少. 思路: 建图:每个格子掰成两个点,分别叫"出点","入点", 入点到出点间连一个容量1,费用为格子数的边,以及一个容量∞,费用0的边. 同时,一个格子的"出点"向它右.下的格子的"入点"连边,容量∞,费用0. 源点向(0,0)的入点连一个容量K的边,(N-1,N-1)的出点向汇点连一

POJ训练计划2516_Minimum Cost(网络流/费用流)

解题报告 题意: 有n个商店,m个提供商,k种商品</span> n*k的矩阵,表示每个商店需要每个商品的数目: m*k矩阵,表示每个提供商拥有每个商品的个数 然后对于每个物品k,都有n*m的矩阵 i行j列表示 从j提供商向i商店运送一个k商品的代价是多少 判断所有的仓库能否满足所有客户的需求,如果可以,求出最少的运输总费用 思路: 建图的题,不能直接把所有信息建成图,因为n和m跟k都有关系,如果那样子建图的话,就要把k种拆成m类,每个仓库连向该仓库的第k种,然后再和n连线,有费用, 不过这样

POJ 1459 Power Network(网络流 最大流 多起点,多汇点)

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 22987   Accepted: 12039 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied

POJ 1273 Drainage Ditches(网络流 最大流)

Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55893   Accepted: 21449 Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by

hdu 4289 Control(网络流 最大流+拆点)(模板)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4289 Control Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1545    Accepted Submission(s): 677 Problem Description You, the head of Department o

POJ 2455 Secret Milking Machine(搜索-二分,网络流-最大流)

Secret Milking Machine Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9658   Accepted: 2859 Description Farmer John is constructing a new milking machine and wishes to keep it secret as long as possible. He has hidden in it deep within

【bzoj1822】[JSOI2010]Frozen Nova 冷冻波 计算几何+二分+网络流最大流

题目描述 WJJ喜欢“魔兽争霸”这个游戏.在游戏中,巫妖是一种强大的英雄,它的技能Frozen Nova每次可以杀死一个小精灵.我们认为,巫妖和小精灵都可以看成是平面上的点. 当巫妖和小精灵之间的直线距离不超过R,且巫妖看到小精灵的视线没有被树木阻挡(也就是说,巫妖和小精灵的连线与任何树木都没有公共点)的话,巫妖就可以瞬间杀灭一个小精灵. 在森林里有N个巫妖,每个巫妖释放Frozen Nova之后,都需要等待一段时间,才能再次施放.不同的巫妖有不同的等待时间和施法范围,但相同的是,每次施放都可以

[笔记] 网络流-最大流 POJ-1273\HDU-4240

[1] POJ-1273 题目:http://poj.org/problem?id=1273 最直接的最大流问题,用了Ford-Fulkerson方法,DFS随机搜索增广路. 算法原理参考:http://blog.csdn.net/smartxxyx/article/details/9293665 /************************ POJ-1273* Ford-Fulkerson***********************/#include <stdio.h> #inclu