bzoj1834: [Zjoi2010]network Network expansion

Source: Internet
Author: User

Hard to see a long time sample has been unable to ... Then various output intermediate process Ah Balabala got 1h, no way ... Then suddenly think of ah the original side can use AH why not ... So a ... Touching

#include <cstdio> #include <cstring> #include <queue> #include <iostream> #include < algorithm>using namespace std; #define REP (I,n) for (int. i=1;i<=n;i++) #define QWQ (x) for (Edge *e=head[x];e;e=e- >next) #define CLR (x,c) memset (x,c,sizeof (x)) #define REP (i,s,t) for (int i=s;i<=t;i++) int read () {int X=0;char c= GetChar (); bool F=true;while (!isdigit (c)) {if (c== '-') F=false;c=getchar ();} while (IsDigit (c)) x=x*10+c-' 0 ', C=getchar (); return f?x:-x;} const int NMAX=1005;CONST int Maxn=20005;const int inf=0x7f7f7f7f;struct edge{int to,cap,cost;edge *next,*rev;}; Edge Edges[maxn],*pt,*head[nmax],*cur[nmax],*p[nmax];int u[maxn],v[maxn],w[maxn];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];} BOOL Inq[nmax];int h[nmax],cnt[nmax],d[nmax],a[nmax];int mincost (int s,int t) {int Cost=0;while (1) {clr (inq,0); inq[s]= 1;CLR (d,0x7f);d [S]=0;a[s]=inf;queue<int>q;q.push (s); while (!q.empty ()) {int X=q.front (); Q.pop (); inq[x]=0;//printf ("% D:%d\n ", x,d[x]); Qwq (x) if (e->cap>0&&d[e->to]>d[x]+e->cost) {int to=e->to;d[to]=d[x]+e- >cost;//printf ("tmd:%d\n", E->cost), A[to]=min (A[x],e->cap);p [To]=e;if (!inq[to]) Q.push (to), Inq[to]=1;}} if (d[t]==inf) Break;cost+=d[t]*a[t];int x=t;while (x!=s) p[x]->cap-=a[t],p[x]->rev->cap+=a[t],x=p[x]-> rev->to;//printf ("%d%d\n", d[t],cost);} return cost;} int maxflow (int s,int t,int n) {clr (cnt,0); cnt[0]=n;clr (h,0); int Flow=0,a=inf,x=s;edge *e;while (h[s]<n) {for (e=cur[ X];e;e=e->next) if (E->cap>0&&h[e->to]+1==h[x]) break;if (e) {a=min (a,e->cap);p [e->to]= Cur[x]=e;x=e->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[h[x]]) break;h[x]=n;for (e=head[x];e;e=e->next) if (e->cap>0&&h[x]>h[e->to]+ 1) cur[x]=e,h[x]=h[e->to]+1;cnt[h[x]]++;if (X!=s) x=p[x]->rev->to;}} return flow;} int main () {int n=read (), M=read (), K=read (), SA=1,TA=N,SB=0,TB=N+1,CAP;PT=EDGES;CLR (head,0), Rep (i,m) {u[i]=read (), v[ I]=read (), Cap=read (), W[i]=read (); Adde (u[i],v[i],cap,0);} /*rep (i,n) {qwq (i) printf ("%d%d%d\n", e->to,e->cap,e->cost);p rintf ("\ n");} */printf ("%d", Maxflow (Sa,ta,n));//pt=edges;clr (head,0); Rep (i,m) Adde (U[i],v[i],k,w[i]); Adde (sb,sa,k,0); Adde (TA, tb,k,0);/*rep (i,0,n) {qwq (i) printf ("%d%d%d\n", e->to,e->cap,e->cost);p rintf ("\ n");} */printf ("%d\n", Mincost (SB,TB)); return 0;}

 

1834: [Zjoi2010]network Network expansion time limit:3 Sec Memory limit:64 MB
submit:2628 solved:1333
[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: [Zjoi2010]network Network expansion

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.