题解 UVA1486 【Transportation】

$Description$

某国有$n(n\leqslant 100)$座城市,由$m(m\leqslant 5000)$条单向道路相连。你希望从城市$1$运送$k(k\leqslant 100)$单位货物到城市$n$,这些道路并不安全,有很多强盗,所以你需要雇佣保镖来做护卫。每条道路都有一个危险系$a_i(a_i\leqslant 100),$如果你带着$x$个单位的货物通过,需要给保镖$a_i\times x^{2}$的佣金,保镖才会保证你的安全。每条道路都有一个限制,最多能运送$c_i(c_i\leqslant 5)$的货物。现在问,在能完成运送$x$个单位的货物到$n$号城市的情况下最小的花费,如果送不到,则输出$-1$。

$Solution$

很容易看出是费用流的做法,但是我们发现对于不同的流的大小,费用并不相同。由于题目给出$c_i\leqslant 5$,我们考虑拆边。

我们将每条边拆成$c_i$条边,分别标成$e_{i,1\sim c_{i,j}}$.

对于一条边$i$。

我们把$e_{i,1}$的费用设成$a_i\times 1=a_i\times (1^2-0^2)$

把$e_{i,2}$的费用设成$a_i\times 3=a_i\times (2^2-1^2)$

把$e_{i,3}$的费用设成$a_i\times 5=a_i\times (3^2-2^2)$

把$e_{i,4}$的费用设成$a_i\times 7=a_i\times (4^2-3^2)$

把$e_{i,5}$的费用设成$a_i\times 9=a_i\times (5^2-4^2)$

我们开始模拟边$i$的每种流量对应的费用,设$w_i~$为边$i$的费用。

假设流量为$0$,费用显然$=0$,符合题意

假设流量为$1$,费用显然$=w_{e_{i,1}}$

$\qquad\qquad\qquad\qquad\, =a_{i}\times 1$

$\qquad\qquad\qquad\qquad\, =a_{i}\times 1^2$

假设流量为$2$,费用显然$=w_{e_{i,1}}$$+w_{e_{i,2}}$

$\qquad\qquad\qquad\qquad\, =a_{i}\times 1+a_i\times 3$

$\qquad\qquad\qquad\qquad\, =a_{i}\times 2^2$

假设流量为$3$,费用显然$=w_{e_{i,1}}+w_{e_{i,2}}+w_{e_{i,3}}$

$\qquad\qquad\qquad\qquad\, =a_{i}\times 1+a_i\times 3+a_i\times 5$

$\qquad\qquad\qquad\qquad\, =a_{i}\times 3^2$

假设流量为$4$,费用显然$=w_{e_{i,1}}+w_{e_{i,2}}+w_{e_{i,3}}+w_{e_{i,4}}$

$\qquad\qquad\qquad\qquad\, =a_{i}\times 1+a_i\times 3+a_i\times 5+a_i\times 7$

$\qquad\qquad\qquad\qquad\, =a_{i}\times 4^2$

假设流量为$5$,费用显然$=w_{e_{i,1}}+w_{e_{i,2}}+w_{e_{i,3}}+w_{e_{i,4}}+w_{e_{i,5}}$

$\qquad\qquad\qquad\qquad\, =a_{i}\times 1+a_i\times 3+a_i\times 5+a_i\times 7+a_i\times 9$

$\qquad\qquad\qquad\qquad\, =a_{i}\times 5^2$

这样拆边显然是对的。然后源点$s$连点$1$,容量为$k$,费用为$0$,点$n$连汇点$t$,容量为$k$,费用为$0$,其他边进行拆边,最后跑最小费用最大流即可。

$Code$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define N 400000
using namespace std;
struct node{
int to,dis,w,next;
}e[1070000];
const int d[]={0,1,3,5,7,9};
inline int read(){
int x=0,w=0;char ch=getchar();
while (!isdigit(ch))w|=ch=='-',ch=getchar();
while (isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return w?-x:x;
}
int cnt=1,head[N],flow[N],pre[N],pri[N],inque[N],dis[N],cost,n,m,k,s,t;
inline void add(int u,int v,int d,int w){
e[++cnt].to=v;
e[cnt].dis=d;
e[cnt].w=w;
e[cnt].next=head[u];
head[u]=cnt;
e[++cnt].to=u;
e[cnt].dis=0;
e[cnt].w=-w;
e[cnt].next=head[v];
head[v]=cnt;
}
bool spfa(){
memset(dis,0x3f,sizeof(dis));
memset(flow,0x3f,sizeof(flow));
queue<int>q;q.push(s);
dis[s]=0;
while (!q.empty()){
int u=q.front();q.pop();inque[u]=0;
for (int i=head[u];i;i=e[i].next){
int v=e[i].to;
if (e[i].dis&&dis[v]>dis[u]+e[i].w){
dis[v]=dis[u]+e[i].w;
pre[v]=u;pri[v]=i;flow[v]=min(flow[u],e[i].dis);
if (!inque[v])
q.push(v),inque[v]=1;
}
}
}
return dis[t]<inf;
}
int mcmf(){
int res=0;
while (spfa()){
res+=flow[t];
cost+=flow[t]*dis[t];
for (int now=t;now!=s;now=pre[now]){
e[pri[now]].dis-=flow[t];
e[pri[now]^1].dis+=flow[t];
}
}
return res;
}
int main(){
while (scanf("%d%d%d",&n,&m,&k)!=EOF){
s=0,t=n+1;
memset(head,0,sizeof(head));cnt=1;cost=0;
for (int i=1;i<=m;++i){
int u=read(),v=read(),a=read(),c=read();
for (int i=1;i<=c;++i)
add(u,v,1,a*d[i]);
}
add(s,1,k,0);add(n,t,k,0);
printf("%d\n",mcmf()==k?cost:-1);
}
return 0;
}