The "BZOJ3876" "Ahoi2014" spur plot has the lowest cost maximum flow in the Nether

Source: Internet
Author: User

#include <stdio.h>int main () {puts ("reprint please specify source thank you");p UTS ("http://blog.csdn.net/vmurder/article/details/43025375 ");}


"BZOJ2324" rescue Pikachu This problem is also a lower-bound minimum cost maximum flow.

My address: http://blog.csdn.net/vmurder/article/details/41378979


This problem is actually a template problem.

My approach is to take each side of a flow of 1 out, and then the cost of the original cost-inf. In the construction of the diagram can be deducted from these INF added back. It can be proved that this method will not be stuck in a negative loop at least on the topology map.


Code:

#include <queue> #include <cstdio> #include <cstring> #include <iostream> #include < algorithm> #define N 500#define M 31000#define inf 0x3f3f3f3f#define inf 0x3f3f3f3f3f3f3f3fllusing namespace std; struct ksd{int u,v,len,next;long long fee;} E[m];int head[n],cnt;void Add (int u,int v,int len,int fee) {Cnt++;e[cnt].u=u;e[cnt].v=v;e[cnt].len=len;e[cnt].fee=fee ; e[cnt].next=head[u];head[u]=cnt;} int S,t;long long dist[n];int pre[n],lim[n];bool in[n];queue<int>q;void SPFA () {memset (dist,0x3f,sizeof (Dist)); memset (Lim,0,sizeof (LIM)), while (!q.empty ()) Q.pop (), int i,u,v;dist[s]=0,in[s]=1;lim[s]=inf;q.push (s), while (! Q.empty ()) {U=q.front (), Q.pop (), In[u]=0;for (I=head[u];i;i=e[i].next) {v=e[i].v;if (!e[i].len) continue;if (Dist[v] >dist[u]+e[i].fee) {dist[v]=dist[u]+e[i].fee;lim[v]=min (E[i].len,lim[u]);p re[v]=i;if (!in[v]) {In[v]=1;q.push ( v);}}} return;} void handle (int flow) {for (int i=pre[t];i;i=pre[e[i].u]) {e[i].len-=flow;e[i^1].len+=flow;}} Long Long minfee;int n,m;void build (){int I,j,k;int a,b,c;cnt=1;scanf ("%d", &n), S=n+1,t=n+2;add (s,1,inf,0), add (1,s,0,0); for (i=1;i<=n;i++) {scanf ("%d", &a), add (i,t,inf,0), add (t,i,0,0), while (a--) {scanf ("%d%d", &b,&c); Minfee+=inf;add (I,b,inf,c), Add (b,i,0,-c); Add (I,b,1,c-inf), add (B,i,0,inf-c);}} return;} void Checkedge () {for (int u=1;u<=t;u++) {printf (' from%d: ', u), for (int i=head[u];i;i=e[i].next) {printf ("%d/%d/%d", E[i].v,e[i].len,e[i].fee);} Puts ("");} Puts ("");} int main () {freopen ("test.in", "R", stdin); build (); while (SPFA (), Dist[t]<inf) {Minfee+=dist[t]*lim[t];handle (lim[ T]);} Cout<<minfee<<endl;return 0;}


The "BZOJ3876" "Ahoi2014" spur plot has the lowest cost maximum flow in the Nether

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.