傳送門
 網絡流水題啊。
 
第一問直接放心跑最大流(本來還以為有什么tricktricktrick)。
 第二問就直接把原來的邊(u,v,c,w)(u,v,c,w)(u,v,c,w)變成(u,v,c,0)(u,v,c,0)(u,v,c,0)和(u,v,inf,w)(u,v,inf,w)(u,v,inf,w),然后把ttt拆點限制流量跑費用流就行了。
 代碼:
 #include<bits/stdc++.h>
#define N 1005
#define M 10005
using namespace std
;
inline int read(){int ans
=0;char ch
=getchar();while(!isdigit(ch
))ch
=getchar();while(isdigit(ch
))ans
=(ans
<<3)+(ans
<<1)+(ch
^48),ch
=getchar();return ans
;
}
int s
,t
,n
,m
,k
,first
[N
],cur
[N
],d
[N
],flow
[N
],pos
[N
],pred
[N
],cnt
;
bool in
[N
];
struct Edge
{int u
,v
,w
,c
;}tt
[M
];
struct edge
{int v
,next
,w
,c
;}e
[M
<<1];
inline void addedge(int u
,int v
,int c
,int w
){e
[++cnt
].v
=v
,e
[cnt
].next
=first
[u
],e
[cnt
].c
=c
,e
[cnt
].w
=w
,e
[cnt
].v
=v
,first
[u
]=cnt
;}
inline void add(int u
,int v
,int c
,int w
){addedge(u
,v
,c
,w
),addedge(v
,u
,0,-w
);}
inline void init(){memset(first
,-1,sizeof(first
)),cnt
=-1,s
=1,t
=n
;}
inline bool bfs(){queue
<int>q
;memset(d
,-1,sizeof(d
)),d
[s
]=0,q
.push(s
);while(!q
.empty()){int x
=q
.front();q
.pop();for(int i
=first
[x
];~i
;i
=e
[i
].next
){int v
=e
[i
].v
;if(!e
[i
].c
||~d
[v
])continue;d
[v
]=d
[x
]+1,q
.push(v
);}}return ~d
[t
];
}
inline int dfs(int x
,int f
){if(!f
||x
==t
)return f
;int flow
=f
;for(int&i
=cur
[x
];~i
;i
=e
[i
].next
){int v
=e
[i
].v
;if(!flow
)return f
;if(e
[i
].c
&&d
[v
]==d
[x
]+1){int tmp
=dfs(v
,min(flow
,e
[i
].c
));if(!tmp
)d
[v
]=-1;e
[i
].c
-=tmp
,e
[i
^1].c
+=tmp
,flow
-=tmp
;}}return f
-flow
;
}
inline bool spfa(){queue
<int>q
;memset(d
,0x3f3f3f3f,sizeof(d
)),memset(in
,false,sizeof(in
)),q
.push(s
),d
[s
]=0,in
[s
]=true,pred
[t
]=-1,flow
[s
]=0x3f3f3f3f;while(!q
.empty()){int x
=q
.front();q
.pop(),in
[x
]=false;for(int i
=first
[x
];~i
;i
=e
[i
].next
){int v
=e
[i
].v
;if(e
[i
].c
&&d
[v
]>d
[x
]+e
[i
].w
){d
[v
]=d
[x
]+e
[i
].w
,pred
[v
]=x
,pos
[v
]=i
,flow
[v
]=min(flow
[x
],e
[i
].c
);if(!in
[v
])in
[v
]=true,q
.push(v
);}}}return d
[t
]!=0x3f3f3f3f;
}
inline int dinic(){int ret
=0;while(bfs()){for(int i
=1;i
<=n
;++i
)cur
[i
]=first
[i
];ret
+=dfs(s
,0x3f3f3f3f);}return ret
;
}
inline int mcmf(){int ret
=0;for(int w
=t
;spfa();w
=t
){ret
+=flow
[t
]*d
[t
];while(w
!=s
)e
[pos
[w
]].c
-=flow
[t
],e
[pos
[w
]^1].c
+=flow
[t
],w
=pred
[w
];}return ret
;
}
int main(){n
=read(),m
=read(),k
=read(),init();for(int i
=1;i
<=m
;++i
){tt
[i
].u
=read(),tt
[i
].v
=read(),tt
[i
].c
=read(),tt
[i
].w
=read();add(tt
[i
].u
,tt
[i
].v
,tt
[i
].c
,0);}int tmp
=dinic();printf("%d ",tmp
);memset(first
,-1,sizeof(first
)),cnt
=-1;for(int i
=1;i
<=m
;++i
)add(tt
[i
].u
,tt
[i
].v
,tt
[i
].c
,0),add(tt
[i
].u
,tt
[i
].v
,0x3f3f3f3f,tt
[i
].w
);add(n
,n
+1,k
+tmp
,0),t
=n
+1,printf("%d",mcmf());return 0;
}
 
 
轉載于:https://www.cnblogs.com/ldxcaicai/p/10084899.html
                            總結
                            
                                以上是生活随笔為你收集整理的2018.10.13 bzoj1834: [ZJOI2010]network 网络扩容(最大流+费用流)的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                            
                                如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。