Luogp3381 [TEMPLATE] The maximum minimum fee (dijstra fee stream) and p3381dijstra

Source: Internet
Author: User

Luogp3381 [TEMPLATE] The maximum minimum fee (dijstra fee stream) and p3381dijstra
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: Copy
4 5 4 34 2 30 24 3 20 32 3 20 12 1 30 91 3 40 5
Output example #1: Copy
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.

 

The dijstra fee stream is really not very fast

SPFA

I have a good blog.

Http://www.yhzq-blog.cc/%E6%9C%80%E5%B0%8F%E8%B4%B9%E7%94%A8%E6%9C%80%E5%A4%A7%E6%B5%81%E7% AE %97%E6%B3%95%E6%80%BB%E7%BB%93/

The last sentence is * h, not * dis.

In my personal understanding, because h exists during the most short circuit, the dis here is not actually dis, and h is actually dis.

 

// luogu-judger-enable-o2#include<cstdio>#include<cstring>#include<queue>#define Pair pair<int,int>#define fi first#define se second#define AddEdge(x,y,f,z) add_edge(x,y,f,z);add_edge(y,x,0,-z);#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXN,stdin),p1==p2)?EOF:*p1++)char buf[1<<20],*p1=buf,*p2=buf;using namespace std;const int MAXN=1e6+1,INF=1e8+10;inline int read(){    char c=getchar();int x=0,f=1;    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}    return x*f;}int N,M,S,T;struct node{    int u,v,f,w,nxt;}edge[MAXN];int head[MAXN],num=2;inline void add_edge(int x,int y,int f,int z){    edge[num].u=x;    edge[num].v=y;    edge[num].f=f;    edge[num].w=z;    edge[num].nxt=head[x];    head[x]=num++;}int h[MAXN],dis[MAXN],PrePoint[MAXN],PreEdge[MAXN];Pair Dij(){    int ansflow=0,anscost=0;    while(1)    {        priority_queue<Pair>q;        memset(dis,0xf,sizeof(dis));        dis[S]=0;        q.push(make_pair(0,S));        while(q.size()!=0)        {            Pair p=q.top();q.pop();            if(-p.fi!=dis[p.se]) continue;            if(p.se==T) break;            for(int i=head[p.se];i!=-1;i=edge[i].nxt)            {                int nowcost=edge[i].w+h[p.se]-h[edge[i].v];                if(edge[i].f>0&&dis[edge[i].v]>dis[p.se]+nowcost)                {                    dis[edge[i].v]=dis[p.se]+nowcost;                    q.push(make_pair(-dis[edge[i].v],edge[i].v));                    PrePoint[edge[i].v]=p.se;                    PreEdge[edge[i].v]=i;                }            }        }        if(dis[T]>INF) break;        for(int i=0;i<=N;i++) h[i]+=dis[i];        int nowflow=INF;        for(int now=T;now!=S;now=PrePoint[now])            nowflow=min(nowflow,edge[PreEdge[now]].f);        for(int now=T;now!=S;now=PrePoint[now])            edge[PreEdge[now]].f-=nowflow,            edge[PreEdge[now]^1].f+=nowflow;        ansflow+=nowflow;        anscost+=nowflow*h[T];    }    return make_pair(ansflow,anscost);}int main(){    #ifdef WIN32    freopen("a.in","r",stdin);    #endif    memset(head,-1,sizeof(head));    N=read(),M=read(),S=read(),T=read();    for(int i=1;i<=M;i++)    {        int x=read(),y=read(),f=read(),z=read();        AddEdge(x,y,f,z);    }    Pair ans=Dij();    printf("%d %d",ans.fi,ans.se);    return 0;}

 

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.