LibreOJ #102. 最小费用流
二次联通门 : LibreOJ #102. 最小费用流
/*
LibreOJ #102. 最小费用流 Spfa跑花费
记录路径
倒推回去 */
#include <cstring>
#include <cstdio>
#include <queue> #define Max 10000 void read (int &now)
{
now = ;
register char word = getchar ();
while (word > '' || word < '')
word = getchar ();
while (word >= '' && word <= '')
{
now = now * + word - '';
word = getchar ();
}
} inline int min (int a, int b)
{
return a < b ? a : b;
} class Cost_Flow_Type
{ private : int __to[Max * ];
int __next[Max * ];
int __flow[Max * ], __cost[Max * ]; int edge_list[Max];
int Edge_Count; int dis[Max], can_flow[Max];
bool visit[Max];
int pre[Max]; public : Cost_Flow_Type ()
{
Edge_Count = ;
} inline void Insert_edge (int from, int to, int flow, int cost)
{
Edge_Count ++; __to[Edge_Count] = to;
__next[Edge_Count] = edge_list[from];
edge_list[from] = Edge_Count; __flow[Edge_Count] = flow;
__cost[Edge_Count] = cost; Edge_Count ++; __to[Edge_Count] = from;
__next[Edge_Count] = edge_list[to];
edge_list[to] = Edge_Count; __flow[Edge_Count] = ;
__cost[Edge_Count] = -cost;
} bool Spfa (int Start, int End)
{
memset (dis, 0x3f, sizeof dis);
memset (visit, false, sizeof visit); std :: queue <int> Queue;
register int now; visit[Start] = true;
can_flow[Start] = dis[];
pre[Start] = ; for (Queue.push (Start), dis[Start] = ; !Queue.empty (); Queue.pop ())
{
now = Queue.front (); visit[now] = false; for (int i = edge_list[now]; i; i = __next[i])
if (__flow[i] && dis[__to[i]] > dis[now] + __cost[i])
{
dis[__to[i]] = dis[now] + __cost[i];
pre[__to[i]] = i; can_flow[__to[i]] = min (can_flow[now], __flow[i]); if (!visit[__to[i]])
{
Queue.push (__to[i]);
visit[__to[i]] = true;
}
}
} return dis[End] < dis[];
} void Get_Cost_Flow (int Start, int End)
{
int res = ;
int Cost_ = ;
for (int x; Spfa (Start, End); )
{
x = can_flow[End];
for (int i = End; i != Start; i = __to[pre[i] ^ ])
{
__flow[pre[i]] -= x;
__flow[pre[i] ^ ] += x;
}
res += x;
Cost_ += dis[End] * x;
} printf ("%d %d", res, Cost_);
}
}; int N, M; Cost_Flow_Type Make; int main (int argc, char *argv[])
{
int x, y, z, Flandre; for (read (N), read (M); M --; )
{
read (x);
read (y);
read (z);
read (Flandre); Make.Insert_edge (x, y, z, Flandre);
} Make.Get_Cost_Flow (, N); return ;
}
LibreOJ #102. 最小费用流的更多相关文章
- loj 102 最小费用流
补一发费用流的代码 %%%棒神 #include<iostream> #include<cstdio> #include<cstring> #include< ...
- LibreOJ 题解汇总
目录 #1. A + B Problem #2. Hello, World! #3. Copycat #4. Quine #7. Input Test #100. 矩阵乘法 #101. 最大流 #10 ...
- Entity Framework 6 Recipes 2nd Edition(10-2)译 -> 返回输出参数
10-2. 返回输出参数 问题 想获取存储过程里的一个或多个输出参数的值 解决方案 假设我们有一个像Figure 10-1所示的,出租车辆与租金收入的模型 Figure 10-1.出租车辆与租金收入的 ...
- 【安卓】aidl.exe E 10744 10584 io_delegate.cpp:102] Error while creating directories: Invalid argument
这几天在使用.aidl文件的时候eclipse的控制台总是爆出如下提示: aidl.exe E 10744 10584 io_delegate.cpp:102] Error while creatin ...
- POJ2195 最小费用流
题目:http://poj.org/problem?id=2195 处理出每个人到每个门的曼哈顿距离,分别建立容量为1费用为曼哈顿距离的边,在源点和每个人人之间建立容量为1费用为0的边,在门和汇点之间 ...
- HDU 4067 hdoj 4067 Random Maze 最小费用流
给出n个点,m条边,入口s和出口t,对于每条边有两个值a,b,如果保留这条边需要花费:否则,移除这条边需要花费b. 题目要求用最小费用构造一个有向图满足以下条件: 1.只有一个入口和出口 2.所有路都 ...
- POJ 2516:Minimum Cost(最小费用流)
https://vjudge.net/problem/11079/origin 题意:有N个商店和M个供应商和K种物品,每个商店每种物品有一个需求数,每个供应商每种物品有一个供应量,供应商到商店之间的 ...
- POJ-2175 Evacuation Plan 最小费用流、负环判定
题意:给定一个最小费用流的模型,根据给定的数据判定是否为最优解,如果不为最优解则给出一个比给定更优的解即可.不需要得出最优解. 解法:由给定的数据能够得出一个残图,且这个图满足了最大流的性质,判定一个 ...
- Going Home (hdu 1533 最小费用流)
集训的图论都快结束了,我才看懂了最小费用流,惭愧啊. = = 但是今天机械键盘到了,有弄好了自行车,好高兴\(^o^)/~ 其实也不是看懂,就会套个模板而已.... 这题最重要的就是一个: 多组输入一 ...
随机推荐
- SAS学习笔记39 MINOPERATOR | NOMINOPERATOR
MINOPERATOR(可理解为Macro IN Operator的简写,帮助大家记忆)系统选项用于控制是否在宏程序中是否可以用IN操作符或#操作符,默认值为NOMINOPERATOR.需要注意的是, ...
- 为什么用JS取不到cookie的值?解决方法如下!
注意:cookie是基于域名来储存的.要放到测试服务器上或者本地localhost服务器上才会生效.cookie具有不同域名下储存不可共享的特性.单纯的本地一个html页面打开是无效的. 明明在浏览中 ...
- 烧脑!CMU、北大等合著论文真的找到了神经网络的全局最优解
烧脑!CMU.北大等合著论文真的找到了神经网络的全局最优解 机器之心 已认证的官方帐号 811 人赞同了该文章 选自arXiv,作者:Simon S. Du.Jason D. Lee.Haochu ...
- Java MergeSort
Java MergeSort /** * <html> * <body> * <P> Copyright 1994-2018 JasonInternational ...
- 解决https 请求过程中SSL问题
最近一个项目中用到了https的请求,在实际调用过程中发现之前的http方法不支持https,调用一直报错. 查询了一下,添加几行代码解决问题. public string HttpPost(stri ...
- Map集合中key不存在时使用toString()方法、valueOf()方法和强制转换((String))之间的区别
1.toString()方法 底层代码 public String toString() { return this; } 其返回值为String类型的字符串本身 Map<String, Obj ...
- epoll、mysql概念及简单操作
epoll 程序阻塞的过程 假设我们目前运行了三个进程A B C ,如果他们都在处于运行态,那就会被加到一个运行队列中 进程A正在运行socket程序 在linux中有句话,万物皆文件,socket对 ...
- CVE-2018-2879 - anniversary
For the anniversary of the discovery of CVE-2018-2879 by Sec Consult (https://sec-consult.com/en/blo ...
- python2.7.5安装docker-compose的方法
yum -y install epel-release && yum install -y python-pip && pip install --upgrade pi ...
- leetcode-64. 最小路径和 · vector + DP
题面 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right wh ...