无向图费用流

还有一段话摘自别人博客

这道题是无向图的最小费用最大流问题,看清楚是无向图的。这么说无向图和有向图的费用流问题有什么区别呢?主要是反向边的问题。首先我们说一下最大流问题中的反向边,我们需要将其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的更多相关文章

  1. SSIS Data Flow优化

    一,数据流设计优化 数据流有两个特性:流和在内存缓冲区中处理数据,根据数据流的这两个特性,对数据流进行优化. 1,流,同时对数据进行提取,转换和加载操作 流,就是在source提取数据时,转换组件处理 ...

  2. Data Flow的Error Output

    一,在Data Flow Task中,对于Error Row的处理通过Error Output Tab配置的. 1,操作失败的类型:Error(Conversion) 和 Truncation. 2, ...

  3. SSIS Data Flow 的 Execution Tree 和 Data Pipeline

    一,Execution Tree 执行树是数据流组件(转换和适配器)基于同步关系所建立的逻辑分组,每一个分组都是一个执行树的开始和结束,也可以将执行树理解为一个缓冲区的开始和结束,即缓冲区的整个生命周 ...

  4. SSIS的 Data Flow 和 Control Flow

    Control Flow 和 Data Flow,是SSIS Design中主要用到的两个Tab,理解这两个Tab的作用,对设计更高效的package十分重要. 一,Control Flow 在Con ...

  5. 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 ...

  6. SSIS ->> Data Flow Design And Tuning

    Requirements: Source and destination system impact Processing time windows and performance Destinati ...

  7. SSIS ->> Control Flow And Data Flow

    In the Control Flow, the task is the smallest unit of work, and a task requires completion (success, ...

  8. Data Flow ->> Union All

    Wrox的<Professional Microsoft SQL Server 2012 Integration Services>一书中再讲Merge的时候有这样一段解释: This t ...

  9. Data Flow ->> Import Column & Export Column

    这两个transformation的作用是把DT_TEXT, DT_NTEXT, DT_IMAGE类型的数据在文件系统和数据库间导出或者导入.比如把某个数据库表的image类型的字段导出到文件系统成为 ...

随机推荐

  1. python——获取数据类型

    在python中,可使用type()和isinstance()内置函数获取数据类型 如: (1)type()的使用方法: >>> a = '230'         >> ...

  2. 009---Django的模型层(1)

    ORM 全称:对象--关系--映射 数据表----类 数据行----对象 字 段----属性优势:专注于后端逻辑,不用写复杂的sql语句劣势:会忘掉sql,牺牲了效率 Django连接mysql数据库 ...

  3. SVN 的基本用法

    克隆远程库 # svn checkout $URL --username=$userName 显示库信息 # svn info 显示库状态 # svn status 将文件纳入版本管理 # svn a ...

  4. centos使用--centos7.3配置LNMP

    目录 1 源的配置 2 安装软件 2.1 安装php7 2.2 安装nginx 2.3 安装mysql 2.4 安装vsftp (ftp登录配置) 3 开机启动设置 4 其它一些配置 4.1 git的 ...

  5. React + webpack 快速搭建开发环境

    因网上大多React + webpack快速搭建的运行不起来,便自行写了一个.在搭建开发环境的前需安装nodejs,npm. 新建一个工作目录,比如叫reactdome,在reactdome目录中运行 ...

  6. 对setTimeout函数的理解

    之前去面试一家公司时,面试官出了一道关于js的setTimeout函数的题目: /* *面试官给的原题目如下: *执行mytest()后,控制台输出内容是_____ *function mytest( ...

  7. mysql错误:Column ‘id’ in field list is ambiguous的解决方法

    [Err] 1052 - Column 'modify_time' in where clause is ambiguous 出错的语句: SELECT AVG(T.se)%60FROM( SELEC ...

  8. Python3.7在win10下安装PyAudio库以及实现音频的录制与播放

    Python3.7 无法安装pyaudio 度娘的结果基本都是这个,pip install pyaudio.....然而十有八九你的电脑不买账,会报错. 报错信息: running install r ...

  9. [C++] 拓展属性

    inline函数 函数重载 占位参数和默认参数 /*__________________________________________________________________ 背景: C++ ...

  10. CodeBlocks X64 SVN 编译版

    CodeBlocks X64 SVN 编译版 采用官方最新的SVN源码编译而来,纯64位的,所以32位系统是不能使用的.字体使用的是微软的YaHei UI字体,如果有更好的字节建议,可以留言. 由于直 ...