# [BZOJ 1834] [ZJOI2010]network 网络扩容

## 1834: [ZJOI2010]network 网络扩容

Time Limit: 3 Sec
Memory Limit: 64 MB

5 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 Output

13 19
30%的数据中，N<=100
100%的数据中，N<=1000，M<=5000，K<=10

## Source

Day1

【题解】

  1 #include <stdio.h>
2 #include <queue>
3 #include <string.h>
4 using namespace std;
5 const int B=30010;
6 const int INF=2139062143;
7 int next[B], head[B], to[B], flow[B], w[B], c[B], oq[B];
8 int pre[B], cnt=-1, bi[B], n, m, k, ans=0, d[B];
9 bool vis[B];
10 int u[B], v[B], _f[B], _w[B];
11 queue <int> q;
12 inline void add(int u,int v,int flo,int _w) {
14 }
15 inline int min(int a,int b) {return a<b?a:b;}
16 inline void G(int &x) {
17     int y=0, yy=1; char ch=getchar();
18     while(ch<'0'||ch>'9') {
19         if(ch=='-') yy=-1;
20         ch=getchar();
21     }
22     while(ch>='0'&&ch<='9') {
23         y=(y<<1)+(y<<3)+ch-'0';
24         ch=getchar();
25     }
26     x=y*yy;
27 }
28 inline void qclear() {
29     while(!q.empty()) q.pop();
30 }
31 inline bool getdfn() {
32     qclear();
33     memset(c,-1,sizeof(c));
34     q.push(1); c[1]=1;
35     while(!q.empty()) {
36         int top=q.front();
37         q.pop();
39             int t=to[i];
40             if(c[t]>=0||flow[i]==0) continue;
41             c[t]=c[top]+1;
42             q.push(t);
43             if(t==n) return 1;
44         }
45     }
46     return 0;
47 }
48 int dinic(int x,int low) {
49     if(x==n) return low;
50     int flo,r=low;
52         int t=to[i], fl=flow[i];
53         if(c[t]!=c[x]+1 || fl==0) continue;
54         flo=dinic(t,min(r,fl));
55         r-=flo;
56         flow[i]-=flo;
57         flow[i^1]+=flo;
58         if(!r) return low;
59     }
60     if(r==low) c[x]=-1;
61     return low-r;
62 }
63 inline bool spfa() {
64     memset(vis,0,sizeof(vis));
65     memset(oq,0,sizeof(oq));
66     memset(d,0X7f,sizeof(d));
67     qclear();
68     vis[1]=1; d[1]=0; q.push(1);
69     while(!q.empty()) {
70         int top=q.front(); q.pop();
71         vis[top]=0; oq[top]++;
72         if(oq[top]>n) return 0;
74             if(flow[i]&&d[top]+w[i]<d[to[i]]) {
75                 d[to[i]]=d[top]+w[i];
76                 pre[to[i]]=i;
77                 if(!vis[to[i]]) {
78                     vis[to[i]]=1;
79                     q.push(to[i]);
80                 }
81             }
82         }
83     }
84     if(d[n]==INF) return 0;
85     return 1;
86 }
87 inline void mincostflow() {
88     int s=INF;
89     for (int i=pre[n];i>=0;i=pre[to[i^1]]) {
90         s=min(s,flow[i]);
91         if(to[i^1]==1) break;
92     }
93     for (int i=pre[n];i>=0;i=pre[to[i^1]]) {
94         flow[i]-=s;
95         flow[i^1]+=s;
96         ans+=s*w[i];
97         if(to[i^1]==1) break;
98     }
99 }
100 int main() {
101     G(n), G(m), G(k);
103     for (int i=1;i<=m;++i) {
104         G(u[i]), G(v[i]), G(_f[i]), G(_w[i]);
107     }
108     int _flowx=0;
109     while(getdfn()) _flowx += dinic(1,INF);
110     printf("%d ",_flowx);
111     for (int i=1;i<=m;++i) {
120 }