bzoj1834: Maximum flow + minimum cost maximum flow

Source: Internet
Author: User

Why yesterday looked at the time has been not understand, sure enough IQ is flawed ... And then I see it tonight. Orzorz, the key is to limit the capacity of that skill ...

--------------------------------------------------------------------------------------------

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace Std;
#define REP (i,n) for (int i=1;i<=n;i++)
#define CLR (x,c) memset (x,c,sizeof (x))
const int inf=0x3f3f3f3f;
int read () {
int X=0;char C=getchar ();
while (!isdigit (c)) C=getchar ();
while (IsDigit (c)) {
x=x*10+c-' 0 ';
C=getchar ();
}
return x;
}


struct edge{
int to,cap,cost;
Edge *next,*rev;
};
Edge e[20005],*pt=e,*cur[1005],*p[1005],*head[1005];
int d[1005],cnt[1005],inq[1005],a[1005];
void Add (int u,int v,int D,int w) {
pt->to=v;pt->cap=d;pt->cost=w;pt->next=head[u];head[u]=pt++;
}
void Adde (int u,int v,int D,int w) {
Add (u,v,d,w); add (v,u,0,-w);
head[u]->rev=head[v];
head[v]->rev=head[u];
}
struct edge{
int from,to,cap,cost;
}EDGES[5005];


int maxflow (int s,int t,int N) {
CLR (d,0); CLR (cnt,0); cnt[0]=n;
int flow=0,a=inf,x=s;
while (D[s]<n) {
Edge *ee;
for (Ee=cur[x];ee;ee=ee->next)
if (ee->cap>0&&d[ee->to]+1==d[x]) break;
if (EE) {
p[ee->to]=cur[x]=ee;
A=min (A,EE-&GT;CAP);
x=ee->to;
if (x==t) {
while (X!=s) {
p[x]->cap-=a;
p[x]->rev->cap+=a;
x=p[x]->rev->to;
}
Flow+=a;
A=inf;
}
}else{
if (!--Cnt[d[x]]) break;
D[x]=n;
for (Ee=head[x];ee;ee=ee->next) {
if (Ee->cap>0&&d[ee->to]+1<d[x]) {
d[x]=d[ee->to]+1;
Cur[x]=ee;
}
}
cnt[d[x]]++;
if (x!=s) x=p[x]->rev->to;
}
}
return flow;
}


int mincost (int s,int t) {
int cost=0;
while (1) {
CLR (D,inf); CLR (inq,0);
D[s]=0;a[s]=inf;
Queue<int>q;q.push (s); inq[s]=1;
while (!q.empty ()) {
int X=q.front (); Q.pop (); inq[x]=0;
For (Edge *ee=head[x];ee;ee=ee->next) {
if (Ee->cap>0&&d[x]+ee->cost<d[ee->to]) {
d[ee->to]=d[x]+ee->cost;
p[ee->to]=ee;
A[ee->to]=min (A[X],EE-&GT;CAP);
if (!inq[ee->to]) {
Q.push (ee->to);inq[ee->to]=1;
}
}
}
}
if (d[t]==inf) break;
COST+=A[T]*D[T];
int x=t;
while (X!=s) {
p[x]->cap-=a[t];
p[x]->rev->cap+=a[t];
x=p[x]->rev->to;
}
}
return cost;
}


int main () {
int N=read (), M=read (), K=read ();
Rep (i,m) {
Edge &o=edges[i];
O.from=read (), O.to=read (), O.cap=read (), O.cost=read ();
Adde (o.from,o.to,o.cap,0);
}
int s=1,t=n;
printf ("%d", Maxflow (S,t,n));
Rep (i,m) {
Edge &o=edges[i];
Adde (O.from,o.to,inf,o.cost);
}
Adde (0,s,k,0); Adde (t,n+1,k,0);
printf ("%d\n", Mincost (0,n+1));
return 0;
}

--------------------------------------------------------------------------------------------

1834: [Zjoi2010]network Network expansion time limit:3 Sec Memory limit:64 MB
submit:2262 solved:1129
[Submit] [Status] [Discuss] Description given a forward graph, each edge has a capacity of C and an expansion fee of W. The expansion fee here refers to the cost of expanding the capacity by 1. 1, in the case of no expansion, the maximum flow of 1 to N, 2, the maximum flow of 1 to n increases the minimum cost of expansion required by K. The first line of the input file contains three integer n,m,k, indicating the number of points, sides, and the amount of traffic required to the graph. The next M-line contains four integer u,v,c,w, representing an edge from U to V with a capacity of C and a expansion fee of W. The output file row contains two integers that represent the answers to question 1 and question 2, respectively. Sample Input5 8 2
1 2 5 8
2 5 9 9
5 1 6 2
5 1 1 8
1 2 8 7
2 5 4 9
1 2 1 1
1 4 2 1
Sample Output13 19
30% of the data, n<=100
100% of the data, n<=1000,m<=5000,k<=10
HINT Source

Day1

[Submit] [Status] [Discuss]

bzoj1834: Maximum flow + minimum cost maximum flow

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.