[ZJOI2010]network 网络扩容

来源:互联网 发布:申报教学软件 编辑:程序博客网 时间:2024/04/30 21:30

题目都告诉你了,很裸的网络流……

在第一问基础上

对于每条边,另外加一条带费用的边,容量只要大于等于k就行

在此基础上做一遍费用流

然后搞定……


PS:我做的时候脑残了退流的时候一个单位一个单位的退……


//Lib#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<ctime>#include<iostream>#include<algorithm>#include<vector>#include<string>#include<queue>using namespace std;//Macro#define rep(i,a,b) for(int i=a,tt=b;i<=tt;++i)#define drep(i,a,b) for(int i=a,tt=b;i>=tt;--i)#define erep(i,e,x) for(int i=x;i;i=e[i].next)#define irep(i,x) for(__typedef(x.begin()) i=x.begin();i!=x.end();i++)#define read() (strtol(ipos,&ipos,10))#define sqr(x) ((x)*(x))#define pb push_back#define PS system("pause");typedef long long ll;typedef pair<int,int> pii;const int oo=~0U>>1;const double inf=1e100;const double eps=1e-6;string name="",in=".in",out=".out";//Varstruct E{int node,next,cap,cost;}e[20008];struct ED{int a,b,c;}edge[5008];queue<int> q;int tot=1,n,m,k,st,ed,ans;int dis[1008],h[1008],pre[1008],pree[1008];bool vis[1008];void add(int a,int b,int c,int d){e[++tot].next=h[a];e[tot].node=b;e[tot].cap=c;e[tot].cost=d;h[a]=tot;e[++tot].next=h[b];e[tot].node=a;e[tot].cap=0;e[tot].cost=-d;h[b]=tot;}void Init(){scanf("%d%d%d",&n,&m,&k);st=1;ed=n;int a,b,c,d;rep(i,1,m){scanf("%d%d%d%d",&a,&b,&c,&d);add(a,b,c,0);edge[i].a=a;edge[i].b=b;edge[i].c=d;}}bool BFS(){memset(dis,-1,sizeof dis);q.push(ed);int u,v;bool flag=false;dis[ed]=0;while(!q.empty()){u=q.front();q.pop();erep(i,e,h[u])if(e[i^1].cap&&dis[v=e[i].node]==-1){dis[v]=dis[u]+1;q.push(v);if(v==st)flag=true;}}return flag;}int DFS(int u,int low){int ret=low,tmp,v;if(u==ed)return low;erep(i,e,h[u])if(e[i].cap&&dis[u]==dis[v=e[i].node]+1){tmp=DFS(v,min(low,e[i].cap));e[i].cap-=tmp;e[i^1].cap+=tmp;low-=tmp;if(!low)break;}if(ret==low)dis[u]=-1;return ret-low;}void Dinic(){int ans=0,flow;while(BFS())while(flow=DFS(st,oo))ans+=flow;printf("%d ",ans);}void SPFA(){memset(dis,55,sizeof dis);q.push(st);int u,v;dis[st]=0;while(!q.empty()){u=q.front();q.pop();vis[u]=false;erep(i,e,h[u])if(e[i].cap&&dis[v=e[i].node]>dis[u]+e[i].cost){dis[v]=dis[u]+e[i].cost;pre[v]=u;pree[v]=i;if(!vis[v])vis[v]=true,q.push(v);}}}int Flow(){int tmp=ed,ret=0,minn=k;while(tmp!=st){ret+=e[pree[tmp]].cost;minn=min(minn,e[pree[tmp]].cap);tmp=pre[tmp];}tmp=ed;while(tmp!=st){e[pree[tmp]].cap-=minn;e[pree[tmp]^1].cap+=minn;tmp=pre[tmp];}k-=minn;return ret*minn;}void Cost_Flow(){rep(i,1,m)add(edge[i].a,edge[i].b,k,edge[i].c);while(k){SPFA();ans+=Flow();}cout<<ans<<endl;}int main(){//freopen((name+in).c_str(),"r",stdin);//freopen((name+out).c_str(),"w",stdout);Init();Dinic();Cost_Flow();return 0;}




原创粉丝点击