传送门:HDU - 3667
题目描述
There are N cities, and M directed roads connecting them. Now you want to transport K units of goods from city 1 to city N. There are many robbers on the road, so you must be very careful. The more goods you carry, the more dangerous it is. To be more specific, for each road i, there is a coefficient $a_i$. If you want to carry x units of goods along this road, you should pay $a_i * x^2$ dollars to hire guards to protect your goods. And what’s worse, for each road i, there is an upper bound $C_i$, which means that you cannot transport more than $C_i$ units of goods along this road. Please note you can only carry integral unit of goods along each road.
You should find out the minimum cost to transport all the goods safely.
输入描述
There are several test cases. The first line of each case contains three integers, N, M and K. (1 <= N <= 100, 1 <= M <= 5000, 0 <= K <= 100). Then M lines followed, each contains four integers ($u_i, v_i, a_i, C_i)$, indicating there is a directed road from city $u_i to v_i$, whose coefficient is $a_i$ and upper bound is $C_i$. $(1 <= u_i, v_i <= N, 0 < a_i <= 100, C_i <= 5)$
输出描述
Output one line for each test case, indicating the minimum cost. If it is impossible to transport all the K units of goods, output -1.
思路分析
很容易看出是道费用流,可以边的费用不是普通的,跟运送量有关系,直接建边跑的话是不行的。
所以可以这样考虑每一条边
- 假设有一条边是$u,v,2,3$
- 因为C最大只有5,所以我们可以拆边,拆成C条容量为1的边。
- 这条边的花费分别是:2,8,18。
- 第一条边花费为2,第二条边花费为8-2=6,第三条边花费为18-8=10。
- 那么可以将这条容量为3的边,拆成三条容量为1的边,权值分别是:1×1×1,2×2×2-2×1×1,2×3×3-2×2×2。
样例输入
2 1 2
1 2 1 2
2 1 2
1 2 1 1
2 2 2
1 2 1 2
1 2 2 2
样例输出
4
-1
3
AC代码
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <functional>
#if __cplusplus >= 201103L
#include <unordered_map>
#include <unordered_set>
#endif
#define ls ro<<1
#define fi first
#define se second
#define rs ro<<1|1
#define ll long long
#define pb push_back
#define vi vector<int>
#define lowbit(x) x&(-x)
#define pii pair<int,int>
#define lson ro<<1,l,mid
#define umap unordered_map
#define uset unordered_set
#define rson ro<<1|1,mid+1,r
#define mem(a,b) memset(a,b,sizeof(a))
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0);
using namespace std;
const int INF = 0x3f3f3f3f;
const int N=1e4+10;
const int M=1e6+10;
struct node {
int v,flow,w,next;
} e[M];
int head[N],cnt;
int n,m,s,t,k,maxflow,mincost,path[N],pre[N];
int dis[N],vis[N];
inline void ade(int u,int v,int f,int w) {
e[++cnt].v=v;
e[cnt].flow=f;
e[cnt].w=w;
e[cnt].next=head[u];
head[u]=cnt;
}
inline void add(int u,int v,int f,int w) {
ade(u,v,f,w);
ade(v,u,0,-w);
}
inline bool spfa() {
mem(dis,INF);
mem(vis,0);
mem(pre,-1);
dis[s]=0;
vis[s]=1;
queue<int>q;
q.push(s);
while(!q.empty()) {
int u=q.front();
vis[u]=0;
q.pop();
for(int i=head[u]; i; i=e[i].next) {
int v=e[i].v;
int w=e[i].w;
if(e[i].flow && dis[v]>dis[u]+w) {
dis[v]=dis[u]+w;
pre[v]=u;
path[v]=i;
if(!vis[v]) {
q.push(v);
vis[v]=1;
}
}
}
}
return pre[t]!=-1;
}
inline void EK() {
while(spfa()) {
int mi=INF;
for(int i=t; i!=s; i=pre[i])
mi=min(mi,e[path[i]].flow);
for(int i=t; i!=s; i=pre[i]) {
e[path[i]].flow-=mi;
e[path[i]^1].flow+=mi;
}
maxflow+=mi;
mincost+=dis[t]*mi;
}
}
inline void init() {
cnt=1;
mem(head,0);
maxflow=mincost=0;
s=0;
t=n+1;
add(s,1,k,0);
add(n,t,k,0);
}
int main() {
while(~scanf("%d %d %d",&n,&m,&k)) {
init();
while(m--){
int u,v,a,c;
scanf("%d %d %d %d",&u,&v,&a,&c);
for(int i=1;i<=c;i++)
add(u,v,1, a*(i*i-(i-1)*(i-1)) );
}
EK();
printf("%d\n",maxflow==k?mincost:-1);
}
return 0;
}