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 今天学习最小费用流.模板手敲了一遍. 产生了一个新的问题:对于一条无向边,这样修改了正向边容量后,反向边不用管吗? 后来想了想, ...
随机推荐
- java List 简单使用
Student类 class Student{ String name; String pwd; public Student(){} public Student(String name, Stri ...
- 无序数组的中位数(set+deque)hdu5249
KPI Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- nyist 604 小明的难题
http://acm.nyist.net/JudgeOnline/problem.php?pid=604 小明的难题 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描述 ...
- [原创]java WEB学习笔记50:文件上传案例
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- 创建一个web Test Plan
1.添加ThreadGroup (1).线程组界面解析: 线程数:虚拟用户的个数 Ramp-up Period:开启每个用户的延迟时间,如果有5个虚拟用户,Ramp-up Period值是5,Jmet ...
- Android 利用Service BroadcastReceiver实现小例子
Activity: package com.example.test; import android.app.Activity; import android.content.Context; imp ...
- 【py网页】sitecopy代码
001 #coding:utf-8 002 import re,os,shutil,sys 003 import urllib2,socket,cookielib 004 from threading ...
- 锋利的JQuery(四)
表单: 一个表单有三个基本组成部分:表单标签.表单域.表单按钮 Cookie: 在jQuery中有一款Cookie插件,<script src="js/jquery.cookie.js ...
- jquery 实践总结
Ready事件 对DOM操作之前需要监听页面加载进度,应当在页面加载完成之后再执行DOM编辑操作. $(document).ready(function(){ ... }); 或者 $(functio ...
- 编译busybox-1.24.1 制作文件系统
arm-linux-gcc 3.4.5 busybox-1.24.1.tar.bz21, 修改 Makefile找到以下2处修改为ARCH ?= armCROSS_COMPILE ?= arm-li ...