题目链接:http://poj.org/problem?id=2135

今天学习最小费用流。模板手敲了一遍。

产生了一个新的问题:对于一条无向边,这样修改了正向边容量后,反向边不用管吗?

后来想了想,得出了个结论。路径所选的边只会包括正反中的一条。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 2e3;
const int INF = 1e9;
int dist[maxn];
int pv[maxn],pe[maxn];
struct edge
{
int to, cap, rev;
int cost;
edge(int a, int b, int c, int d)
{
to = a, cap = b, cost = c, rev = d;
}
};
vector<edge> g[maxn];
void addedge(int from,int to,int cap,int cost)
{
g[from].push_back(edge(to,cap,cost,g[to].size()));
g[to].push_back(edge(from,,-cost,g[from].size()-));
}
int n;
int vis[maxn];
void SPFA(int s, int t)
{
for(int i = ; i < maxn; i++) dist[i] = INF;
memset(vis, , sizeof(vis));
dist[s] = , vis[s] = ;
queue<int> q;
q.push(s);
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = ;
for(int i = ; i < g[u].size(); i++)
{
edge &e = g[u][i];
if(e.cap > && (dist[e.to] - (dist[u] + e.cost)) > )
{
pv[e.to] = u, pe[e.to] = i;
dist[e.to] = dist[u] + e.cost;
if(!vis[e.to])
{
vis[e.to] = ;
q.push(e.to);
}
}
}
}
}
int min_cost_flow(int s,int t,int f,int& max_flow)
{
int ret = 0.0;
while(f>)
{
SPFA(s, t);
if(dist[t] == INF) return ret;///同一目的地,每次增广路都是最小费用
///当所有边的流量都流净后,即没有残余网络,返回。
int d = f;
for(int v=t;v!=s;v=pv[v])
{
d = min(d,g[pv[v]][pe[v]].cap);
}
f -= d;
max_flow += d;
ret += (int)d*dist[t]; ///走一单位就消耗dist[t]
for(int v=t;v!=s;v=pv[v])
{
edge &e = g[pv[v]][pe[v]];
e.cap -= d;
g[v][e.rev].cap += d;
}
}
return ret;
}
int main()
{
int n,m;
scanf("%d %d",&n,&m);
int s=,t=n+;
addedge(s,,,);
addedge(n,t,,);
for(int i=;i<=m;i++)
{
int x,y,w;
scanf("%d %d %d",&x,&y,&w);
addedge(x,y,,w);
addedge(y,x,,w);
}
// printf("%d\n",e[6].cap);
///反向边不用管它,因为路径只会选择正反里面的一条边
int maxflow = ;
int ans = min_cost_flow(s,t,INF,maxflow);
for(int i = ; i < maxn; i++) g[i].clear();
printf("%d\n",ans);
return ;
}

Code

POJ - 2135最小费用流的更多相关文章

  1. POJ 2135 /// 最小费用流最大流 非负花费 BellmanFord模板

    题目大意: 给定一个n个点m条边的无向图 求从点1去点n再从点n回点1的不重叠(同一条边不能走两次)的最短路 挑战P239 求去和回的两条最短路很难保证不重叠 直接当做是由1去n的两条不重叠的最短路 ...

  2. POJ 2135 Farm Tour (最小费用最大流模板)

    题目大意: 给你一个n个农场,有m条道路,起点是1号农场,终点是n号农场,现在要求从1走到n,再从n走到1,要求不走重复路径,求最短路径长度. 算法讨论: 最小费用最大流.我们可以这样建模:既然要求不 ...

  3. POJ 2135 Farm Tour (网络流,最小费用最大流)

    POJ 2135 Farm Tour (网络流,最小费用最大流) Description When FJ's friends visit him on the farm, he likes to sh ...

  4. poj 2135 Farm Tour 【无向图最小费用最大流】

    题目:id=2135" target="_blank">poj 2135 Farm Tour 题意:给出一个无向图,问从 1 点到 n 点然后又回到一点总共的最短路 ...

  5. 【网络流#9】POJ 2135 Farm Tour 最小费用流 - 《挑战程序设计竞赛》例题

    [题意]给出一张无向图,从1开始到n,求两条没有公共边的最短路,使得路程总和最小 每条边的权值设为费用,最大流量设为1,然后就是从源点到汇点流量为2的最小费用流. 因为是规定了流量,新建一个源点和一个 ...

  6. Farm Tour POJ - 2135 (最小费用流)

    When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= ...

  7. POJ 2135 Farm Tour 最小费用流

    两条路不能有重边,既每条边的容量是1.求流量为2的最小费用即可. //#pragma comment(linker, "/STACK:1024000000,1024000000") ...

  8. Poj(2135),MCMF,模板

    题目链接:http://poj.org/problem?id=2135 Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  9. POJ 2135.Farm Tour 消负圈法最小费用最大流

    Evacuation Plan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4914   Accepted: 1284   ...

随机推荐

  1. HTTP-常用配置

    前言 这篇主要介绍HTTP服务程序环境 可能有一些介绍不到,博主能力有限,欢迎大神来纠正改进 HTTP协议从http/0.9到如今的http/2.0中间发生了很大的改变,现在主流的事http/1.1 ...

  2. ccf 201803-1 跳一跳(Python实现)

    一.原题 问题描述 试题编号: 201803-1 试题名称: 跳一跳 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 近来,跳一跳这款小游戏风靡全国,受到不少玩家的喜爱. 简化 ...

  3. 第6章 AOP与全局异常处理6.5-6.11 慕课网微信小程序开发学习笔记

    https://coding.imooc.com/learn/list/97.html 目录: 第6章 AOP与全局异常处理6-1 正确理解异常处理流程 13:236-2 固有的处理异常的思维模式与流 ...

  4. SQL登录注册练习

    /class User package com.neusoft.bean; public class User { private int password; private String name; ...

  5. 动态规划:Codeforces Round #427 (Div. 2) C Star sky

    C. Star sky time limit per test2 seconds memory limit per test256 megabytes inputstandard input outp ...

  6. debian7不能apt安装emacs24

    维护者在主页上 http://emacs.naquadah.org/ 提到: These packages are not maintained anymore I don't use these p ...

  7. 64位程序调用32DLL解决方案

    最近做一个.NETCore项目,需要调用以前用VB6写的老程序,原本想重写,但由于其调用了大量32DLL,重写后还需要编译为32位才能运行,于是干脆把老代码整个封装为32DLL,然后准备在64位程序中 ...

  8. webdriver高级应用- 操作日期控件

    1. 通过点击的方式操作日期控件 #encoding=utf-8 from selenium import webdriver import unittest, time, traceback fro ...

  9. log4net配置分析

    appender   附加器 RollingFileAppender      滚动文件appender MaxSizeRollBackups      最大尺寸回滚 ConversionPatter ...

  10. linux shell脚本监控进程是否存在

    用shell脚本监控进程是否存在 不存在则启动的实例,先上代码干货:    #!/bin/shps -fe|grep processString |grep -v grepif [ $? -ne 0 ...