Solution report
Http://blog.csdn.net/juncoder/article/details/38235609
Question Portal
Question:
N soldiers and M weapons. The weight range of each soldier is [minw, maxw]
Ideas:
I thought it was okay to use a bipartite graph. (The data range is incorrect.) greedy seems to be okay.
SCF indicates that network streams can be scaled down.
Drawing Method: the source and soldiers are connected to a line. Each soldier is connected to the weight of the [] weapon, and the [] is connected to the sink. The capacity is the quantity of weapon I.
#include <queue>#include <cstdio>#include <cstring>#include <iostream>using namespace std;#define inf 99999999int 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 areN(1 <= n <= 2500) unarmed soldier in your kingdom and there areM(1 <= m <= 40000) weapons in your arsenal. Each weapon has a weightW(1 <= W <= 1000), and for soldier I, he can only arm the weapon whose weight isMinwiAndMaxwi(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 31 53 75 104892 25 1010 20421
Sample output
20