第一道费用流的题目---

其实---还是不是很懂,只知道沿着最短路找增广路

建图

源点到1连一条容量为2(因为要来回),费用为0的边

n到汇点连一条容量为2,费用为0的边

另外的就是题目中输入的了

另外这题是无向边-----

maxn 开到1000会re----

存个模板先吧------------------------------------------

 #include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<vector>
#include<queue>
#include<string>
using namespace std; typedef long long LL;
const int maxn = ;
const int INF = ( << ) - ; int first[maxn],vis[maxn],dis[maxn],pos[maxn],ecnt,size; struct Edge{
int v,next,cap,cost;
} e[*maxn]; void init(){
ecnt = ;
memset(first,-,sizeof(first));
} void add_edge(int u,int v,int cap,int cost){
e[ecnt].v = v;
e[ecnt].cap = cap;
e[ecnt].cost = cost;
e[ecnt].next = first[u];
first[u] = ecnt++; e[ecnt].v = u;
e[ecnt].cap = ;
e[ecnt].cost = -cost;
e[ecnt].next = first[v];
first[v] = ecnt++;
} bool SPFA(int s, int t)
{
int u,v,i;
queue <int> q;
memset(vis,,sizeof(vis));
for(i= ;i <= size;i++) dis[i]=INF; dis[s]=;
vis[s]=;
q.push(s); while(!q.empty()){
u=q.front(); q.pop(); vis[u]=;
for (i = first[u]; ~i;i = e[i].next){
v=e[i].v;
if(e[i].cap > && dis[u]+e[i].cost < dis[v]){
dis[v]=dis[u]+e[i].cost;
pos[v]=i;
if(!vis[v]){
vis[v]=;
q.push(v);
}
}
}
}
return dis[t] != INF;
} LL MCMF(int s,int t)
{
int i;
LL cost=,flow=;
while(SPFA(s,t)){
int d=INF;
for (i = t;i != s;i = e[pos[i]^].v){
d = min(d,e[pos[i]].cap);
}
for(i = t;i != s;i = e[pos[i]^].v){
e[pos[i]].cap -= d;
e[pos[i]^].cap += d;
}
flow += d;
cost += dis[t]*d;
}
return cost;
} int main(){
int n,m;
while(scanf("%d %d",&n,&m) != EOF){
init();
size = n+;
add_edge(,,,); add_edge(,,,);
add_edge(n+,n,,);add_edge(n,n+,,); for(int i = ;i < m;i++){
int u,v,w;
scanf("%d %d %d",&u,&v,&w);
add_edge(u,v,,w);
add_edge(v,u,,w);
}
printf("%I64d\n",MCMF(,n+));
}
return ;
}

poj 2135 Farm Tour【 最小费用最大流 】的更多相关文章

  1. poj 2135 Farm Tour 最小费用最大流建图跑最短路

    题目链接 题意:无向图有N(N <= 1000)个节点,M(M <= 10000)条边:从节点1走到节点N再从N走回来,图中不能走同一条边,且图中可能出现重边,问最短距离之和为多少? 思路 ...

  2. POJ 2135 Farm Tour [最小费用最大流]

    题意: 有n个点和m条边,让你从1出发到n再从n回到1,不要求所有点都要经过,但是每条边只能走一次.边是无向边. 问最短的行走距离多少. 一开始看这题还没搞费用流,后来搞了搞再回来看,想了想建图不是很 ...

  3. poj 2351 Farm Tour (最小费用最大流)

    Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17230   Accepted: 6647 Descri ...

  4. [poj] 1235 Farm Tour || 最小费用最大流

    原题 费用流板子题. 费用流与最大流的区别就是把bfs改为spfa,dfs时把按deep搜索改成按最短路搜索即可 #include<cstdio> #include<queue> ...

  5. POJ2135 Farm Tour —— 最小费用最大流

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

  6. TZOJ 1513 Farm Tour(最小费用最大流)

    描述 When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 &l ...

  7. Farm Tour(最小费用最大流模板)

    Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18150   Accepted: 7023 Descri ...

  8. POJ 2135 Farm Tour (费用流)

    [题目链接] http://poj.org/problem?id=2135 [题目大意] 有一张无向图,求从1到n然后又回来的最短路 同一条路只能走一次 [题解] 题目等价于求从1到n的两条路,使得两 ...

  9. poj 2135 Farm Tour 最小费最大流

    inf开太小错了好久--下次还是要用0x7fffffff #include<stdio.h> #include<string.h> #include<vector> ...

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

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

随机推荐

  1. (转)Bootstrap3 概述

    http://blog.csdn.net/duruiqi_fx/article/details/53285607 注意:HTML5 文档类型 Bootstrap 使用到的某些 HTML 元素和 CSS ...

  2. Rx (Reactive Extensions)介绍

    Reactive Extensions (Rx) 原来是由微软提出的一个综合了异步和基于事件驱动编程的库包,使用可观察序列和LINQ-style查询操作. 使用Rx, 开发者可以用Observable ...

  3. utf8_general_ci、utf8_unicode_ci和utf8_bin的区别(转载)

    例如: CREATE DATABASE IF NOT EXISTS redmine DEFAULT CHARSET utf8 COLLATE utf8_general_ci; CREATE DATAB ...

  4. Redis详解入门篇(转载)

    Redis详解入门篇(转载) [本教程目录] 1.redis是什么2.redis的作者3.谁在使用redis4.学会安装redis5.学会启动redis6.使用redis客户端7.redis数据结构 ...

  5. spring重点一:处理对象创建时间 个数以及方式

    /** * 1) 对象创建: 单例/多例(个数) * scope="singleton", 默认值, 即 默认是单例 [service/dao/工具类] *  scope=&quo ...

  6. Python数据结构2-----队列和堆

    一.线性结构:栈.队列.双端队列.列表 二.非线性结构:树.图.堆 [算法中看堆是非线性的,因为其相当于完全二叉树,但堆的存储元素是采用线性的顺序表数组来实现的] 三.队列: 1.队列类型:FIFO. ...

  7. Node笔记(1)

    Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境.  进程 1.process.argv 用于获取当前进程信息 0--node.exe的目录1--js文件的目录2 ...

  8. 【JavaScript框架封装】在实现一个自己定义类似于JQuery的append()函数的时候遇到的问题及解决方案

    主要问题: 在刚开始创建了这个函数之后,使用的时候,总是会出现一个问题,就是按照正常步骤给一个ID选择器添加子节点的时候正常,但是到了给一个class选择器的元素添加的时候始终只能添加一个. 下面是我 ...

  9. 【LibreOJ 6279】 数列分块入门 3 (分块)

    传送门 code: //By Menteur_Hxy #include<cstdio> #include<iostream> #include<algorithm> ...

  10. maven 依赖的传递性

    1.如图我们有三个项目,项目Age,项目Bge,项目Cge 2.我们使Age项目依赖到Bge项目,Bge项目依赖到Cge项目 Age项目和Bge项目分别执行命令:mvn install  打包*.ja ...