Poj(2135),MCMF,模板
题目链接:http://poj.org/problem?id=2135
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14821 | Accepted: 5657 |
Description
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
Source
- #include <iostream>
- #include <cstring>
- #include <cstdio>
- #include <queue>
- using namespace std;
- int sumFlow;
- const int MAXN = ;
- const int MAXM = ;
- #define INF 0x3f3f3f3f
- struct Edge
- {
- int u;
- int v;
- int cap;
- int cost;
- int next;
- } edge[MAXM<<];
- int NE;
- int head[MAXN], dist[MAXN], pre[MAXN];
- bool vis[MAXN];
- void addedge(int u,int v,int cap,int cost)
- {
- edge[NE].u=u;
- edge[NE].v=v;
- edge[NE].cap=cap;
- edge[NE].cost=cost;
- edge[NE].next=head[u];
- head[u]=NE++;
- edge[NE].u=v; ///反的容量图
- edge[NE].v=u;
- edge[NE].cap=;
- edge[NE].cost=-cost;
- edge[NE].next=head[v];
- head[v]=NE++;
- }
- bool SPFA(int s,int t,int n)
- {
- int i,u,v;
- queue<int>Q;
- memset(vis,false,sizeof(vis));
- memset(pre,-,sizeof(pre));
- for(i=; i<=n; i++)
- dist[i]=INF;
- vis[s]=true;
- dist[s]=;
- Q.push(s);
- while(!Q.empty())
- {
- u=Q.front();
- Q.pop();
- vis[u]=false;
- for(i=head[u]; i!=-; i=edge[i].next)
- {
- v=edge[i].v;
- if(edge[i].cap&&dist[v]>dist[u]+edge[i].cost)
- {
- dist[v]=dist[u]+edge[i].cost;
- pre[v]=i;
- if(!vis[v])
- {
- Q.push(v);
- vis[v]=true;
- }
- }
- }
- }
- if(dist[t]==INF)
- return false;
- return true;
- }
- int MCMF(int s,int t,int n)
- {
- int flow=; /// 总流量
- int minflow,mincost;
- mincost=;
- while(SPFA(s,t,n))
- {
- minflow=INF+; ///容量瓶颈
- for(int i=pre[t]; i!=-; i=pre[edge[i].u])
- if(edge[i].cap<minflow)
- minflow=edge[i].cap;
- flow+=minflow;
- for(int i=pre[t]; i!=-; i=pre[edge[i].u])
- {
- edge[i].cap-=minflow;
- edge[i^].cap+=minflow; ///更新残余网络,^1处理,因为0,是正边,1是反边,做^1,就能找到反边
- }
- mincost+=dist[t]*minflow; ///这里和书上有点不一样,就是相当于一个合并同类项了,dist[t]到汇点的最少花费*瓶颈容量
- }
- sumFlow=flow; /// 最大流
- return mincost;
- }
- int main()
- {
- int n,m;
- int u,v,c;
- while(~scanf("%d%d",&n,&m))
- {
- NE=;
- memset(head,-,sizeof(head));
- int S=;
- int T=n+;
- while(m--)
- {
- scanf("%d%d%d",&u,&v,&c);
- addedge(u,v,,c);
- addedge(v,u,,c); ///建反图,这样在SPFA里面就能回来了,
- }
- addedge(S,,,);
- addedge(n,T,,);
- int ans=MCMF(S,T,T+);
- printf("%d\n",ans);
- }
- return ;
- }
Poj(2135),MCMF,模板的更多相关文章
- POJ 2135 Farm Tour (最小费用最大流模板)
题目大意: 给你一个n个农场,有m条道路,起点是1号农场,终点是n号农场,现在要求从1走到n,再从n走到1,要求不走重复路径,求最短路径长度. 算法讨论: 最小费用最大流.我们可以这样建模:既然要求不 ...
- POJ 2135 Farm Tour (网络流,最小费用最大流)
POJ 2135 Farm Tour (网络流,最小费用最大流) Description When FJ's friends visit him on the farm, he likes to sh ...
- HIT 2715 - Matrix3 - [最小费用最大流][数组模拟邻接表MCMF模板]
题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=2715 Time limit : 5 sec Memory limit : 64 M Zhouguyue ...
- Poj 2187 凸包模板求解
Poj 2187 凸包模板求解 传送门 由于整个点数是50000,而求凸包后的点也不会很多,因此直接套凸包之后两重循环即可求解 #include <queue> #include < ...
- poj 2135 Farm Tour 【无向图最小费用最大流】
题目:id=2135" target="_blank">poj 2135 Farm Tour 题意:给出一个无向图,问从 1 点到 n 点然后又回到一点总共的最短路 ...
- POJ 2195 - Going Home - [最小费用最大流][MCMF模板]
题目链接:http://poj.org/problem?id=2195 Time Limit: 1000MS Memory Limit: 65536K Description On a grid ma ...
- POJ 2135 /// 最小费用流最大流 非负花费 BellmanFord模板
题目大意: 给定一个n个点m条边的无向图 求从点1去点n再从点n回点1的不重叠(同一条边不能走两次)的最短路 挑战P239 求去和回的两条最短路很难保证不重叠 直接当做是由1去n的两条不重叠的最短路 ...
- 网络流(最小费用最大流):POJ 2135 Farm Tour
Farm Tour Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: ...
- POJ - 2135最小费用流
题目链接:http://poj.org/problem?id=2135 今天学习最小费用流.模板手敲了一遍. 产生了一个新的问题:对于一条无向边,这样修改了正向边容量后,反向边不用管吗? 后来想了想, ...
随机推荐
- 在自定义的UINavigationController中设置背景图片
//这个方法中设置 + (void)initialize { UINavigationBar *bar = [UINavigationBar appearance]; [bar setBackgrou ...
- Lintcode: Maximum Subarray II
Given an array of integers, find two non-overlapping subarrays which have the largest sum. The numbe ...
- java-语句
JAVA语句 1.顺序语句(用:结束)(一个分号也是一个语句)(多条语句形成符合语句) 2.分支语句(又称条件语句) 1. if 语句 例: int a=10 if(a>0) {System ...
- java中的BigDecimal和String的相互转换
/*由数字字符串构造BigDecimal的方法 02.*设置BigDecimal的小数位数的方法 03.*/ 04.import java.math.BigDecimal; 05.//数字字符串 06 ...
- (转)flexigrid 参数说明
本文为转载 http://simple1024.iteye.com/blog/1171090 项目用到这玩意,像样的API都是英文的,英文不好,所以经过各种搜集,flexigrid就整理了这么多用得上 ...
- 【你吐吧c#每日学习】10.30 C#Nullable Types
分两种类型,value type and reference type. By default, value type owns a default value. For integer, the d ...
- linux第12天 线程
今天主要学习了共享内存,信号量的封装,还有线程. POSIX线程库 与线程有关的函数构成了一个完整的系列,绝大多数函数的名字都是以“pthread_”打头的 要使用这些函数库,要通过引入头文<p ...
- cocos2d对动画的各种操作
瞬时动作:瞬时动作的基类是InstantAction 1.放置位置 CGPoint p = ccp(width,height); [sprite runAction:[CCPlace action ...
- Script to set the Purchase Order Status to ‘OPEN’(将采购订单重新打开)
Business Requirement: The finance user requests the IT team to change the PO status to OPEN as they ...
- Entity Framework 无法对没有主键的视图映射实体的解决办法
我们在使用Entity Framework的时候经常会把数据库中的某一个视图映射为EF的实体,但是如果数据库视图中的列没有包含表的主键列,EF会报出警告说视图没有主键,导致视图映射为实体失败,错误如下 ...