标签:
挺裸的最小费用最大流。。。
#include<cstdio> #include<queue> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define rep(i,n) for(int i=1;i<=n;i++) #define clr(x,c) memset(x,c,sizeof(x)) #define op() clr(head,0);pt=edges; #define REP(i,s,t) for(int i=s;i<=t;i++) #define qwq(x) for(edge *o=head[x];o;o=o->next) int read(){ int x=0;char c=getchar(); while(!isdigit(c)) c=getchar(); while(isdigit(c)) x=x*10+c-‘0‘,c=getchar(); return x; } const int nmax=405; const int maxn=100005; const int inf=0x7f7f7f7f; struct edge{ int to,cap,cost;edge *next,*rev; }; struct oo{ int cap,cost; oo(int cap,int cost):cap(cap),cost(cost){} }; edge edges[maxn<<1],*pt,*head[nmax],*p[nmax]; int d[nmax],a[nmax];bool inq[nmax]; 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]; } oo mincost(int s,int t){ int flow=0,cost=0; while(1){ clr(d,0x7f),d[s]=0;clr(inq,0);inq[s]=1;a[s]=inf; queue<int>q;q.push(s); while(!q.empty()){ int x=q.front();q.pop();inq[x]=0; qwq(x) if(o->cap>0&&d[o->to]>d[x]+o->cost){ int to=o->to;d[to]=d[x]+o->cost; a[to]=min(a[x],o->cap);p[to]=o; if(!inq[to]) q.push(to),inq[to]=1; } } if(d[t]==inf) break; flow+=a[t];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; } return oo(flow,cost); } int main(){ op(); int n=read(),m=read(),u,v,d; REP(i,2,n-1) adde(i,n+i,1,0); rep(i,m) u=read(),v=read(),d=read(),u==1?adde(u,v,1,d):adde(u+n,v,1,d); /*rep(i,n) { qwq(i) printf("%d ",o->to);printf("\n"); }*/ oo o=mincost(1,n); printf("%d %d\n",o.cap,o.cost); return 0; }
对于30%的数据,N ≤ 20,M ≤ 120。
对于100%的数据,N ≤ 200,M ≤ 20000。
标签:
原文地址:http://www.cnblogs.com/fighting-to-the-end/p/5662650.html