zoukankan      html  css  js  c++  java
  • ZOJ3508 The War 贪心,最大流

    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 
    3 3
    1 5
    3 7
    5 10
    4
    8
    9
    2 2
    5 10
    10 20
    4
    21
    Sample Output
    2
    0

    常规解法是贪心,但是在复习最大流的写法,因此用sap来写的。思路是很好想的

    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<memory.h>
    #include<cmath>
    using namespace std;
    const int M=2510000;
    int s,t,cnt;;
    int a[4000],b[4000],num[1010];;
    const int inf=1000000;
    int vd[4001],dis[4001],ans,m,n;
    int Laxt[M],Next[M],Val[M],To[M];
    void _update()
    {
        memset(vd,0,sizeof(vd));
        memset(dis,0,sizeof(dis));
        memset(num,0,sizeof(num));
        memset(Laxt,0,sizeof(Laxt));
        ans=0; cnt=2;
        s=0; t=1000+n+1; 
    }
    void _add(int u,int v,int w)
    {
        Next[cnt]=Laxt[u];
        Laxt[u]=cnt;
        To[cnt]=v;
        Val[cnt++]=w;
        
        Next[cnt]=Laxt[v];
        Laxt[v]=cnt;
        To[cnt]=u;
        Val[cnt++]=0;
    }
    int dfs(int u,int flow)
    {    
          int temp,delta;
          if(u==t)return flow;
          delta=0;
          for(int i=Laxt[u];i>0;i=Next[i]) 
             if(Val[i]>0 && dis[u]==dis[To[i]]+1) 
             {
                 temp=dfs(To[i],min(flow-delta,Val[i]));
                 Val[i]-=temp;
                 Val[i^1]+=temp;
    
                 delta+=temp;
                 if(delta==flow||dis[s]>t) return delta;
             }
          vd[dis[u]]--;
          if(vd[dis[u]]==0)dis[s]=t+1;
          dis[u]++;
          vd[dis[u]]++;
          return delta;
    }
    void _work()
    { 
          int i,j,x,y;
          for(i=1;i<=n;i++) scanf("%d%d",&a[i],&b[i]);
          for(i=1;i<=m;i++) {
             scanf("%d",&x);
             num[x]++;
          }
          for(i=1;i<=1000;i++)
           if(num[i]>0)
            _add(s,i,num[i]);
          for(i=1;i<=1000;i++) 
           for(j=1;j<=n;j++){
              if(i>=a[j]&&i<=b[j])
              _add(i,1000+j,1);
           }
           for(j=1;j<=n;j++)
            _add(1000+j,t,1);
          int temp=0;
         while(dis[s]<t+1)
         {
             int flow=dfs(s,inf);
             ans+=flow;
         }
         printf("%d
    ",ans);
    }
    int main()
    {     
         while(~scanf("%d%d",&n,&m))
         { 
             _update();
             _work();
        }
         return 0;
    }
  • 相关阅读:
    mybatis-plus学习
    代码规范系列
    Error:Abnormal build process termination:
    SpringBoot常用注解
    git深度学习
    spring中创建bean的方式
    jenkis构建“ERROR: Error fetching remote repo ‘origin’“
    tensor2tensor安装bug修复
    Swin-Transformer代码工程进行物体检测
    Linux常用命令
  • 原文地址:https://www.cnblogs.com/hua-dong/p/7603950.html
Copyright © 2011-2022 走看看