[网络流24题] COGS 运输问题1

11. 运输问题1

★★☆   输入文件:maxflowa.in   输出文件:maxflowa.out   简单对比
时间限制:1 s   内存限制:128 MB

【问题描述】
    一个工厂每天生产若干商品,需运输到销售部门进行销售。从产地到销地要经过某些城镇,有不同的路线可以行走,每条两城镇间的公路都有一定的流量限制。请你计算,在不考虑其它车辆使用公路的前提下,如何充分利用所有的公路,使产地运输到销地的商品最多,最多能运输多少商品。
【输入格式】
输入文件有若干行
第一行,一个整数n,表示共有n个城市(2<=n<=100),产地是1号城市,销地是n号城市。
下面有n行,每行有n个数字。第p行第q列的数字表示城镇p与城镇q之间有无公路连接。数字为0表示无,大于0表示有公路,且该数字表示该公路流量。
【输出格式】
输出文件有一行
第一行,1个整数max,表示最大流量为max。
【输入输出样例】
输入文件名: maxflowa.in
6
0 4 8 0 0 0
0 0 4 4 1 0
0 0 0 2 2 0
0 0 0 0 0 7
0 0 0 6 0 9
0 0 0 0 0 0
输出文件名:maxflowa.out
8
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<queue>
 5 using namespace std;
 6 #define N 105
 7 struct Node{
 8     int u,v,w,next;
 9 }e[N*N];
10 int dep[N],n,m,tot=1,head[N],ans,maxx=1e9;// 一定注意要从第二条边开始用   否则会WA
11 void Add_Edge(int u,int v,int w){
12     e[++tot].u=u;e[tot].v=v;e[tot].w=w;
13     e[tot].next=head[u];head[u]=tot;
14 }
15 queue<int>q;
16 bool BFS(int s,int t){
17     while(!q.empty())q.pop();
18     memset(dep,-1,sizeof dep );
19     dep[s]=0;q.push(s);
20     while(!q.empty()){
21         int u=q.front();q.pop();
22         for(int i=head[u];i;i=e[i].next){
23             int v=e[i].v,w=e[i].w;
24             if(dep[v]==-1&&w){
25                 dep[v]=dep[u]+1;
26                 if(v==t)return true;
27                 else q.push(v);
28             }
29         }
30     }
31     return false;
32 }
33 int DFS(int now,int flow){// flow 表示分配到
34     //该节点的流量 分配到起始节点的 为无限大
35     if(now==n)return flow;
36     int ret=0;
37     for(int i=head[now];i;i=e[i].next){
38         int v=e[i].v,w=e[i].w;
39         if(dep[v]==dep[now]+1&&w){
40             int x=DFS(v,min(flow-ret,w));
41             ret+=x;
42             e[i].w-=x;
43             e[i^1].w+=x;
44         }
45     }
46     return ret;
47 }
48 void Dinic(int s,int t){
49     while(BFS(s,t))ans+=DFS(s,maxx);
50     printf("%d
",ans);
51 }
52 int main(){
53     freopen("maxflowa.in","r",stdin);
54     freopen("maxflowa.out","w",stdout);
55     scanf("%d",&n);
56     for(int i=1;i<=n;i++)
57         for(int j=1,w;j<=n;j++){
58             scanf("%d",&w);
59             if(!w)continue;
60             Add_Edge(i,j,w);Add_Edge(j,i,0);
61         }
62     Dinic(1,n);
63     return 0;
64 }
原文地址:https://www.cnblogs.com/suishiguang/p/6519455.html