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 今天学习最小费用流.模板手敲了一遍. 产生了一个新的问题:对于一条无向边,这样修改了正向边容量后,反向边不用管吗? 后来想了想, ...
随机推荐
- Nginx简单配置
Nginx 配置文件结构如果你下载好啦,你的安装文件,不妨打开 conf 文件夹的 nginx.conf 文件,Nginx 服务器的基础配置,默认的配置也存放在此.在 nginx.conf 的注释符号 ...
- [转] 多线程 《深入浅出 Java Concurrency》目录
http://ifeve.com/java-concurrency-thread-directory/ synchronized使用的内置锁和ReentrantLock这种显式锁在java6以后性能没 ...
- C#控件:TabControl
tabcontrol在切换页面的时候经常用到 这里讲下如何让tabcontrol更好看 ref:http://blog.csdn.net/conmajia/article/details/759671 ...
- CCF真题之ISBN号码
201312-2 问题描述 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字.1位识别码和3位分隔符,其规定格式如“x-xxx-xxxxx-x”,其中符号“-”是分隔符(键盘上 ...
- mybatis(一)安装
1.创建web项目,添加jar包 2.创建实验表user_t 3.在src下创建conf.xml文件,如下 <?xml version="1.0" encoding=&quo ...
- Oracle游标总结三
-- 声明游标:CURSOR cursor_name IS select_statement --For 循环游标--(1)定义游标--(2)定义游标变量--(3)使用for循环来使用这个游标,for ...
- 《zw版·Halcon-delphi系列原创教程》 水果自动分类脚本(机器学习、人工智能)
<zw版·Halcon-delphi系列原创教程> 水果自动分类脚本(机器学习.人工智能) 前面介绍了超市,流水线,酸奶的自动分类算法,下面再介绍一个水果的自动分类算法. Halcon强大 ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON AddNoiseWhite
zw版[转发·台湾nvp系列Delphi例程]HALCON AddNoiseWhite unit Unit1;interfaceuses Windows, Messages, SysUtils, Va ...
- [ThinkPHP] 输出、模型的使用
# # ThinkPHP 3.1.2 输出和模型使用 # 讲师:赵桐正 微博:http://weibo.com/zhaotongzheng 本节课大纲: 一.ThinkPHP 3 的输出 ...
- C语言初学者代码中的常见错误与瑕疵(1)
曾在豆瓣上看到过一个小朋友贴出他自己的代码(http://www.douban.com/group/topic/40293109/),当时随口指点了几句.难得这位小朋友虚心修正.从善如流,不断地改,又 ...