P3381 [TEMPLATE] minimum fee maximum flow, minimum fee of p3381 Template

Source: Internet
Author: User

P3381 [TEMPLATE] minimum fee maximum flow, minimum fee of p3381 Template
Description

For example, a network diagram and its source and sink points are provided. The maximum traffic and unit traffic fee are known for each edge, and the maximum network flow and the minimum fee are obtained.

Input/Output Format

Input Format:

 

The first line contains four positive integers N, M, S, and T, indicating the number of vertices, the number of directed edges, The Source Vertex sequence number, and the sink vertex sequence number.

Each row in the next M line contains four positive integers, ui, vi, wi, and fi, indicating that the I-th directed edge departs from the ui and reaches vi, the edge weight is wi (that is, the maximum traffic for this edge is wi), and the unit traffic fee is fi.

 

Output Format:

 

A row contains two integers, which are the maximum traffic and the minimum fee in the case of the maximum traffic.

 

Input and Output sample input sample #1:
4 5 4 34 2 30 24 3 20 32 3 20 12 1 30 91 3 40 5
Output sample #1:
50 280
Description

Time-Space limit: 1000 ms, 128 M

(BYX: The last two points are changed to 1200 ms)

Data scale:

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

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

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

Example:

, The optimal solution is as follows:

The first stream is 4 --> 3, the traffic is 20, and the fee is 3*20 = 60.

The second stream is 4 --> 2 --> 3, the traffic is 20, and the fee is (2 + 1) * 20 = 60.

The third stream is 4 --> 2 --> 1 --> 3, the traffic is 10, and the fee is (2 + 9 + 5) * 10 = 160.

Therefore, the maximum traffic is 50. In this case, the minimum fee is 60 + 60 + 160 = 280.

Therefore, the output is 50 280.

 

Template question of SPFA billing flow,

Use SPFA to check whether it can be extended

 

  1 #include<iostream>  2 #include<cstdio>  3 #include<cstring>  4 #include<cmath>  5 #include<queue>  6 using namespace std;  7 const int MAXN=2000001;  8 const int maxn=0x7fffffff;  9 void read(int &n) 10 { 11     char c='+';int x=0;bool flag=0; 12     while(c<'0'||c>'9'){c=getchar();if(c=='-')flag=1;} 13     while(c>='0'&&c<='9'){x=x*10+(c-48);c=getchar();} 14     flag==1?n=-x:n=x; 15 } 16 struct node 17 { 18     int u,v,flow,spend,nxt; 19 }edge[MAXN]; 20 int head[MAXN]; 21 int num=0; 22 int n,m,s,t; 23 int ans=0,maxflow=0; 24 int dis[MAXN]; 25 int vis[MAXN]; 26 int from[MAXN]; 27 void add_edge(int x,int y,int z,int c) 28 { 29     edge[num].u=x; 30     edge[num].v=y; 31     edge[num].flow=z; 32     edge[num].spend=c; 33     edge[num].nxt=head[x]; 34     head[x]=num++; 35 } 36 bool SPFA() 37 { 38     for(int i=1;i<=n;i++) 39         dis[i]=maxn; 40     memset(vis,0,sizeof(vis)); 41     dis[s]=0; 42     queue<int>q; 43     q.push(s); 44     vis[s]=1; 45     while(q.size()!=0) 46     { 47         int p=q.front(); 48         q.pop(); 49         vis[p]=0; 50         for(int i=head[p];i!=-1;i=edge[i].nxt) 51         { 52             if(dis[edge[i].v]>dis[edge[i].u]+edge[i].spend&&edge[i].flow>0) 53             { 54                 dis[edge[i].v]=dis[edge[i].u]+edge[i].spend; 55                 from[edge[i].v]=i; 56                 if(!vis[edge[i].v]) 57                 { 58                     vis[edge[i].v]=1; 59                     q.push(edge[i].v); 60                 } 61             } 62         } 63     } 64     if(dis[t]!=maxn) 65         return 1; 66     else  67         return 0; 68      69 } 70 void f() 71 { 72     int mn=maxn; 73     for(int i=t;i!=s;i=edge[from[i]].u) 74         mn=min(mn,edge[from[i]].flow); 75     for(int i=t;i!=s;i=edge[from[i]].u) 76     { 77         edge[from[i]].flow-=mn; 78         edge[from[i]^1].flow+=mn; 79         ans+=(mn*edge[from[i]].spend); 80     } 81     maxflow+=mn; 82 } 83 int main() 84 { 85     read(n);read(m);read(s);read(t); 86     //s=1; 87     //t=n; 88     memset(head,-1,sizeof(head)); 89     for(int i=1;i<=m;i++) 90     { 91         int x,y,z,c; 92         read(x);read(y);read(z);read(c); 93         add_edge(x,y,z,c); 94         add_edge(y,x,0,-c); 95     } 96     while(SPFA()) 97         f(); 98     printf("%d %d",maxflow,ans); 99     return 0;100 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.