POJ-2135-Farm Tour(最大费用最小流)模板
Farm Tour POJ - 2135
To show off his farm in the best way, he walks a tour that starts at his house, potentially travels through some fields, and ends at the barn. Later, he returns (potentially through some fields) back to his house again.
He wants his tour to be as short as possible, however he doesn't want to walk on any given path more than once. Calculate the shortest tour possible. FJ is sure that some tour exists for any given farm.
Input
* Lines 2..M+1: Three space-separated integers that define a path: The starting field, the end field, and the path's length.
Output
Sample Input
- 4 5
- 1 2 1
- 2 3 1
- 3 4 1
- 1 3 2
- 2 4 2
Sample Output
- 6
题意很简单,说思路;
设源点为s,汇点为t;
然后建图的时候,s->1 容量为2,费用为0;
n->t,容量为2,费用为0;
这样就能从s出发,到t,然后再走不重复的路径的再回到s;
由于s->1,和n->t这两条路径费用为0,所以不影响最后结果。
这样直接跑一边最大费用最小流的模板就行了。
详见代码:
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <vector>
- #include <queue>
- using namespace std;
- typedef long long ll;
- const int MAXX=;
- const int INF=0x3f3f3f3f;
- struct node
- {
- int to;
- int next;
- int cap;
- int flow;
- int cost;
- }edge[MAXX];
- int head[MAXX],tol;
- int pre[MAXX],dis[MAXX];
- bool vis[MAXX];
- int N;
- void init(int n)
- {
- N=n;
- tol=;
- memset(head,-,sizeof(head));
- }
- void addedge(int u,int v,int cap,int cost)
- {
- edge[tol].to=v;
- edge[tol].cap=cap;
- edge[tol].cost=cost;
- edge[tol].flow=;
- edge[tol].next=head[u];
- head[u]=tol++;
- edge[tol].to=u;
- edge[tol].cap=;
- edge[tol].cost=-cost;
- edge[tol].flow=;
- edge[tol].next=head[v];
- head[v]=tol++;
- }
- bool SPFA(int s,int t)
- {
- queue<int> q;
- for(int i=;i<N;i++)
- {
- dis[i]=INF;
- vis[i]=;
- pre[i]=-;
- }
- dis[s]=;
- vis[s]=;
- q.push(s);
- while(!q.empty())
- {
- int u=q.front();
- q.pop();
- vis[u]=;
- for(int i=head[u];i!=-;i=edge[i].next)
- {
- int v=edge[i].to;
- if(edge[i].cap>edge[i].flow&&dis[v]>dis[u]+edge[i].cost)
- {
- dis[v]=dis[u]+edge[i].cost;
- pre[v]=i;
- if(!vis[v])
- {
- vis[v]=;
- q.push(v);
- }
- }
- }
- }
- if(pre[t]==-)return ;
- return ;
- }
- int minCostMaxFlow(int s,int t)
- {
- int cost=;
- while(SPFA(s,t))
- {
- int minn=INF;
- for(int i=pre[t];i!=-;i=pre[edge[i^].to])
- {
- if(minn>edge[i].cap-edge[i].flow)
- minn=edge[i].cap-edge[i].flow;
- }
- for(int i=pre[t];i!=-;i=pre[edge[i^].to])
- {
- edge[i].flow+=minn;
- edge[i^].flow-=minn;
- cost+=edge[i].cost*minn;
- }
- }
- return cost;
- }
- int main()
- {
- int n,m;
- while(~scanf("%d%d",&n,&m))
- {
- int u,v,g;
- init(n+);
- for(int i=;i<m;i++)
- {
- scanf("%d%d%d",&u,&v,&g);
- addedge(u,v,,g);
- addedge(v,u,,g);
- }
- addedge(,,,);
- addedge(n,n+,,);
- printf("%d\n",minCostMaxFlow(,n+));
- }
- return ;
- }
POJ-2135-Farm Tour(最大费用最小流)模板的更多相关文章
- poj 2135 Farm Tour 【无向图最小费用最大流】
题目:id=2135" target="_blank">poj 2135 Farm Tour 题意:给出一个无向图,问从 1 点到 n 点然后又回到一点总共的最短路 ...
- POJ 2135 Farm Tour (费用流)
[题目链接] http://poj.org/problem?id=2135 [题目大意] 有一张无向图,求从1到n然后又回来的最短路 同一条路只能走一次 [题解] 题目等价于求从1到n的两条路,使得两 ...
- poj 2135 Farm Tour 最小费最大流
inf开太小错了好久--下次还是要用0x7fffffff #include<stdio.h> #include<string.h> #include<vector> ...
- POJ 2135 Farm Tour (网络流,最小费用最大流)
POJ 2135 Farm Tour (网络流,最小费用最大流) Description When FJ's friends visit him on the farm, he likes to sh ...
- 网络流(最小费用最大流):POJ 2135 Farm Tour
Farm Tour Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: ...
- POJ 2135 Farm Tour (最小费用最大流模板)
题目大意: 给你一个n个农场,有m条道路,起点是1号农场,终点是n号农场,现在要求从1走到n,再从n走到1,要求不走重复路径,求最短路径长度. 算法讨论: 最小费用最大流.我们可以这样建模:既然要求不 ...
- POJ 2135 Farm Tour(最小费用最大流)
Description When FJ's friends visit him on the farm, he likes to show them around. His farm comprise ...
- POJ 2135.Farm Tour 消负圈法最小费用最大流
Evacuation Plan Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4914 Accepted: 1284 ...
- POJ-2135 Farm Tour---最小费用最大流模板题(构图)
题目链接: https://vjudge.net/problem/POJ-2135 题目大意: 主人公要从1号走到第N号点,再重N号点走回1号点,同时每条路只能走一次. 这是一个无向图.输入数据第一行 ...
- POJ 2135 Farm Tour [最小费用最大流]
题意: 有n个点和m条边,让你从1出发到n再从n回到1,不要求所有点都要经过,但是每条边只能走一次.边是无向边. 问最短的行走距离多少. 一开始看这题还没搞费用流,后来搞了搞再回来看,想了想建图不是很 ...
随机推荐
- keepalived+nginx安装配置
软件版本号: pcre8.36 ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.gz keepalived1.2 ...
- python 数据描述字符串转整数
q3 = int(float(q3.replace('万', '')) * 10000)
- cacheed 限制 4节点 3000万 es 批量删除 shell脚本练习 elasticsearch_action
文件分割 "www.laiwunews.cn/xinxi/25324717.html""www.zznews.cn/xinxi/10411214.html"&q ...
- sed 之 -n p
sed是一个面向字符流的编辑器,一般情况下每次读入一行到一个名为模式空间的地方,进行编辑:但是也可以读入多行数据进行编辑. -n:抑制默认输出 p打印模式空间内容 cat test a b sed ' ...
- 【转】Android 关闭多个视图Intent.FLAG_ACTIVITY_CLEAR_TOP用法
如果已经启动了四个Activity:A,B,C和D.在D Activity里,我们要跳到B Activity,同时希望C finish掉, 可以在startActivity(intent)里的inte ...
- B1826 [JSOI2010]缓存交换 贪心+离散化+堆
这个题仔细一想可以直接贪心做,因为队列里下一个出现的早的一定最优.正确性显然.然后我只拿了50,我直接模拟另一个队列暴力修改最后一个点的nxt值,自然会T.但是其实不用修改,直接插入就行了前面的不影响 ...
- ubuntu系统快捷键设置
1.打开'系统设置' 2.点击键盘 3.选择快捷键,查看和修改对应的快捷键.
- CentOS Linux VPS桌面环境一键安装包
- PCB MS SQL 标量函数(CLR) 实现DataTable转HTML的方法
一.准备需转为HMLT字符串的DataTable数据 在数据库中执行一段SQL返回的数据 需转换后的HTML的文本 <html ><head></head>< ...
- [App Store Connect帮助]一、 App Store Connect 使用入门(2)登录至 App Store Connect
请使用您的 Apple ID 登录 App Store Connect.如果您是具有“帐户持有人”职能的用户,请使用您用于加入“Apple 开发者计划”的 Apple ID 登录并添加其他用户至您的 ...