标签:
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3667
思路:平方关系,直接建图每次增广并不是最优。。。。
1^2=1,2^2=1+3,3^2=1+3+5,4^2=1+3+5+7.......
所以,对于每条边<x,y>,若流量为c,则在x与y之间连c条边,流量均为1,费用分别为a[i],3*a[i],5*a[i].........由于每次增广时流量相同时选择最小花费的边,若该边<x,y>流量为c,则总花费为a[x]+3*a[x]+5*a[x]+.........+(2*c-1)*a[x]=a[x]*c^2,符合题意。对新图求最小费用最大流即可,若最大流小于k,则无解。
#include<cmath> #include<queue> #include<vector> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define debu using namespace std; typedef long long LL; const int maxn=200; const int INF=0x3f3f3f3f; struct Edge { int from,to,cap,flow,cost; Edge(int u,int v,int c,int f,int w):from(u),to(v),cap(c),flow(f),cost(w) {} }; queue<int> q; vector<Edge> edges; vector<int> G[maxn]; int n,m,k,p[maxn],a[maxn]; int inq[maxn],d[maxn]; LL cost; void init(int n) { memset(p,0,sizeof(p)); memset(a,0,sizeof(a)); memset(d,0,sizeof(d)); for(int i=0; i<=n+1; i++) G[i].clear(); edges.clear(); } void addedges(int from,int to,int cap,int cost) { edges.push_back(Edge(from,to,cap,0,cost)); edges.push_back(Edge(to,from,0,0,-cost)); int m=edges.size(); G[from].push_back(m-2); G[to].push_back(m-1); } bool BF(int s,int t,int& flow,LL& cost) { while(!q.empty()) q.pop(); for(int i=0; i<=n; i++) d[i]=INF; memset(inq,0,sizeof(inq)); d[s]=0,inq[s]=1,p[s]=0; q.push(s); a[s]=INF; while(!q.empty()) { int u=q.front(); q.pop(); inq[u]=0; for(int i=0; i<G[u].size(); i++) { Edge& e=edges[G[u][i]]; if(e.cap>e.flow&&d[e.to]>d[u]+e.cost) { d[e.to]=d[u]+e.cost; p[e.to]=G[u][i]; a[e.to]=min(a[u],e.cap-e.flow); if(!inq[e.to]) { q.push(e.to); inq[e.to]=1; } } } } if(d[t]==INF) return 0; flow+=a[t]; cost+=(LL)d[t]*(LL)(a[t]); //cout<<a[t]<<" "<<d[t]<<" "<<flow<<" "<<cost<<endl; for(int u=t; u!=s; u=edges[p[u]].from) { edges[p[u]].flow+=a[t]; edges[p[u]^1].flow-=a[t]; } return 1; } int mincostmaxflow(int s,int t,LL& cost) { int flow=0; cost=0; while(BF(s,t,flow,cost)); return flow; } int main() { #ifdef debug freopen("in.in","r",stdin); #endif // debug ios::sync_with_stdio(0); while(cin>>n>>m>>k) { init(n); for(int i=0; i<m; i++) { int x,y,c,w,tmp=1; cin>>x>>y>>w>>c; //cout<<x<<" "<<y<<" "<<w<<" "<<c<<endl; for(int j=1; j<=c; j++) { addedges(x,y,1,w*tmp); tmp+=2; } } addedges(0,1,k,0); //cout<<"123"<<endl; int tmp=mincostmaxflow(0,n,cost); //cout<<tmp<<endl; if(tmp<k) cout<<-1<<endl; else cout<<cost<<endl; //cout<<"flag"<<endl; } return 0; }
Hdu 3667 Transportation(最小费用流+思路)
标签:
原文地址:http://blog.csdn.net/wang2147483647/article/details/52224113