#include<stdio.h> #include<memory.h> #include<iostream> using namespace std ; /* Hint: gra 数组每次使用需要重新初始化 memset(gra,0,sizeof(gra)); */ const int MAXPT = 501 ; const int MAXNUM = 1000000 ; class MNF_EK { public: int gra[MAXPT][MAXPT]; // 存储图 int n ; // 表示图的顶点个数 - 1 int s,t; // s 为源点 , t 为 汇点 private: int que[10000],rt,ft; int check[MAXPT],pre[MAXPT]; int path() { int u,v; rt=ft=0; memset(check,0,sizeof(check)); check[s]=1; pre[s]=-1; que[rt++]=s; while(rt!=ft) { u=que[ft++]; for(v=0;v<=n;v++) // n 是节点个数减1 if(gra[u][v] && !check[v]) { check[v]=1; pre[v]=u; que[rt++]=v; if(v==t) return 1; } } return 0; } public: MNF_EK(){} ~MNF_EK(){} int EK() { int max=0,min,v; while(path()) { for(v=t,min=MAXNUM;v!=s;v=pre[v]) if(gra[pre[v]][v]<min) min=gra[pre[v]][v]; for(v=t;v!=s;v=pre[v]) { gra[pre[v]][v]-=min; gra[v][pre[v]]+=min; } max+=min; } return max; } } ; int main() { MNF_EK ob ; int n,np,nc,m; // np power station nc cosumer m the edge int u,v,z; int i,j,k; char tch ; while(cin>>n>>np>>nc>>m) { memset( ob.gra , 0 , sizeof(ob.gra ) ); ob.s = 0 ; ob.t = n + 1 ; ob.n = n + 1 ; //printf("%d %d %d %d ",n,np,nc,m); for(i=0;i<m;i++) { tch = ' ' ; while(scanf("%c",&tch)!=EOF&&tch!='(') ; scanf("%d%c%d",&u,&tch,&v); tch = ' '; while(scanf("%c",&tch)!=EOF&&tch!=')') ; scanf("%d",&z ); ob.gra[u+1][v+1] = z; } for(i=0;i<np;i++) // source -> p { tch = ' ' ; while(scanf("%c",&tch)!=EOF&&tch!='(') ; scanf("%d",&u); tch = ' '; while(scanf("%c",&tch)!=EOF&&tch!=')') ; scanf("%d",&z ); ob.gra[ob.s][u+1] = z; //printf("(%d)%d " , u, z); } for(i=0;i<nc;i++) // c -> sink { tch = ' ' ; while(scanf("%c",&tch)!=EOF&&tch!='(') ; scanf("%d",&u); tch = ' '; while(scanf("%c",&tch)!=EOF&&tch!=')') ; scanf("%d",&z ); ob.gra[u+1][ob.t] = z; //printf("(%d)%d " , u, z); } cout<<ob.EK()<<endl; } return 0 ; } 不多说,只是用模板A题,但是一开始准备用SAP的模板,但是还没有看明白,所以先用EK吧