解题报告
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