codeforces 277E 最小费用最大流
2013-03-02 22:07
369 查看
用邻接矩阵和邻接表两种方式各写了一遍...= =|||
矩阵代码:(953ms)
邻接表:(1437ms)
矩阵代码:(953ms)
#include<iostream> #include<cstring> #include<algorithm> #include<queue> #include<cmath> using namespace std; #define Inf 0x7fffffff #define DInf 1e9 #define eps 1e-9 #define maxn 1005 int Cap[maxn][maxn]; double Cost[maxn][maxn]; bool Inq[maxn]; double Dis[maxn]; int Pre[maxn],Low[maxn],Cnt[maxn]; queue<int> Q; int MaxFlow; double MinCost; int X[maxn],Y[maxn]; double CalDis(int x1,int y1,int x2,int y2) { return sqrt(pow(x1-x2-0.0,2)+pow(y1-y2-0.0,2)); } void AddEdge(int i,int j,int c,double d) { Cap[i][j]=c,Cost[i][j]=d; Cap[j][i]=0,Cost[j][i]=-d; } void BuildGraph(int n) { int i,j,c; double d; for(i=1;i<=n;++i) { AddEdge(0,i,2,0.0); AddEdge(i+n,2*n+1,1,0.0); for(j=1;j<=n;++j) { if(Y[i]>Y[j]) { d=CalDis(X[i],Y[i],X[j],Y[j]); AddEdge(i,j+n,1,d); } } } } bool SPFA(int s,int t) { int i,u,v; while(!Q.empty()) Q.pop(); for(i=0;i<=t;++i) { Inq[i]=false; Dis[i]=DInf; Cnt[i]=0; } Q.push(s); Inq[s]=true,Dis[s]=0.0,Cnt[s]=1,Low[s]=Inf; while(!Q.empty()) { u=Q.front(); Q.pop(),Inq[u]=false; for(v=0;v<=t;++v) { if(Cap[u][v]&&Dis[v]>Dis[u]+Cost[u][v]+eps) { Dis[v]=Dis[u]+Cost[u][v]; Pre[v]=u,Low[v]=min(Low[u],Cap[u][v]); if(!Inq[v]) { ++Cnt[v]; if(Cnt[v]>t+1) return 0; Q.push(v); Inq[v]=true; } } } } return Dis[t]<DInf; } void MaxFlowMinCost(int s,int t) { MaxFlow=0,MinCost=0.0; int v; while(SPFA(s,t)) { MinCost+=Dis[t]*Low[t]; MaxFlow+=Low[t]; for(v=t;v!=s;v=Pre[v]) { Cap[Pre[v]][v]-=Low[t]; Cap[v][Pre[v]]+=Low[t]; } } } int main() { int n,i; cin>>n; for(i=1;i<=n;++i) cin>>X[i]>>Y[i]; BuildGraph(n); MaxFlowMinCost(0,2*n+1); if(MaxFlow==n-1) { cout.precision(10); cout<<MinCost<<endl; } else cout<<-1<<endl; return 0; }
邻接表:(1437ms)
#include<iostream> #include<cstring> #include<algorithm> #include<queue> #include<cmath> using namespace std; #define maxn 1005 #define Inf 0x7fffffff #define DInf 1e9 #define eps 1e-9 struct Edge{ int v,c; double d; Edge *next,*rev; } *E[maxn],*Path[maxn],EE[maxn*maxn]; int ECnt=0; int X[maxn],Y[maxn]; queue<int> Q; double Dis[maxn]; bool Inq[maxn]; int Cnt[maxn],Low[maxn]; int MaxFlow=0; double MinCost=0.0; double CalDis(int x1,int y1,int x2,int y2) { return sqrt(pow(x1-x2-0.0,2)+pow(y1-y2-0.0,2)); } void AddEdge(int i,int j,int c,double d) { EE[ECnt].v=j,EE[ECnt].c=c,EE[ECnt].d=d,EE[ECnt].next=E[i],EE[ECnt].rev=&EE[ECnt+1]; E[i]=&EE[ECnt]; ECnt++; EE[ECnt].v=i,EE[ECnt].c=0,EE[ECnt].d=-d,EE[ECnt].next=E[j],EE[ECnt].rev=&EE[ECnt-1]; E[j]=&EE[ECnt]; ECnt++; } void BuildGraph(int n) { int i,j,u,v; double d; for(i=1;i<=n;++i) { AddEdge(0,i,2,0.0); AddEdge(i+n,2*n+1,1,0.0); for(j=1;j<=n;++j) { if(Y[i]>Y[j]) { d=CalDis(X[i],Y[i],X[j],Y[j]); AddEdge(i,j+n,1,d); } } } } bool SPFA(int s,int t) { while(!Q.empty()) Q.pop(); int i,u,v,c; double d; Edge *tp; for(i=0;i<=t;++i) { Dis[i]=DInf; Inq[i]=false; Cnt[i]=0; } Q.push(s); Dis[s]=0.0,Inq[s]=true,Cnt[s]=1,Path[s]=NULL,Low[s]=Inf; while(!Q.empty()) { u=Q.front(); Inq[u]=false,Q.pop(); for(tp=E[u];tp;tp=tp->next) { v=tp->v,c=tp->c,d=tp->d; if(c&&Dis[v]>Dis[u]+d+eps) { Dis[v]=Dis[u]+d; Path[v]=tp; Low[v]=min(Low[u],c); if(!Inq[v]) { Cnt[v]++; if(Cnt[v]>t+1) return 0; Q.push(v); Inq[v]=true; } } } } return Dis[t]<DInf; } void MaxFlowMinCost(int s,int t) { MaxFlow=0,MinCost=0.0; int i,u,v; while(SPFA(s,t)) { MaxFlow+=Low[t]; MinCost+=Dis[t]*Low[t]; for(v=t;v!=s;v=u) { u=Path[v]->rev->v; Path[v]->c-=Low[t]; Path[v]->rev->c+=Low[t]; } } } int main() { int n,i; cin>>n; for(i=1;i<=n;++i) cin>>X[i]>>Y[i]; BuildGraph(n); MaxFlowMinCost(0,2*n+1); if(MaxFlow==n-1) { cout.precision(10); cout<<MinCost<<endl; } else cout<<-1<<endl; return 0; }
相关文章推荐
- Codeforces 316C2 题解 (网络流-最小费用最大流)
- codeforces 277E - Binary Tree on Plane (MinCostMaxFlow, 简单)
- Codeforces 132E Bits of merry old England 【最小费用最大流】
- Codeforces 237E (最小费用最大流)
- [Codeforces 277E][Round #170 div.1 E]Binary Tree on Plane
- Codeforces 717G Underfail(最小费用最大流 + AC自动机)
- Codeforces 155 C.Double Profiles
- 【进阶——最小费用最大流】hdu 1533 Going Home (费用流)Pacific Northwest 2004
- CodeForces 670A Holidays
- Codeforces 233A Perfect Permutation(完美序列,水题)
- Codeforces 812A Sagheer and Crossroads[模拟]
- Codeforces 808E Selling Souvenirs(花费是倍数关系的背包)
- codeforces 187A
- CodeForces 221D Little Elephant and Array
- Codeforces 354 B Pyramid of Glasses (dp)
- CodeForces - 681A A Good Contest
- Codeforces 808D. Array Division
- CodeForces 864B Polycarp and Letters
- Codeforces 196 E. Opening Portals
- Codeforces 448C:Painting Fence 刷栅栏 超级好玩的一道题目