题目链接: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);
}
}

HDU3667 Transportation —— 最小费用流(费用与流量平方成正比)的更多相关文章

  1. hdu3667 Transportation 费用与流量平方成正比的最小流 拆边法+最小费用最大流

    /** 题目:hdu3667 Transportation 拆边法+最小费用最大流 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3667 题意:n个城市由 ...

  2. HDU 3667.Transportation 最小费用流

    Transportation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. 资深阿里程序员一一为你解刨Web前端知识体系结构,付出与收获成正比!

    只要接触过前端,都会指导web前端的知识主要由三部分组成:分别为静态html,样式css,动态javascript(简称js)这三大部分组成.其三部分组成的一个体系的复杂程度不亚于其他一门技术的复杂程 ...

  4. 【 UVALive - 5095】Transportation(费用流)

    Description There are N cities, and M directed roads connecting them. Now you want to transport K un ...

  5. hdu 3667 /2010哈尔滨赛区H题 费用与流量为非线性关系/费用流

    题意: 在一般费用流题目改动:路过某路,每x单位流量须要花费 ai*x^2(ai为给定的系数). 開始的的时候,一看仅仅只是是最后统计费用上在改动罢了,一看例子.发现根本没那么简单(ps:以后每次写程 ...

  6. ZOJ3231 Apple Transportation(最小费用流)

    题目给你一棵苹果树,然后每个结点上有一定的苹果树,你要将苹果运输达到某个状态,使得均方差最小. 将苹果x个从a->b的花费是x*w,w是边权. 当时比赛的时候想的就是,最后达到的状态一定是sum ...

  7. 纹理特征描述之自相关函数法 纹理粗糙性与自相关函数的扩展成正比 matlab代码实现

    图像中通常采用自相关函数作为纹理测度 自相关函数的定义为: ​ 调用自定义函数 zxcor()对砖墙面和大理石面纹理进行分析: 自定义函数 zxcor(): function [epsilon,eta ...

  8. ACM技能表

    看看就好了(滑稽) 数据结构 栈 栈 单调栈 队列 一般队列 优先队列/单调队列 循环队列 双端队列 链表 一般链表 循环链表 双向链表 块状链表 十字链表 邻接表/邻接矩阵 邻接表 邻接多重表 Ha ...

  9. HDU 3667 费用流 拆边 Transportation

    题意: 有N个城市,M条有向道路,要从1号城市运送K个货物到N号城市. 每条有向道路<u, v>运送费用和运送量的平方成正比,系数为ai 而且每条路最多运送Ci个货物,求最小费用. 分析: ...

随机推荐

  1. Spring-IOC源码解读3-依赖注入

    当容器已经载入了BeanDefinition的信息完成了初始化,我们继续分析依赖注入的原理,需要注意的是依赖注入是用户第一次向IOC容器获取Bean的时候发生的,这里有个例外,那就是如果用户在Bean ...

  2. 军训分批(codevs 2751)

    题目描述 Description 某学校即将开展军训.共有N个班级. 前M个优秀班级为了保持学习优势,必须和3位任课老师带的班级同一批. 问共有几批? 输入描述 Input Description N ...

  3. virtualbox中centos虚拟机网络配置

    本文讲述的是如何在Oracle VM VirtualBox安装的CentOS虚拟机中进行网络配置,使得虚拟机可以访问宿主主机,也能访问外网,宿主主机可以访问虚拟机,虚拟机之间也可以相互访问. 在Vir ...

  4. LeetCode OJ--Search in Rotated Sorted Array

    http://oj.leetcode.com/problems/search-in-rotated-sorted-array/ 转换了一次的有序数组,进行类似二分查找. 从begin到mid和从mid ...

  5. LightOJ1234 Harmonic Number 调和级数求和

    [题目] [预备知识] ,其中r是欧拉常数,const double r= 0.57721566490153286060651209; 这个等式在n很大 的时候 比较精确. [解法]可以在 n较小的时 ...

  6. Codeforces 12 D Ball

    Discription N ladies attend the ball in the King's palace. Every lady can be described with three va ...

  7. delphi函数大全

    delphi函数大全Abort                 函数    引起放弃的意外处理Abs                   函数    绝对值函数AddExitProc          ...

  8. Webstorm上面通过babel将es6转化为es5

    1.首先创建一个新的文件夹(名为do);2.然后再根目录下面新建一个package.json,只需要写明两个属性即name和version,这个没有必须要和下面我写的一致,可以自行定义. { &quo ...

  9. TFTP服务器

    为什么要学习有关TFTP服务器的安装及配置呢?主要是为了后续学习有关linux系统的无人值守安装做准备. TFTP简单文件传输协议,使用UDP的69端口.主要提供文件的上传和下载,TFTP一般是适用于 ...

  10. 生活娱乐 WIFI机器人(某机器发烧友自己动手做一台)

    某机器发烧友自己动手做一台WIFI机器人,以下是这位发烧友的自述!让我们一起来分享他的劳动成果-- 在经历了十多天的疯狂淘宝.组装.调试.拆卸.再组装.再调试的过程后,俺的Wifi Robot终于于2 ...