两条路不能有重边,既每条边的容量是1。求流量为2的最小费用即可。

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define pb(a) push(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define PI  3.1415926535898
template<class T> T min(const T& a,const T& b,const T& c)
{return min(min(a,b),min(a,c));
}
template<class T> T max(const T& a,const T& b,const T& c)
{return max(max(a,b),max(a,c));
}
void debug()
{
#ifdef ONLINE_JUDGE
#elsefreopen("data.in","r",stdin);// freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch()
{int ch;while((ch=getchar())!=EOF){if(ch!=' '&&ch!='\n')return ch;}return EOF;
}struct Edge
{int from,to,cost,cap;
};
const int maxn = 3111;vector<int> g[maxn];
vector<Edge> edge;
int n,m,s,t;
void init()
{for(int i = 1; i <= n; i++)g[i].clear();edge.clear();
}
void add(int from, int to, int cost, int cap)
{edge.push_back((Edge){from, to, cost, cap});g[from].push_back(edge.size() - 1);edge.push_back((Edge){to, from, -cost, 0});g[to].push_back(edge.size() - 1);
}int d[maxn];
int inq[maxn];
int road[maxn];int SPFA()
{queue<int> q;q.push(s);memset(d, INF, sizeof(d));memset(inq, 0, sizeof(inq));inq[s] = true;d[s] = 0;road[s] = -1;while(!q.empty()){int x = q.front(); q.pop();inq[x] = false;for(int i = 0; i < g[x].size(); i++){Edge &e = edge[g[x][i]];if(e.cap>0 && d[x] + e.cost < d[e.to]){d[e.to] = d[x] + e.cost;road[e.to] = g[x][i];if(!inq[e.to]){inq[e.to] = true;q.push(e.to);}}}}return d[t];
}
int max_cost_flow()
{int flow = 2;int cost = 0;while(flow){int d = SPFA();int f = flow;for(int e = road[t]; e != -1; e = road[edge[e].from]){Edge &E = edge[e];f = min(f, E.cap);}flow -= f;cost += d * f;for(int e = road[t]; e != -1; e = road[edge[e].from]){edge[e].cap -= f;edge[e^1].cap += f;}}return cost;
}
int main()
{debug();while(scanf("%d%d", &n, &m) != EOF){init();for(int i = 1; i <= m; i++){int from,to,cost;scanf("%d%d%d", &from, &to, &cost);add(from, to, cost, 1);add(to, from, cost, 1);}s=1;t=n;printf("%d\n", max_cost_flow());}return 0;
}

View Code

转载于:https://www.cnblogs.com/BMan/p/3713712.html

POJ 2135 Farm Tour 最小费用流相关推荐

  1. POJ 2135 Farm Tour (费用流)

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

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

    Description 给出一张\(N\)个点\(M\)条边的带权无向图,结点编号从\(1\)到\(N\),求从\(1\)到\(N\)再到\(1\)的最短路,每条边最多走一次. Input 第一行给出 ...

  3. POJ 2135 Farm Tour amp;amp; HDU 2686 Matrix amp;amp; HDU 3376 Matrix Again 费用流求来回最短路...

    累了就要写题解,近期总是被虐到没脾气. 来回最短路问题貌似也能够用DP来搞.只是拿费用流还是非常方便的. 能够转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1.然后连 ...

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

    题目链接:点击查看 题目大意:给出一个由n个点和m条边组成的无向图,每条边都有权值,求点1->点n->点1的最短路,且要求每条路至多经过一次,并使得途径的权值和最小 题目分析:虽然题目要求 ...

  5. POJ-2135 Farm Tour 最小费用流

    题目链接:http://poj.org/problem?id=2135 很容易看出来时最小费用流,但这里要注意是无向边,所以要建立两条边.为了满足退流时,花费还是最小,反向边的花费要为相反数. 1 / ...

  6. POJ 1637 Sightseeing tour(最大流)

    POJ 1637 Sightseeing tour 题目链接 题意:给一些有向边一些无向边,问能否把无向边定向之后确定一个欧拉回路 思路:这题的模型很的巧妙,转一个http://blog.csdn.n ...

  7. poj 1637 Sightseeing tour 混合欧拉图判定

    POJ - 1637点我点我:-) Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %ll ...

  8. poj 1637 Sightseeing tour

    http://poj.org/problem?id=1637 题意: 给出一张混合图,判断是否存在欧拉回路 原理: 1.存在欧拉回路的充要条件:所有点入度=出度 2.给无向边随便定向不会影响点的|出度 ...

  9. POJ 2135 最小费用最大流

    思路: 源->1连费用0 流量2 其它的边 费用w 流量1 n->汇 费用0 流量2 最小费用流 搞定~ //By SiriusRen #include <queue> #in ...

最新文章

  1. 如何查询土地规划用途_一秒辨别“三无产品”,护肤品的猫腻如何发现?
  2. 解决At least one JAR was scanned for TLDs yet contained no TLDs. 问题
  3. html5 如何实现客户端验证上传文件的大小
  4. JPA关系映射之one-to-many和many-to-one
  5. conj--复数的共轭值
  6. memc_nginx+srcache_nginx+memcached构建透明的动态页面缓存
  7. 2018 蓝桥杯省赛 A 组模拟赛(一)数列求值+推导
  8. java shiro security_安全框架Shiro和SpringSecurity的比较
  9. CentOS7 安装MongoDB 3.0服务
  10. 运筹学作业(一)——线性规划
  11. IAR执行到断点处不能单步运行解决方法
  12. CYQ.Data V4.5.5 版本发布[顺带开源Emit编写的快速反射转实体类FastToT类]
  13. ssm房屋租赁管理系统ssm房屋管理系统JSP网上租房系统JSP房产信息网站房屋租赁系统房屋
  14. 语料库翻译学需要学计算机吗,语料库翻译学发展现状及转向
  15. 遭DeFi反噬,以太坊绝地反击
  16. 小魔推如何解决实体商家痛点,实现短视频高转化
  17. 【计算机网络】因特网概述
  18. 使用react脚手架创建 tsx版本,react添加typescript
  19. 2020年二建YL黄金AB卷-目前只有建筑-机电-市政-管理
  20. MySQL的数值类型

热门文章

  1. VS2005创建CLR自定义触发器
  2. css布局中的居中问题
  3. junit集成Hamcrest测试集合中某个属性是否包含特定值
  4. 在Eclipse中使用Maven构建Spring项目
  5. Python3中with用法
  6. VS2010运行速度优化汇总
  7. 【C++】google gtest 详解
  8. 【Qt】Qt程序查看动态链接库(windows)
  9. json vue 对象转数组_vue 基础入门(一)修改
  10. 树莓派4外置wifi天线_树莓派打造最强车载娱乐终端(1)音乐和WiFi