第一次写EK,写了一个晚上没有什么大的进展,仔仔细细研究了一下论文,自己想了BFS找增广路的过程,写了一个邻接矩阵版的EK交上去,居然只跑了30多毫秒,不知道什么情况。。。。 我的代码: #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include <queue> using namespace std; const int oo=0x3f3f3f3f; int get() { char ch; int flag = 0, tmp = 0; for (ch = getchar(); ch < 48 || ch > 57; ch = getchar()) if (ch == int('-')) break; if (ch == int('-')) flag = 1; else tmp = int(ch) - 48; for (ch = getchar(); 48 <= ch && ch <= 57; ch = getchar()) tmp = tmp * 10 + int(ch) - 48; return (flag) ? -tmp : tmp; } int c[20][20],pre[20],low[20]; int st,ed,n; int bfs(){ bool vis[20]={0}; memset(low,0,sizeof(low)); queue<int> q; low[st]=oo; q.push(st); vis[st]=true; while(!q.empty()){ int u=q.front(); q.pop(); for(int v=1;v<=n;v++){ if(!vis[v]&&c[u][v]){ vis[v]=true;pre[v]=u;q.push(v); low[v]=min(c[u][v],low[u]); } } } if(low[st]){ int u=ed; do{ c[pre[u]][u]-=low[ed]; c[u][pre[u]]+=low[ed]; u=pre[u]; }while(u!=st); } return low[ed]; } int maxflow(){ int ret=0; do{ ret+=bfs(); }while(low[ed]); return ret; } int main(){ int t,m; int cnt=0; t=get(); while(t--){ cnt++; n=get(); m=get(); memset(c,0,sizeof(c)); for(int i=0;i<m;i++){ int u=get(); int v=get(); int w=get(); c[u][v]+=w; } st=1; ed=n; printf("Case %d: %d/n",cnt,maxflow()); } return 0; } 注意BFS过程,low作为全局变量。。。