题意:有许多party在同一天举行,每个party有开始时间与截止时间,爱玩的女主要在一天中参加尽量多的party,而且每个party待的时长不小于半个小时,求女主最多能够参加的party数量。
1 #include<stdio.h> 2 #include<string.h> 3 #include<queue> 4 using namespace std; 5 #define captype __int32 6 const int N = 4005; 7 8 9 captype cap[N][N],f[N][N],rest[N]; 10 int sNode,eNode,pre[N]; 11 12 13 void init(){ 14 memset(f,0,sizeof(f)); 15 memset(cap,0,sizeof(cap)); 16 } 17 18 19 bool searchPath(int n){//找一条增广路 20 bool vist[N]={0}; 21 queue<int>q; 22 int u,v; 23 u=sNode; vist[u]=1; 24 pre[u]=u; rest[u]=1<<30; 25 q.push(u); 26 while(!q.empty()){ 27 u=q.front(); q.pop(); 28 for(v=1; v<=n; v++) 29 if(!vist[v]&&cap[u][v]-f[u][v]>0) 30 { 31 vist[v]=1; pre[v]=u; 32 if(cap[u][v]-f[u][v]>rest[u]) 33 rest[v]=rest[u]; 34 else 35 rest[v]=cap[u][v]-f[u][v]; 36 if(v==eNode) return true; 37 q.push(v); 38 } 39 } 40 return false; 41 } 42 captype maxflow(int s,int t,int n){ 43 captype ans=0; 44 sNode=s; eNode=t; 45 while(searchPath(n)){ 46 ans+=rest[eNode]; 47 int v=eNode; 48 while(v!=sNode){ 49 int u=pre[v]; 50 f[u][v]+=rest[eNode]; 51 f[v][u]-=rest[eNode];//给一个回流的机会 52 v=u; 53 } 54 } 55 return ans; 56 } 57 int main(){ 58 freopen("C:/Users/fuliujun/Desktop/input.txt","r",stdin); 59 freopen("C:/Users/fuliujun/Desktop/output.txt","w",stdout); 60 int a,b,m,n,a1[100],b1[100],n1,sum=0,day=1; 61 while(scanf("%d",&n)!=EOF){ 62 for(int i=0;i<n;i++){ 63 scanf("%d%d",&a1[i],&b1[i]); 64 sum+=(b1[i]-a1[i])*2; 65 } 66 n1=34+n; 67 m=sum+32+n; 68 init(); 69 for(int i=0;i<32;i++) 70 cap[1][i+2]=1; 71 for(int i=0;i<n;i++) 72 cap[34+i][n1]=1; 73 for(int i=0;i<n;i++){ 74 int j=(b1[i]-a1[i])*2; 75 for(int z=1;z<=j;z++) 76 { int tem=a1[i]*2-15; 77 cap[tem+z][34+i]=1; 78 } 79 } 80 if(n!=0){ 81 printf("On day %d Emma can attend as many as %d parties.\n",day,maxflow(1,n1,n1)); 82 } 83 day++; 84 } 85 fclose(stdin); 86 fclose(stdout); 87 return 0; 88 }
时间: 2024-11-02 01:49:08