首页 > 代码库 > BZOJ 2324 (有上下界的)费用流
BZOJ 2324 (有上下界的)费用流
思路:
先跑一遍Floyd 更新的时候map[i][j]=map[i][k]+map[k][j] k需要小于i或j
正常建边:
把所有点 拆点-> i,i+n
add(x,y,C,E)表示x->y建边 话费为C 容量为E
add(S,0,0,k)
add(i,j+n,map[i][j],1)
add(j+n,j,0,0x3f3f3f3f)->这条边下界为1
add(j,T,0,1)
这个时候我们有两种方法
1.套用有上下界的网络流
2.把add(j+n,j,0,0x3f3f3f3f)这条边拆成
add(j+n,j,-0x3f3f3f3f,1),add(j+n,j,0,0x3f3f3f3f)两条边
我都写了一遍 为啥时间差那么多....
//By SiriusRen#include <queue>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define int long long#define mem(x,y) memset(x,y,sizeof(x))#define N 666666int map[155][155],xx,yy,zz,n,m,k,vis[N],with[N],dis[N],minn[N];int first[666],next[N],v[N],edge[N],cost[N],tot,S,T,jy,ans;void Add(int x,int y,int C,int E){edge[tot]=E,cost[tot]=C,v[tot]=y,next[tot]=first[x],first[x]=tot++;}void add(int x,int y,int C,int E){Add(x,y,C,E),Add(y,x,-C,0);}bool tell(){ mem(vis,0),mem(with,0),mem(dis,0x3f),mem(minn,0x3f); queue<int>q;q.push(S),dis[S]=0; while(!q.empty()){ int t=q.front();q.pop(),vis[t]=0; for(int i=first[t];~i;i=next[i]) if(dis[v[i]]>dis[t]+cost[i]&&edge[i]){ dis[v[i]]=dis[t]+cost[i],minn[v[i]]=min(minn[t],edge[i]),with[v[i]]=i; if(!vis[v[i]])vis[v[i]]=1,q.push(v[i]); } }return dis[T]<=0x3f3f3f3fll;}int zeng(){ for(int i=T;i!=S;i=v[with[i]^1])edge[with[i]]-=minn[T],edge[with[i]^1]+=minn[T]; return dis[T]*minn[T];}signed main(){ mem(map,0x3f),mem(first,-1); scanf("%lld%lld%lld",&n,&m,&k); T=2*n+2,S=2*n+1; for(int i=1;i<=n;i++)map[i][i]=0; for(int i=1;i<=m;i++){ scanf("%lld%lld%lld",&xx,&yy,&zz); map[xx][yy]=min(map[xx][yy],zz),map[yy][xx]=map[xx][yy]; } for(int kk=0;kk<=n;kk++) for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) if(kk<=i||kk<=j)map[i][j]=min(map[i][j],map[i][kk]+map[kk][j]); add(S,0,0,k); for(int j=1;j<=n;j++)add(j+n,j,-0x3f3f3f3f,1),add(j+n,j,0,0x3f3f3f3f),add(j,T,0,1); for(int i=0;i<=n;i++) for(int j=i+1;j<=n;j++) add(i,j+n,map[i][j],1); while(tell())ans+=zeng(); printf("%lld\n",ans+0x3f3f3f3f*n);}
//By SiriusRen#include <queue>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define int long long#define mem(x,y) memset(x,y,sizeof(x))#define N 666666int map[155][155],xx,yy,zz,n,m,k,vis[N],with[N],dis[N],minn[N];int first[666],next[N],v[N],edge[N],cost[N],tot,S,T,jy,ans;void Add(int x,int y,int C,int E){edge[tot]=E,cost[tot]=C,v[tot]=y,next[tot]=first[x],first[x]=tot++;}void add(int x,int y,int C,int E){Add(x,y,C,E),Add(y,x,-C,0);}bool tell(){ mem(vis,0),mem(with,0),mem(dis,0x3f),mem(minn,0x3f); queue<int>q;q.push(S),dis[S]=0; while(!q.empty()){ int t=q.front();q.pop(),vis[t]=0; for(int i=first[t];~i;i=next[i]) if(dis[v[i]]>dis[t]+cost[i]&&edge[i]){ dis[v[i]]=dis[t]+cost[i],minn[v[i]]=min(minn[t],edge[i]),with[v[i]]=i; if(!vis[v[i]])vis[v[i]]=1,q.push(v[i]); } }return dis[T]<=0x3f3f3f3fll;}int zeng(){ for(int i=T;i!=S;i=v[with[i]^1])edge[with[i]]-=minn[T],edge[with[i]^1]+=minn[T]; return dis[T]*minn[T];}signed main(){ mem(map,0x3f),mem(first,-1); scanf("%lld%lld%lld",&n,&m,&k); T=2*n+2,S=2*n+1; for(int i=1;i<=n;i++)map[i][i]=0; for(int i=1;i<=m;i++){ scanf("%lld%lld%lld",&xx,&yy,&zz); map[xx][yy]=min(map[xx][yy],zz),map[yy][xx]=map[xx][yy]; } for(int kk=0;kk<=n;kk++) for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) if(kk<=i||kk<=j)map[i][j]=min(map[i][j],map[i][kk]+map[kk][j]); add(S,0,0,k); for(int i=1;i<=n;i++)add(S,i,0,1),add(i+n,T,0,1); for(int i=0;i<=n;i++) for(int j=i+1;j<=n;j++) add(i,j+n,map[i][j],1); while(tell())ans+=zeng(); printf("%lld\n",ans);}
BZOJ 2324 (有上下界的)费用流
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。