用Bellman Ford找增广路进行增广
代码着实很烂,自己也还不太明白,先贴上来再说.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | #include <iostream> #include <queue> #define msize 1024 #define inf 1<<20 using namespace std; int n,m,s,t; //n个顶点,m条边,源点s,汇点t int graph[msize][msize],flow[msize][msize],cost[msize][msize]; int pre[msize],dis[msize]; int deltaCost() //从s到t的最小费用 { int res=0,i,j; bool quit; while (1) { //Bellman Ford for (int k=0;k<n;k++) dis[k]=inf; dis[s]=0; quit=false; pre[s]=s; while (!quit) { quit=true; for (i=0;i<n;i++) for (j=0;j<n;j++) if (flow[i][j]<graph[i][j]&&dis[i]+cost[i][j]<dis[j]) { dis[j]=dis[i]+cost[i][j]; pre[j]=i; quit=false; } } if (dis[t]==inf) break; int delta=inf; for (i=t;i!=s;i=pre[i]) delta=min(delta,graph[pre[i]][i]-flow[pre[i]][i]); for (i=t;i!=s;i=pre[i]) { flow[pre[i]][i] += delta; flow[i][pre[i]] = -flow[pre[i]][i]; } res += delta*dis[t]; } return res; } int main() { char str[32]; int id; int total_case; int a,b,c,d; scanf("%d",&total_case); while (total_case--) { scanf("%s%d",str,&id); scanf("%d%d%d%d",&n,&m,&s,&t); memset(flow,0,sizeof(flow)); memset(cost,0,sizeof(cost)); memset(graph,0,sizeof(graph)); for (int i=0;i<m;i++) { scanf("%d%d%d%d",&a,&b,&d,&c); graph[a][b]=d; cost[a][b]=c; cost[b][a]=-c; } printf("%d\n",deltaCost()); } return 0; } |












帮你测了一下,我机器上跑了32秒~我的跑29秒,嘿嘿~
Orz,小师兄又出模板了
orz 和精简的模板啊^_^
你好,请教一下。你这个最小费用最大流的算法吗?能否把输入的格式说明一下,我现在需要用到这处算法。。。。
@janlim 测试数据和输入格式说明已经发送至你邮箱
你好,可否也给我发下测试数据和输入格式,急着用。多谢了
@jessica 测试数据和输入格式说明已经发送至你邮箱
测试数据和输入格式 也给我发一份吧 我也想用
测试数据和输入格式 也给我发一份吧 我也想用 我的邮箱是 hanbing434861465@163.com