"Bzoj" 1834: [Zjoi2010]network Network expansion (max Stream + expense flow)

Source: Internet
Author: User

I've been thinking about life again. T_t,nd Array opened small, has been WA, adjusted for one hours only to find AH!!!!! I always thought my isap wrong t_t, but absolutely right!!!!

In fact, the first question is very simple, run once the maximum flow can. The second question is to run the maximum flow of the residual network on each edge of the expansion capacity of OO, the cost of the cost of the side, and then set a super-source with a capacity of K side to point 1, and then run a cost stream.

The reason is very simple, I think, I will not say.

#include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream > #include <algorithm>using namespace std; #define REP (i, n) for (int i=0; i< (n); ++i) #define FOR1 (I,a,n) for (in T i= (a); i<= (n) ++i) #define FOR2 (i,a,n) for (int i= (a);i< (n), ++i) #define FOR3 (i,a,n) for (int i= (a); i>= (n);-I.) #define FOR4 (I,a,n) for (int i= (a);i> (n); i) #define CC (i,a) memset (i,a,sizeof (i)) #define READ (a) a=getint () #define Print (a) printf ("%d", a) #define DBG (x) cout << #x << "=" << x << endl#define Printarr (A, N, m) r EP (AAA, N) {rep (BBB, m) cout << a[aaa][bbb]; cout << Endl;} inline const int Getint () {int r=0, k=1; char C=getchar (); for (; c< ' 0 ' | | C> ' 9 '; C=getchar ()) if (c== '-') k=-1; for (; c>= ' 0 ' &&c<= ' 9 '; C=getchar ()) r=r*10+c-' 0 '; return k*r; }inline const int MAX (const int &a, const int &b) {return a>b?a:b;} inline const int min (const int &AMP;A, const int &b) {return a<b?a:b; }const int n=1010, m=2000000, Oo=~0u>>1;int Ihead[n], cnt=1, Cur[n], gap[n], d[n], p[n], N, M, K, Vis[n], q[n], fron  T, tail, nd[m][3];struct ED {int from, to, Cap, W, Next,} e[m];inline void Add (const int &AMP;U, const int &AMP;V, const int &c, const int &w) {e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v; e[cnt].from=u; e[cnt].cap=c; e[cnt].w= W;E[++CNT].NEXT=IHEAD[V]; ihead[v]=cnt; E[cnt].to=u; E[cnt].from=v; E[cnt].cap=0; E[cnt].w=-w;} int ISAP (const int &s, const int &t, const int &n) {for1 (i, 0, N) cur[i]=ihead[i];int ret=0, I, F, u=s;gap[0]= N;while (D[s]<n) {for (i=cur[u]; i; i=e[i].next) if (e[i].cap && d[u]==d[e[i].to]+1) break;if (i) {p[e[i].to]= Cur[u]=i; U=e[i].to;if (u==t) {for (f=oo; u!=s; u=e[p[u]].from) f=min (f, E[p[u]].cap); for (u=t; u!=s; u=e[p[u]].from) e[p[u]].cap-= F, e[p[u]^1].cap+=f;ret+=f;}} else {if (! (--gap[d[u])) Break;d[u]=n; Cur[u]=ihead[u];for (i=ihead[u]; i; i=e[i].next) if (E[i].cap && d[u]>d[e[i].to]+1) d[u]=d[e[i].to]+1;++gap[d[u]];if (u!=s) U=e[p[u]].from;}} return ret;} Inline const BOOL SPFA (const int &s, const int &t) {For1 (I, S, t) d[i]=1000000000, vis[i]=0;vis[s]=1; d[s]=front=t ail=0; Q[tail++]=s;int u, V, i;while (front!=tail) {u=q[front++], if (front==n) front=0;for (i=ihead[u]; i; i=e[i].next) if (E[i]. Cap && d[v=e[i].to]>d[u]+e[i].w) {d[v]=d[u]+e[i].w; P[v]=i;if (!vis[v]) {vis[v]=1, q[tail++]=v;if (Tail==N) Tail=0;}} vis[u]=0;} return d[t]!=1000000000;} int MCF (const int &s, const int &t) {int ret=0, F, U;while (SPFA (S, t)) {for (F=oo, u=t; u!=s; u=e[p[u]].from) f=min (f, E[p[u]].cap); for (u=t; u!=s; u=e[p[u]].from) e[p[u]].cap-=f, e[p[u]^1].cap+=f;ret+=d[t]*f;} return ret;} int main () {read (n); read (m); Read (k); int S=1, T=n, U, V, C;for1 (i, 1, m) {read (u); Read (v); Read (c); Read (nd[i][2]); Add (U , V, c, 0); nd[i][0]=u; Nd[i][1]=v;} printf ("%d", ISAP (S, T, t+1)); For1 (i, 1, m) Add (Nd[i][0], nd[i][1], Oo, nd[i][2]); s=0; Add (S, 1, K, 0);p rint (MCF (S, t)); return 0;}

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
Hintsource

Day1

"Bzoj" 1834: [Zjoi2010]network Network expansion (max Stream + expense 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.