zoj 3500 The War

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 areN (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
 
分析:武器有40000件,但只有1000种,可以压缩后建图
代码:
#include<cstdio>
#include<cstring>
const int N=4000;
const int M=2500000;
const int INF=0x7fffffff;
int tol,ans,s,t;
int head[N],arc[N],pre[N],dis[N],gap[N];
struct node
{
	int y,f,nxt;
}edge[M];
void add(int x,int y,int f)
{
	edge[tol].y=y;
	edge[tol].f=f;
	edge[tol].nxt=head[x];
	head[x]=tol++;
	edge[tol].y=x;
	edge[tol].f=0;
	edge[tol].nxt=head[y];
	head[y]=tol++;
}
void bfs(int n)
{
	int Q[N],front=0,rear=0,i,u,v;
	bool vis[N];
	memcpy(arc,head,sizeof(head));
	memset(dis,0,sizeof(dis));
	memset(vis,0,sizeof(vis));
	memset(gap,0,sizeof(gap));
	for(i=0;i<n;i++)dis[i]=n;
	vis[t]=1,dis[t]=0,Q[rear++]=t;
	while(front<rear)
	{
		u=Q[front++];
		for(i=head[u];i!=-1;i=edge[i].nxt)
		{
			v=edge[i].y;
			if(!vis[v]&&edge[i^1].f)
			{
				vis[v]=1;
				Q[rear++]=v;
				dis[v]=dis[u]+1;
				gap[dis[v]]++;
			}
		}
	}
}
void sap(int n)
{
	bfs(n);
	int arg=INF,u=pre[s]=s;
	while(dis[s]<n)
	{
L:
		for(int& i=arc[u];i!=-1;i=edge[i].nxt)
		{
			int v=edge[i].y;
			if(edge[i].f&&dis[u]==dis[v]+1)
			{
				if(arg>edge[i].f)arg=edge[i].f;
				pre[v]=u;u=v;
				if(v==t)
				{
					ans+=arg;
					for(u=pre[u];v!=s;v=u,u=pre[u])
					{
						edge[arc[u]].f-=arg;
						edge[arc[u]^1].f+=arg;
					}
					arg=INF;
				}
				goto L;
			}
		}
		int min=n;
		for(int j=head[u];j!=-1;j=edge[j].nxt)
		{
			int v=edge[j].y;
			if(edge[j].f&&min>dis[v])
			{
				arc[u]=j;
				min=dis[v];
			}
		}
		if(--gap[dis[u]]==0)break;
		dis[u]=min+1;
		gap[dis[u]]++;
		u=pre[u];
	}
}
struct Soldier{
	int minw,maxw;
}so[2502];
int cw[1001];
int main()
{
	int n,m,i,j,w,mw;
	while(~scanf("%d%d",&n,&m)){
		ans=0,tol=0;
		memset(head,-1,sizeof(head));
		for(i=1;i<=n;i++)scanf("%d%d",&so[i].minw,&so[i].maxw);
		memset(cw,0,sizeof(cw));mw=0;
		while(m--){scanf("%d",&w);cw[w]++;if(w>mw)mw=w;}
		s=0,t=n+mw+1;
		for(i=1;i<=n;i++){
			add(s,i,1);
			for(j=so[i].minw;j<=so[i].maxw;j++){
				if(cw[j])add(i,n+j,INF);
			}
		}
		for(i=1;i<=mw;i++)if(cw[i])add(n+i,t,cw[i]);
		n=t+1;
		sap(n);
		printf("%d\n",ans);
	}
	return 0;
}
分析:另外此题贪心可解
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int w[1001];
struct node{
	int minw,maxw;
}a[2500];
bool cmp(node a,node b){
	if(a.maxw==b.maxw)return a.minw>b.minw;
	return a.maxw<b.maxw;
}
int main(){
	int n,m,i,j,x,f,ans;
	while(~scanf("%d%d",&n,&m)){
		for(i=0;i<n;i++)scanf("%d%d",&a[i].minw,&a[i].maxw);
		sort(a,a+n,cmp);
		memset(w,0,sizeof(w));
		for(i=0;i<m;i++)scanf("%d",&x),w[x]++;
		ans=0;
		for(i=0;i<n;i++){
			f=0;
			for(j=a[i].minw;j<=a[i].maxw;j++)if(w[j]){w[j]--;f=1;break;}
			ans+=f;
		}
		printf("%d\n",ans);
	}
	return 0;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值