题目链接:https://vjudge.net/problem/HDU-3667

Transportation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3083    Accepted Submission(s): 1341

Problem Description
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 ai. If you want to carry x units of goods along this road, you should pay ai * x2 dollars to hire guards to protect your goods. And what’s worse, for each road i, there is an upper bound Ci, which means that you cannot transport more than Ci 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. 
 
Input
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 (ui, vi, ai, Ci), indicating there is a directed road from city ui to vi, whose coefficient is ai and upper bound is Ci. (1 <= ui, vi <= N, 0 < ai <= 100, Ci <= 5)
 
Output
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.

 
Sample Input
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
 
Sample Output
4
-1
3
 
Source
 
Recommend
lcy

题解:

费用与流量平方成正比。详情在《训练指南》P366 。主要方法是拆边。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int MAXM = 1e5+;
const int MAXN = 1e2+; struct Edge
{
int to, next, cap, flow, cost;
}edge[MAXM];
int tot, head[MAXN];
int pre[MAXN], dis[MAXN];
bool vis[MAXN];
int N; void init(int n)
{
N = n;
tot = ;
memset(head, -, sizeof(head));
} void add(int u, int v, int cap, int cost)
{
edge[tot].to = v; edge[tot].cap = cap; edge[tot].cost = cost;
edge[tot].flow = ; edge[tot].next = head[u]; head[u] = tot++;
edge[tot].to = u; edge[tot].cap = ; edge[tot].cost = -cost;
edge[tot].flow = ; edge[tot].next = head[v]; head[v] = tot++;
} bool spfa(int s, int t)
{
queue<int>q;
for(int i = ; i<N; i++)
{
dis[i] = INF;
vis[i] = false;
pre[i] = -;
} dis[s] = ;
vis[s] = true;
q.push(s);
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = false;
for(int i = head[u]; i!=-; i = edge[i].next)
{
int v = edge[i].to;
if(edge[i].cap>edge[i].flow && dis[v]>dis[u]+edge[i].cost)
{
dis[v] = dis[u]+edge[i].cost;
pre[v] = i;
if(!vis[v])
{
vis[v] = true;
q.push(v);
}
}
}
}
if(pre[t]==-) return false;
return true;
} int minCostMaxFlow(int s, int t, int &cost)
{
int flow = ;
cost = ;
while(spfa(s,t))
{
int Min = INF;
for(int i = pre[t]; i!=-; i = pre[edge[i^].to])
{
if(Min>edge[i].cap-edge[i].flow)
Min = edge[i].cap-edge[i].flow;
}
for(int i = pre[t]; i!=-; i = pre[edge[i^].to])
{
edge[i].flow += Min;
edge[i^].flow -= Min;
cost += edge[i].cost*Min;
}
flow += Min;
}
return flow;
} int main()
{
int n, m, K;
while(scanf("%d%d%d", &n, &m, &K)!=EOF)
{
init(n+);
for(int i = ; i<=m; i++)
{
int u, v, c, a;
scanf("%d%d%d%d", &u, &v, &a, &c);
for(int j = ; j<=c; j++) //拆边
add(u, v, , (j*-)*a); //拆成费用为1 3 5 7 9……的边,每条边的容量为1
}
add(, , K, ); int min_cost;
int start = , end = n;
int max_flow = minCostMaxFlow(start, end, min_cost); if(max_flow<K) printf("-1\n");
else printf("%d\n", min_cost);
}
}

最新文章

  1. Xcode及obj-c的基础知识
  2. windows 录音程序(一)
  3. HDOJ1001-1005题解
  4. 我的android学习经历
  5. 今天说一下Order by 这个常规东西~
  6. javascript中的true和false
  7. 组合数学 + STL --- 利用STL生成全排列
  8. 2015老男孩Python培训第八期视频教程
  9. uCGUI窗口重绘代码分析
  10. SPSS方差分析
  11. JAVA中子类会不会继承父类的构造方法
  12. android使用ViewPager实现欢迎引导页
  13. Reactor和Proactor模式
  14. monitor.go
  15. js 时间格式,加减
  16. 5.Git基础-撤销操作、标签的使用、Git别名
  17. 域名注册域名解析域名绑定 dns服务器解析 域名记录的添加 记录类型含义@ www 访问域名请求过程
  18. (二叉树 BFS) leetcode102. Binary Tree Level Order Traversal
  19. 从客户端(ASPxFormLayout1$txtRule=&quot;&lt;YYYY&gt;&lt;MM&gt;&lt;DD&gt;&lt;XXXX&gt;&quot;)中检测到有潜在危险的 Request.Form 值
  20. 页面生命周期里面还有很东西,如PageHandlerFactory等等这些东东也够吃一壶的,发现每走到一个领域,发现要学的东西实在是太多太多啦,总感觉自己所学的东西只是沧海一粟,走过了这道坎,又是一片海洋,我只能呐喊:生命永不止息,学海无涯----够用就好。

热门文章

  1. springboot开发 第一个案例之hello,world!
  2. Codeforces963D. Frequency of String
  3. toolbarlite随笔之插件的闭包写法
  4. MysqL5.7在使用mysqldump命令备份数据库报错:mysqldump: [Warning] Using a password on the command line interface can be insecure.
  5. Python入门--14--字典
  6. asp.net开发的调试方法集合
  7. 使用SourceTree 来管理 Gitcafe 的Pages 发布Blog!
  8. [React] Persist Form Data in React and Formik with formik-persist
  9. Java并发编程(三)volatile域
  10. C#读取指定路径下的Config配置文件