Description
给定一张有向图,每条边都有一个容量C和一个扩容费用W。这里扩容费用是指将容量扩大1所需的费用。求: 1、 在不扩容的情况下,1到N的最大流;
2、 将1到N的最大流增加K所需的最小扩容费用。
Input
输入文件的第一行包含三个整数N,M,K,表示有向图的点数、边数以及所需要增加的流量。
接下来的M行每行包含四个整数u,v,C,W,表示一条从u到v,容量为C,扩容费用为W的边。
Output
输出文件一行包含两个整数,分别表示问题1和问题2的答案。
Sample Input
5 8 2
1 2 5 8
2 5 9 9
5 1 6 2
5 1 1 8
1 2 8 7
2 5 4 9
1 2 1 1
1 4 2 1
Sample Output
13 19
HINT
30%的数据中,N<=100
100%的数据中,N<=1000,M<=5000,K<=10
题解
第一问不用说吧。。网络流模板大家都会我就不弄了
第二问,实际上就是在第一问的残余网络上重新建图,跑费用流
怎么建图??可以想到,如果最大流要+K,那么残余网络图中至少有一条权为K的增广路径。那么,我们在残留网络上建图。
ins(x,y,c,w)表示x~y有一条权为c,值为w的边。要不要删原图了??当然不用啦
跑spfa的时候,如果x->y原本有残余网络,那么一定会选择值较小的边走对吧。所以不能删原图。
至于x->y没有残余网络了,那就在新图上跑咯
顺带学了一下费用流模板
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
struct node{int x,y,c,d,next,other;}a[211000];int len,last[110000];
void ins(int x,int y,int c,int d)
{
int k1,k2;
k1=++len;
a[len].x=x;a[len].y=y;a[len].c=c;a[len].d=d;
a[len].next=last[x];last[x]=len;
k2=++len;
a[len].x=y;a[len].y=x;a[len].c=0;a[len].d=-d;
a[len].next=last[y];last[y]=len;
a[k1].other=k2;a[k2].other=k1;
}
int list[11000];
int head,tail,st,ed;
int n,m,upd;
int h[11000];
bool bt_h()
{
head=1;tail=2;
memset(h,0,sizeof(h));
h[st]=1;list[1]=st;
while(head!=tail)
{
int x=list[head];
for(int k=last[x];k;k=a[k].next)
{
int y=a[k].y;
if(h[y]==0 && a[k].c>0)
{
h[y]=h[x]+1;
list[tail++]=y;
}
}
head++;
}
if(h[ed]==0)return false;
return true;
}
int find_flow(int x,int f)
{
if(x==ed)return f;
int s=0,t;
for(int k=last[x];k;k=a[k].next)
{
int y=a[k].y;
if(h[y]==h[x]+1 && a[k].c>0 && s<f)
{
s+=(t=find_flow(y,min(a[k].c,f-s)));
a[k].c-=t;a[a[k].other].c+=t;
}
}
if(s==0)h[x]=0;
return s;
}
int pos[21000],tmp[21000];
int d[21000],v[21000];
bool spfa()
{
for(int i=1;i<=n+1;i++)d[i]=999999999;
d[st]=0;
memset(v,false,sizeof(v));v[st]=true;
list[1]=st;head=1;tail=2;
while(head!=tail)
{
int x=list[head];
for(int k=last[x];k;k=a[k].next)
{
int y=a[k].y;
if(a[k].c>0 && d[y]>d[x]+a[k].d)
{
pos[y]=x;tmp[y]=k;
d[y]=d[x]+a[k].d;
if(v[y]==false)
{
v[y]=true;
list[tail++]=y;
if(tail==n+2)tail=1;
}
}
}
head++;
if(head==n+2)head=1;
v[x]=false;
}
if(d[n+1]==999999999)return false;
return true;
}
int p[21000],q[21000],g[21000],co[21000];
int main()
{
scanf("%d%d%d",&n,&m,&upd);
len=0;memset(last,0,sizeof(last));
for(int i=1;i<=m;i++)
{
scanf("%d%d%d%d",&p[i],&q[i],&g[i],&co[i]);
ins(p[i],q[i],g[i],0);
}
st=1;ed=n;
int ans=0;
while(bt_h())ans+=find_flow(st,999999999);
printf("%d ",ans);
for(int i=1;i<=m;i++)ins(p[i],q[i],upd,co[i]);
ins(n,n+1,upd,0);
ans=0;
while(spfa())
{
int tp=n+1,minn=999999999;
while(tp!=st)
{
minn=min(minn,a[tmp[tp]].c);
tp=pos[tp];
}
tp=n+1;
while(tp!=st)
{
ans+=minn*a[tmp[tp]].d;
a[tmp[tp]].c-=minn;a[a[tmp[tp]].other].c+=minn;
tp=pos[tp];
}
}
printf("%d\n",ans);
return 0;
}