HDU3667 Transportation —— 最小费用流(费用与流量平方成正比)
题目链接: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
You should find out the minimum cost to transport all the goods safely.
1 2 1 2
2 1 2
1 2 1 1
2 2 2
1 2 1 2
1 2 2 2
-1
3
题解:
费用与流量平方成正比。详情在《训练指南》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 —— 最小费用流(费用与流量平方成正比)的更多相关文章
- hdu3667 Transportation 费用与流量平方成正比的最小流 拆边法+最小费用最大流
/** 题目:hdu3667 Transportation 拆边法+最小费用最大流 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3667 题意:n个城市由 ...
- HDU 3667.Transportation 最小费用流
Transportation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 资深阿里程序员一一为你解刨Web前端知识体系结构,付出与收获成正比!
只要接触过前端,都会指导web前端的知识主要由三部分组成:分别为静态html,样式css,动态javascript(简称js)这三大部分组成.其三部分组成的一个体系的复杂程度不亚于其他一门技术的复杂程 ...
- 【 UVALive - 5095】Transportation(费用流)
Description There are N cities, and M directed roads connecting them. Now you want to transport K un ...
- hdu 3667 /2010哈尔滨赛区H题 费用与流量为非线性关系/费用流
题意: 在一般费用流题目改动:路过某路,每x单位流量须要花费 ai*x^2(ai为给定的系数). 開始的的时候,一看仅仅只是是最后统计费用上在改动罢了,一看例子.发现根本没那么简单(ps:以后每次写程 ...
- ZOJ3231 Apple Transportation(最小费用流)
题目给你一棵苹果树,然后每个结点上有一定的苹果树,你要将苹果运输达到某个状态,使得均方差最小. 将苹果x个从a->b的花费是x*w,w是边权. 当时比赛的时候想的就是,最后达到的状态一定是sum ...
- 纹理特征描述之自相关函数法 纹理粗糙性与自相关函数的扩展成正比 matlab代码实现
图像中通常采用自相关函数作为纹理测度 自相关函数的定义为: 调用自定义函数 zxcor()对砖墙面和大理石面纹理进行分析: 自定义函数 zxcor(): function [epsilon,eta ...
- ACM技能表
看看就好了(滑稽) 数据结构 栈 栈 单调栈 队列 一般队列 优先队列/单调队列 循环队列 双端队列 链表 一般链表 循环链表 双向链表 块状链表 十字链表 邻接表/邻接矩阵 邻接表 邻接多重表 Ha ...
- HDU 3667 费用流 拆边 Transportation
题意: 有N个城市,M条有向道路,要从1号城市运送K个货物到N号城市. 每条有向道路<u, v>运送费用和运送量的平方成正比,系数为ai 而且每条路最多运送Ci个货物,求最小费用. 分析: ...
随机推荐
- Python入门--9--格式化
字符串格式化符号含义 符 号 说 明 %c 格式化字符及其ASCII码 %s 格式化字符串 %d 格式化整数 %o ...
- WAMP本地环境升级php版本--第二次尝试
wamp 环境下 php5.6.25 升级php7.1.17 实践 本文参考:https://www.cnblogs.com/hubaohua1588/p/6884146.html来进行操作. 1.从 ...
- HDU4850 构造一个长度为n的串,要求任意长度为4的子串不相同
n<=50W.(使用26个字母) 构造方法:26个,最多构造出26^4种不同的串,长度最长是26^4+3,大于是输出"impossble",用四维数组判重.每次向前构造一位( ...
- Jetson TK1 三:项目相关安装
ROS.QT.pyserial2.7.罗技手柄驱动.navigation.slam和rviz等 激光雷达IP设置,tk1对应的IP设置,tk1串口设置 一.安装ros参见官网 二.安装QT 百度QT官 ...
- 初学Java经典例子
我自己看的书的理解学习Java就是学习对象,就像谈恋爱,你对她多付出,收货就多(跑题了对象是啥??对象就是实体,通过类可以生成具有特定状态(或者叫属性)和行为或动作的实例,问题来了怎么创建? new一 ...
- pycharm、idea插件代理设置,插件安装
pycharm和idea都是intellij的,所以插件安装是设置代理方法相似, 以pycharm举例: 1.已经安装的插件列表: 2.查找要安装的插件,没有,会给出下载插件的链接地址: 3.打开链接 ...
- ngnix
nginx的平滑重启 博客分类: nginx nginx平滑重启 在研发过程中,修改nginx的配置文件nginx.conf是很平常的事,需要重启nginx.如果我们直接reload是有一定风险的, ...
- Android Dynamic Action(动态Action)—像访问网页一样地访问Activity
Android Dynamic Action,简称DA,是一种简便.可变Action的实现方案.DA框架的初衷是为了取代Context.startActivity的调用方式,使用建造者模式(Build ...
- 【转载】FAT12格式的引导程序(2)
1.用WinImage来写入到引导区的详细步骤: 启动WinImage后,打开“文件”菜单,单击菜单中的“打开”命令. 选择之前保存的磁盘镜像文件“boot.img”或者“boot.ima”. 打开 ...
- iOS--判断字符串NSString中数字、中文、大小写英文
iOS--判断字符串NSString中数字.中文.大小写英文 <iframe id="iframeu2051914_0" src="http://pos.bai ...