您的位置:首页 > 其它

bzoj1877 [SDOI2009]晨跑(费用流)

2017-12-09 22:29 405 查看
拆点建图费用流即可。

zkw费用流好像跑的很慢???看样子明天还要学习一下spfa的怎么写orz

upd:spfa的费用流这么好写???我为什么要学zkw???反正都会被卡???跳槽了跳槽了orz

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 510
#define M 210010
#define inf 0x3f3f3f3f
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x*f;
}
int n,m,h
,num=1,mncost=0,pay=0,T,mxflow=0;
bool vis
;
struct edge{
int to,next,w,c;
}data[M<<1];
inline void add(int x,int y,int w,int c){
data[++num].to=y;data[num].next=h[x];h[x]=num;data[num].w=w;data[num].c=c;
data[++num].to=x;data[num].next=h[y];h[y]=num;data[num].w=0;data[num].c=-c;
}
inline int dinic(int x,int low){
vis[x]=1;if(x==T){mncost+=low*pay;mxflow+=low;return low;}int tmp=low;
for(int i=h[x];i;i=data[i].next){
int y=data[i].to;if(vis[y]||data[i].c||!data[i].w) continue;
int res=dinic(y,min(tmp,data[i].w));
tmp-=res;data[i].w-=res;data[i^1].w+=res;
if(!tmp) return low;
}return low-tmp;
}
inline bool label(){
int d=inf;
for(int x=1;x<=n<<1;++x){
if(!vis[x]) continue;
for(int i=h[x];i;i=data[i].next){
int y=data[i].to;if(vis[y]||!data[i].w) continue;
d=min(data[i].c,d);
}
}if(d==inf) return 0;
for(int x=1;x<=n<<1;++x){
if(!vis[x]) continue;
for(int i=h[x];i;i=data[i].next) data[i].c-=d,data[i^1].c+=d;
}pay+=d;return 1;
}
int main(){
//  freopen("a.in","r",stdin);
n=read();m=read();T=n;
while(m--){
int x=read(),y=read(),c=read();add(x+n,y,1,c);
}for(int i=2;i<=n;++i) add(i,i+n,1,0);add(1,1+n,inf,0);
do do memset(vis,0,sizeof(vis));while(dinic(1,inf));while(label());
printf("%d %d\n",mxflow,mncost);
return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 410
#define M 21010
#define inf 0x3f3f3f3f
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x*f;
}
int n,m,h
,num=1,mncost=0,T,mxflow=0,dis
,path
;
bool inq
;
struct edge{
int to,next,w,c;
}data[M<<1];
inline void add(int x,int y,int w,int c){
data[++num].to=y;data[num].next=h[x];h[x]=num;data[num].w=w;data[num].c=c;
data[++num].to=x;data[num].next=h[y];h[y]=num;data[num].w=0;data[num].c=-c;
}
inline bool spfa(){
queue<int>q;memset(dis,inf,sizeof(dis));memset(path,0,sizeof(path));
q.push(1+n);inq[1+n]=1;dis[1+n]=0;
while(!q.empty()){
int x=q.front();q.pop();inq[x]=0;
for(int i=h[x];i;i=data[i].next){
int y=data[i].to;if(!data[i].w) continue;
if(dis[x]+data[i].c<dis[y]){
dis[y]=dis[x]+data[i].c;path[y]=i;
if(!inq[y]) q.push(y),inq[y]=1;
}
}
}return path[T];
}
int main(){
//  freopen("a.in","r",stdin);
n=read();m=read();T=n;
while(m--){
int x=read(),y=read(),c=read();add(x+n,y,1,c);
}for(int i=2;i<=n;++i) add(i,i+n,1,0);
while(spfa()){
int low=inf,now=T;
while(path[now]) low=min(low,data[path[now]].w),now=data[path[now]^1].to;
mxflow+=low;mncost+=low*dis[T];now=T;
while(path[now]) data[path[now]].w-=low,data[path[now]^1].w+=low,now=data[path[now]^1].to;
}
printf("%d %d\n",mxflow,mncost);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: