UVA 10594 Data Flow
无向图费用流
还有一段话摘自别人博客
这道题是无向图的最小费用最大流问题,看清楚是无向图的。这么说无向图和有向图的费用流问题有什么区别呢?主要是反向边的问题。首先我们说一下最大流问题中的反向边,我们需要将其cap[u][v]=0表示容量为0,而在费用流问题中添加了费用,所以肯定不能像之前那么简单处理了,那怎么办呢?在有向图中,没有存在的反向边我们用cap[u][v]=0表示容量为0,cost[v][u]=-cost[u][v]表示取反的费用,简单说就是讲这部分费用减除,相当于没有走。 现在可以说一下无向图和有向图的不同了,既然两个方向都是可以走的,那么我们就将原本有的一条边变化出了四条边,两个原有边,两个反向边,原有两个边相互独立,不能将这两个原有边看成互为反向边,否则就出现了环路,spfa就走不通
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
const LL INF = ;
#define MAXN 110
struct node
{
int u,v,next;
LL cap,flow,cost;
}edge[ * ];
int N,M,cnt,src,tag;
LL K,D,F,C,d[];
struct point
{
LL x,y,w;
}res[];
bool inq[MAXN];
int head[MAXN],p[MAXN];
void add(int u, int v, LL cost, LL cap)
{
edge[cnt].v = v;
edge[cnt].u = u;
edge[cnt].cost = cost;
edge[cnt].cap = cap;
edge[cnt].flow = ;
edge[cnt].next = head[u];
head[u] = cnt++;
// 反向边
edge[cnt].v = u;
edge[cnt].u = v;
edge[cnt].cost = -cost;
edge[cnt].cap = ;
edge[cnt].flow = ;
edge[cnt].next = head[v];
head[v] = cnt++;
}
void read()
{
for (int i = ; i <= M; i++) scanf("%lld%lld%lld",&res[i].x,&res[i].y,&res[i].w);
scanf("%lld%lld",&D,&K);
cnt = ;
src = ;
tag = N;
memset(head,-,sizeof(head));
for (int i = ; i <= M; i++)
{
add(res[i].x,res[i].y,res[i].w,K);
add(res[i].y,res[i].x,res[i].w,K);
}
add(,,,D);
}
bool SPFA()
{
queue<int>q; while (!q.empty()) q.pop();
for (int i = ; i < MAXN; i++) d[i] = INF;
d[src] = ;
memset(p,-,sizeof(p));
memset(inq,false,sizeof(inq));
q.push(src);
inq[src] = true;
while (!q.empty())
{
int u = q.front(); q.pop();
inq[u] = false;
for (int i = head[u]; i != -; i = edge[i].next)
{
int v = edge[i].v;
if (edge[i].cap > edge[i].flow && d[v] > d[u] + edge[i].cost)
{
d[v] = d[u] + edge[i].cost;
p[v] = i;
if (!inq[v])
{
inq[v] = true;
q.push(v);
}
}
}
}
//printf("%lld\n",d[tag]);
return d[tag] != INF;
}
void slove()
{
F = C = ;
while (SPFA())
{
LL a = INF;
for (int i = p[tag]; i != -; i = p[edge[i].u])
a = min(a,edge[i].cap - edge[i].flow);
for (int i = p[tag]; i != -; i = p[edge[i].u])
{
edge[i].flow += a;
edge[i ^ ].flow -= a;
}
F += a;
C += d[tag] * a;
}
}
int main()
{
//freopen("sample.txt","r",stdin);
while (scanf("%d%d",&N,&M) != EOF)
{
read();
slove();
if (F == D) printf("%lld\n",C);
else puts("Impossible.");
}
return ;
}
UVA 10594 Data Flow的更多相关文章
- SSIS Data Flow优化
一,数据流设计优化 数据流有两个特性:流和在内存缓冲区中处理数据,根据数据流的这两个特性,对数据流进行优化. 1,流,同时对数据进行提取,转换和加载操作 流,就是在source提取数据时,转换组件处理 ...
- Data Flow的Error Output
一,在Data Flow Task中,对于Error Row的处理通过Error Output Tab配置的. 1,操作失败的类型:Error(Conversion) 和 Truncation. 2, ...
- SSIS Data Flow 的 Execution Tree 和 Data Pipeline
一,Execution Tree 执行树是数据流组件(转换和适配器)基于同步关系所建立的逻辑分组,每一个分组都是一个执行树的开始和结束,也可以将执行树理解为一个缓冲区的开始和结束,即缓冲区的整个生命周 ...
- SSIS的 Data Flow 和 Control Flow
Control Flow 和 Data Flow,是SSIS Design中主要用到的两个Tab,理解这两个Tab的作用,对设计更高效的package十分重要. 一,Control Flow 在Con ...
- Intel® Threading Building Blocks (Intel® TBB) Developer Guide 中文 Parallelizing Data Flow and Dependence Graphs并行化data flow和依赖图
https://www.threadingbuildingblocks.org/docs/help/index.htm Parallelizing Data Flow and Dependency G ...
- SSIS ->> Data Flow Design And Tuning
Requirements: Source and destination system impact Processing time windows and performance Destinati ...
- SSIS ->> Control Flow And Data Flow
In the Control Flow, the task is the smallest unit of work, and a task requires completion (success, ...
- Data Flow ->> Union All
Wrox的<Professional Microsoft SQL Server 2012 Integration Services>一书中再讲Merge的时候有这样一段解释: This t ...
- Data Flow ->> Import Column & Export Column
这两个transformation的作用是把DT_TEXT, DT_NTEXT, DT_IMAGE类型的数据在文件系统和数据库间导出或者导入.比如把某个数据库表的image类型的字段导出到文件系统成为 ...
随机推荐
- POJ:2367-Cleaning Shifts
传送门:http://poj.org/problem?id=2376 Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- 模块hashlib和logging
Python的hashlib提供了常见的摘要算法MD5. 我们以常见的摘要算法MD5为例,计算出一个字符串的MD5值: import hashlib md5=hashlib.md5() md5.upd ...
- iOS系统信息集合
在项目开发的时候,经常需要用到某些系统信息,比如手机型号(5s,6,6p), 操作系统版本(8.0 or 9.3), 当前网络类型(3/4g, wifi)等信息. 有了这些信息, 可以在出了某些bug ...
- 《Cracking the Coding Interview》——第18章:难题——题目5
2014-04-29 01:51 题目:你有一个文本文件,每行一个单词.给定两个单词,请找出这两个单词在文件中出现的其中一对位置,使得这两个位置的距离最短. 解法:我的思路是建立倒排索引,计算出所有单 ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目5
2014-04-25 19:59 题目:C的关键字volatile有什么用? 解法:搞硬件设计的人好像更关注这个关键字.volatile本身是易变的意思,应该和persistent有反义词关系吧.说一 ...
- dynamic基元类型与隐式类型的局部变量var
dynamic代码示例 using System; using System.Collections.Generic; using System.Linq; using System.Text; na ...
- 手动实现一个lazyMan
这应该算一个经典的面试题了,就是一个关于流程控制的问题,要求如下 //实现一个LazyMan,可以按照以下方式调用: LazyMan("Hank")//输出: //Hi! This ...
- Java SE 5.0 - SE 8 的功能增强
Table of Contents 前言 Java 5.0 Generics Enhanced for Loop Autoboxing Typesafe Enums Varargs Static Im ...
- 每个套接字地址error
套接字问题 1 netstat -aon|findstr 5037 2 根据pid,查询占用端口的应用,这里的pid为 8672,查询命令如图 3 杀死对应的PID,taskkill /pid 8 ...
- 孤荷凌寒自学python第十九天python函数嵌套与将函数作为返回对象及闭包与递归
孤荷凌寒自学python第十九天python函数嵌套与将函数作为返回对象及闭包与递归 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) Python函数非常的灵活,今天学习了python函数的以 ...