http://acm.hit.edu.cn/hoj/problem/view?id=2543

1.将原图中的每条边(u, v)拆成两条:(u, v, Ci, 0), (u, v, ∞, Ei)

2.购买的每个石头的费用P加一条 (S, 1, inf, P)的边。

3.总的能够花费的费用C可以在我们求最小费用路的时候判断。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <cmath> using namespace std;
typedef long long LL;
const int maxn = 1e3 + ;
const int maxm = 1e5+ ;
const int inf = 0x3f3f3f3f; struct MCMF
{
struct Edge
{
int v, c, w, next;
}p[maxm << ];
int e, head[maxn], dis[maxn], pre[maxn], cnt[maxn], sumFlow, n;
bool vis[maxn];
void init(int nt)
{
e = , n = nt;
memset(head, -, sizeof(head[]) * (n + ) );
}
void addEdge(int u, int v, int c, int w)
{
p[e].v = v; p[e].c = c; p[e].w = w; p[e].next = head[u]; head[u] = e++;
swap(u, v);
p[e].v = v; p[e].c = ; p[e].w = -w; p[e].next = head[u]; head[u] = e++;
}
bool spfa(int S, int T)
{
queue <int> q;
for (int i = ; i <= n; ++i)
vis[i] = cnt[i] = , pre[i] = -, dis[i] = inf;
vis[S] = , dis[S] = ;
q.push(S);
while (!q.empty())
{
int u = q.front(); q.pop();
vis[u] = ;
for (int i = head[u]; i + ; i = p[i].next)
{
int v = p[i].v;
if (p[i].c && dis[v] > dis[u] + p[i]. w)
{
dis[v] = dis[u] + p[i].w;
pre[v] = i;
if (!vis[v])
{
q.push(v);
vis[v] = ;
if (++cnt[v] > n) return ;
}
}
}
}
return dis[T] != inf;
}
void mcmf(int S, int T, int ct)
{
sumFlow = ;
LL minFlow = , minCost = ;
while (spfa(S, T))
{
minFlow = inf + ;
for (int i = pre[T]; i + ; i = pre[ p[i ^ ].v ])
minFlow = min(minFlow,(LL)p[i].c);
sumFlow += minFlow;
for (int i = pre[T]; i + ; i = pre[ p[i ^ ].v ])
{
p[i].c -= minFlow;
p[i ^ ].c == minFlow;
}
minCost += dis[T] * minFlow;
if (minCost > ct)
{
sumFlow -= ceil((minCost - ct) * 1.0 / dis[T]);
break;
}
}
}
void build(int nt, int mt, int ct, int pt)
{
init(nt);
int u, v, c, w;
addEdge(, , inf, pt);
while (mt--)
{
scanf("%d%d%d%d", &u, &v, &c, &w);
u++, v++;
addEdge(u, v, c, ); addEdge(u, v, inf, w);
swap(u, v);
addEdge(u, v, c, ); addEdge(u, v, inf, w);
}
}
void solve(int nt, int mt, int ct, int pt)
{
build(nt, mt, ct, pt);
mcmf(, , ct);
printf("%d\n", sumFlow);
}
}my;
int main()
{
int tcase, n, m, c, p;
scanf("%d", &tcase);
while (tcase--)
{
scanf("%d%d%d%d",&n, &m, &c, &p);
my.solve(n, m, c, p);
}
return ;
}

hoj 2543 (费用流 拆边)的更多相关文章

  1. HDU 3667 费用流 拆边 Transportation

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

  2. UVA1486 Transportation 费用流 拆边。

    #include <iostream> #include <cstdio> #include <cmath> #include <queue> #inc ...

  3. hoj 2715 (费用流 拆点)

    http://acm.hit.edu.cn/hoj/problem/view?id=2715 将每个格子 i 拆成两个点 i’, i’’并加边(i’, i’’, 1, -Vi), (i’, i’’, ...

  4. 【BZOJ4930】棋盘 拆边费用流

    [BZOJ4930]棋盘 Description 给定一个n×n的棋盘,棋盘上每个位置要么为空要么为障碍.定义棋盘上两个位置(x,y),(u,v)能互相攻击当前仅 当满足以下两个条件: 1:x=u或y ...

  5. 【BZOJ2245】[SDOI2011]工作安排 拆边费用流

    [BZOJ2245][SDOI2011]工作安排 Description 你的公司接到了一批订单.订单要求你的公司提供n类产品,产品被编号为1~n,其中第i类产品共需要Ci件.公司共有m名员工,员工被 ...

  6. 【 UVALive - 2197】Paint the Roads(上下界费用流)

    Description In a country there are n cities connected by m one way roads. You can paint any of these ...

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

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

  8. BZOJ 1449 JSOI2009 球队收益 费用流

    题目大意:给定nn支球队.第ii支球队已经赢了winiwin_i场.输了loseilose_i场,接下来还有mm场比赛.每一个球队终于的收益为Ci∗x2i+Di∗y2iC_i*x_i^2+D_i*y_ ...

  9. HDU 3667 Transportation(网络流之费用流)

    题目地址:HDU 3667 这题的建图真是巧妙...为了保证流量正好达到k.须要让每一次增广到的流量都是1,这就须要把每一条边的流量都是1才行.可是每条边的流量并非1,该怎么办呢.这个时候能够拆边,反 ...

随机推荐

  1. grep怎样匹配tab键

    grep怎样匹配tab键 学习了:https://blog.csdn.net/qixinkui/article/details/2746433 1 grep -P '/t'; 2 awk '//t/' ...

  2. Windows 10系统专业精简

    第1页:捆绑应用一键卸载 随着微软彻底放弃win7的更新,win8的弱势,新一代的win10系统则成为了微软着力打造的王牌系统. 作为微软最新的王牌产品,win10系统从功能到外观都有着超过前代产品的 ...

  3. 解决 The &#39;InnoDB&#39; feature is disabled; you need MySQL built with &#39;InnoDB&#39; to have it working

    事由: 迁移server的时候须要操作数据库.将数据库也进行迁移,在新server中导入数据的时候提示 The 'InnoDB' feature is disabled; you need MySQL ...

  4. java.net.ConnectException: failed to connect to /10.0.2.2 (port 80): connect

    在使用GENYMOTION作为Android程序调试模拟器连接web服务器时,报了:java.net.ConnectException: failed to connect to /10.0.2.2 ...

  5. import * as obj from 'xx'

    import * as obj from 'xx'  这种写法是把所有的输出包裹到obj对象里 例如: xx里中: export function hello(){ return '我是hello 内 ...

  6. ant-design 实现 添加页面

    1.逻辑代码 /** * 添加用户 */ import React,{PureComponent} from 'react' import {Card,Form,Input,Select,Button ...

  7. 微信小程序 解决 数字粗细不一 的bug

    1.bug 2.原因解析 微信小程序本身字体问题 3.解决方案 设置字体 font-family: Microsoft YaHei; .

  8. android中几个很有用的的api

    0x0001 public PackageInfo getPackageArchiveInfo (String archiveFilePath, int flags) Since: API Level ...

  9. springboot学习(九) 使用mybatis访问数据库

    1.添加maven依赖 <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId ...

  10. vue 记一次编译没反应、无进度、没有任何报错的提示,但后台却TM一直消耗内存的BUG:

    控制台一直提示“building for production...”,而且spinner停止了动画! 由于没有任何的提示.况且项目的代码.结构.设计完全未知模糊的情况下,我只能按照unix的理念“使 ...