题意:

有N个城市,M条有向道路,要从1号城市运送K个货物到N号城市。

每条有向道路<u, v>运送费用和运送量的平方成正比,系数为ai

而且每条路最多运送Ci个货物,求最小费用。

分析:

拆边,每条边拆成费用为a, 3a, 5a的边,这样就能保证每条边的费用和流量的平方成正比。

因为最多运送K个货物,所以增加一个源点和城市1连一条容量为K费用为0的边。

跑一边最小费用最大流,如果满流才有解。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std; const int maxn = + ;
const int INF = 0x3f3f3f3f; struct Edge
{
int from, to, cap, flow, cost;
Edge(int u, int v, int cap, int flow, int cost):from(u), to(v), cap(cap), flow(flow), cost(cost) {}
}; int n, s, t, m, k;
vector<Edge> edges;
vector<int> G[maxn]; void init(int n)
{
for(int i = ; i < n; i++) G[i].clear();
edges.clear();
} void AddEdge(int u, int v, int cap, int cost)
{
edges.push_back(Edge(u, v, cap, , cost));
edges.push_back(Edge(v, u, , , -cost));
int m = edges.size();
G[u].push_back(m - );
G[v].push_back(m - );
} bool inq[maxn];
int p[maxn], d[maxn], a[maxn]; bool SPFA()
{
memset(d, 0x3f, sizeof(d));
d[s] = ;
queue<int> Q;
Q.push(s);
memset(inq, false, sizeof(inq));
inq[s] = true;
memset(p, -, sizeof(p));
a[s] = INF; while(!Q.empty())
{
int u = Q.front(); Q.pop(); inq[u] = false;
for(int i = ; i < G[u].size(); i++)
{
Edge& e = edges[G[u][i]];
int v = e.to;
if(e.cap > e.flow && d[u] + e.cost < d[v])
{
d[v] = d[u] + e.cost;
p[v] = G[u][i];
a[v] = min(a[u], e.cap - e.flow);
if(!inq[v]) { inq[v] = true; Q.push(v); }
}
}
} return d[t] < INF;
} int Maxf; int Mincost()
{
int cost = ;
Maxf = ;
while(SPFA())
{
Maxf += a[t];
cost += a[t] * d[t];
int u = t;
while(u != s)
{
edges[p[u]].flow += a[t];
edges[p[u]^].flow -= a[t];
u = edges[p[u]].from;
}
}
return cost;
} int main()
{
while(scanf("%d%d%d", &n, &m, &k) == )
{
init(n + ); s = , t = n;
AddEdge(s, , k, ); while(m--)
{
int u, v, a, C;
scanf("%d%d%d%d", &u, &v, &a, &C);
for(int i = ; i < C; i++)
AddEdge(u, v, , a * (i * + ));
} int cost = Mincost();
if(Maxf < k) puts("-1");
else printf("%d\n", cost);
} return ;
}

代码君

最新文章

  1. 嵌入式Linux驱动学习之路(十一)按键驱动-中断机制
  2. ubuntu下修改进入root用户和修改文件权限
  3. java线程小结3
  4. Spring @Transactional propagation 各个属性值的含义
  5. Linux unzip解压文件到某个目录下面
  6. css模仿表格 居中
  7. phpQuery—基于jQuery的PHP实现
  8. hdu 1250 Hat&#39;s Fibonacci
  9. 《Python基础教程(第二版)》学习笔记 -&gt; 第九章 魔法方法、属性和迭代器
  10. 用const取代宏定义更好的管理内存
  11. foxtable使用笔记
  12. 数字信号处理与音频处理(使用Audition)
  13. 关于异常的疑难解答:System.Runtime.InteropServices.COMException
  14. 启动(Startup)
  15. oracle乱码问题
  16. 51NOD 1584&#160;加权约数和 [莫比乌斯反演 转化 Trick]
  17. 安卓6.0新特性在Fragment申请运行时权限
  18. GC参考手册 —— GC 算法(基础篇)
  19. Set集合(scala)
  20. redis集群报错:(error) CLUSTERDOWN Hash slot not served

热门文章

  1. 3.Freshman阶段学习内容的确定
  2. Kendo MVVM 数据绑定(九) Text
  3. 关于一些Spring MVC控制器的参数注解总结
  4. Eclipse中添加MyEclipse插件
  5. redis的一些问题总结,转载自infoq
  6. Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] A A Problem about Polyline(数学)
  7. uva10129 PlayOnWords(并查集,欧拉回路)
  8. String中关于BeanFactory
  9. 功能强大的CURL
  10. 解决windows系统下打开应用弹出丢失libmysql.dll的问题