[3381] Luo Gu minimum cost maximum flow

Title Description

If that, given a network diagram, and the source and sink, each side units is known maximum flow and traffic charges, which determine the maximum flow and minimum cost network in the case of maximum flow.

Input Format

The first row contains four positive integers N, M, S, T, respectively, the number of points, there are the number of edges, number source, sink point number.

Next M lines contains four integer ui, vi, wi, fi, denotes the i th ui from there to the edge and arriving at VI, the right side of wi (i.e., the side of the maximum flow rate Wi), per unit flow rate fee fi.

Output Format

One line containing two integers, followed by the maximum flow and minimum cost at maximum flow conditions.

Sample input and output

Input # 1
4 5 4 3
4 2 30 2
4 3 20 3
2 3 20 1
2 1 30 9
1 3 40 5
Output # 1
50 280

Description / Tips

Constraints of time: 1000ms, 128M

(BYX: The last two points into the 1200ms)

Data Scale:

For 30% of the data: N <= 10, M <= 10

For 70% of the data: N <= 1000, M <= 1000

To 100% of the data: N <= 5000, M <= 50000

Sample Description:

As shown, the optimal solutions are as follows:

The first stream is a 4 -> 3, flow rate of 20, a cost of 3 * 20 = 60.

The second stream is a 4 -> 2 -> 3, flow rate of 20, the cost is (2 + 1) * 20 = 60.

Third flow 4 -> 2 -> 1 -> 3, flow rate of 10, costs (9 + 2 + 5) * 10 = 160.

Therefore, the maximum flow rate is 50, the minimum cost in this case is 60 + 60 + 160 = 280.

Therefore, the output of 50,280.

 

Solution: Template minimum cost maximum flow.

#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<cstdio>
using namespace std;
const int N=100006;
const int oo=0x3f3f3f3f;
int n,m,s,t,ans1,ans2,cnt=1;
struct node{
    int to,next,w,f;
}e[N];
bool vis[N];
int head[N],flow[N],pre[N],dis[N];
void add(int x,int y,int z,int v){
    cnt++; e[cnt].to=y;
    e[cnt].w=z; e[cnt].f=v;
    e[cnt].next=head[x];
    head[x]=cnt;
    
    cnt++; e[cnt].to=x;
    e[cnt].w=0; e[cnt].f=-v;
    e[cnt].next=head[y];
    head[y]=cnt;
}

queue<int>q;
bool SPFA(int s,int t){
    memset(dis,oo,sizeof(dis));
    memset(vis,0,sizeof(vis));
    dis[s]=0; vis[s]=1;
    flow[s]=oo; q.push(s);
    while(!q.empty()){
        int u=q.front();
        q.pop(); vis[u]=0;
        for(int i=head[u];i!=-1;i=e[i].next){
            int v=e[i].to;
            int f=e[i].f;
            if(e[i].w && dis[u]+f<dis[v]){
                dis[v]=dis[u]+f; pre[v]=i;
                flow[v]=min(flow[u],e[i].w);
                if(!vis[v])
                   { vis[v]=1; q.push(v); }
            }
        }
    }
    if(dis[t]==oo) return 0;
    else return 1;
}
 
void dfs(int s,int t){
    int x=t;
    while(x!=s){
        int i=pre[x];
        e[i].w-=flow[t];
        e[i^1].w+=flow[t];
        x=e[i^1].to;
    }
    ans1+=flow[t];
    ans2+=flow[t]*dis[t];
}
int main(){
    freopen("3381.in","r",stdin);
    freopen("33811.out","w",stdout);
    memset(head,-1,sizeof(head));
    scanf("%d %d %d %d",&n,&m,&s,&t);
    int x,y,z,v;
    for(int i=1;i<=m;i++){
        scanf("%d %d %d %d",&x,&y,&z,&v);
        add(x,y,z,v);
    }
    while(SPFA(s,t)) dfs(s,t);
    //cout<<6666;
    printf("%d %d\n",ans1,ans2);    
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/wuhu-JJJ/p/11298521.html