题解:

最小费用最大流

每一次找最短的

代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int N=;
int fi[N],n,t,cas,m,x,y,z,f[N],ne[N],num,zz[N],fl[N],gp[N],dist[N],pre[N],sl[N];
void jb(int x,int y,int z,int s)
{
ne[num]=fi[x];
fi[x]=num;
zz[num]=y;
sl[num]=z;
fl[num++]=s;
ne[num]=fi[y];
fi[y]=num;
zz[num]=x;
sl[num]=;
fl[num++]=-s;
}
int spfa()
{
memset(dist,0x3f,sizeof dist);
memset(pre,-,sizeof pre);
memset(gp,,sizeof gp);
memset(f,,sizeof f);
queue<int > Q;
Q.push();
dist[]=;
while (!Q.empty())
{
int now=Q.front();
Q.pop();
f[now]=;
for (int i=fi[now];i!=-;i=ne[i])
if (sl[i]>)
{
int t=zz[i];
if (dist[t]>dist[now]+fl[i])
{
dist[t]=dist[now]+fl[i];
pre[t]=now;
gp[t]=i;
if (!f[t])
{
f[t]=;
Q.push(t);
}
}
}
}
if (pre[n+]==-)return ;
return ;
}
void Max_flow()
{
int cost=,flow=;
while (!spfa())
{
int f=1e9;
for (int i=n+;i>;i=pre[i])
f=min(f,sl[gp[i]]);
cost+=f;
flow+=dist[n+]*f;
for (int i=n+;i>;i=pre[i])
{
sl[gp[i]]-=f;
sl[gp[i]^]+=f;
}
}
if (cost<)puts("Not possible");
else printf("%d\n",flow);
}
void init()
{
printf("Instance #%d: ",++cas);
num=;
memset(fi,-,sizeof fi);
}
void doit()
{
while (m--)
{
scanf("%d%d%d",&x,&y,&z);
jb(x+,y+,,z);
}
jb(,,,);
jb(n+,n+,,);
Max_flow();
}
int main()
{
while (~scanf("%d%d",&n,&m),n|m)
{
init();
doit();
}
}

poj3068的更多相关文章

  1. POJ3068 "Shortest" pair of paths 【费用流】

    POJ3068 "Shortest" pair of paths Description A chemical company has an unusual shortest pa ...

  2. POJ3068 "Shortest" pair of paths

    嘟嘟嘟 题目大意:一个有向图,每一条边有一个边权,求从节点\(0\)到\(n - 1\)的两条不经过同一条边的路径,并且边权和最小. 费用流板子题. 发个博客证明一下我写了这题. #include&l ...

  3. POJ3068:"Shortest" pair of paths——题解

    http://poj.org/problem?id=3068 题目大意: 从0-n-1找到两条边和点都不相同(除了0和n-1外)的最小费用路径. ——————————————————————————— ...

随机推荐

  1. cisco anyconnect linux

    cisco anyconnect linux 官方的下载需要登录验证,比较麻烦,可以从这个地方直接下载使用.支持ubuntu,centos. cisco anyconnect vpn client我本 ...

  2. 伪类 :after 清除浮动的原理和方法

    浮动元素容器的clearing问题1. 问题的由来有这样一种情形:在一个容器(container)中,有两个浮动的子元素.<div>        <div style=" ...

  3. 谷歌技术"三宝"之BigTable(转)

    原文地址:   http://blog.csdn.net/opennaive/article/details/7532589 2006年的OSDI有两篇google的论文,分别是BigTable和Ch ...

  4. ThinkPHP将上传问件添加到数据库

    <?php namespace Home\Controller; /***************** use Think\Controller; ****命名空间****/ class Mes ...

  5. What is OWIN? A Beginners Guide

    http://www.codedigest.com/posts/1/what-is-owin-a-beginners-guide http://owin.org/html/spec/owin-1.0. ...

  6. Android studio 运行模拟器报:Application Installation Failed

    前两天笔记本加了个SSD硬盘,原机械硬盘移植到光驱位,硬盘盘符都变了,结果在用android studio 运行以前的程序编译不报错,运行模拟器就会报如下错误. Installation failed ...

  7. 【python】argparse学习(转)

    点击这里成为作者 · 更新于 2018-11-14 21:00:36 argparse argparse 是 Python 内置的一个用于命令项选项与参数解析的模块,通过在程序中定义好我们需要的参数, ...

  8. 【卷积神经网络】对BN层的解释

    前言 Batch Normalization是由google提出的一种训练优化方法.参考论文:Batch Normalization Accelerating Deep Network Trainin ...

  9. codeforces570D Tree Requests

    题目链接:codeforces570D 正解:$dsu$ $on$ $tree$ 解题报告: 考虑这又是一类子树内的不带修改统计问题,直接上$dsu$ $on$ $tree$好咯. 直接按上一道题的做 ...

  10. 在Web API 2 中实现带JSON的Patch请求

    译文:http://www.cnblogs.com/kexxxfeng/p/the-patch-verb-in-web-api-2-with-json.html 原文:https://carly.io ...