poj2135最小费用最大流经典模板题
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 13509 | Accepted: 5125 |
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<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
//最小费用最大流,求最大费用只需要取相反数,结果取相反数即可。
//点的总数为 N,点的编号 0~N-1
const int MAXN = ;
const int MAXM = ;
const int INF = 0x3f3f3f3f;
struct Edge
{
int to,next,cap,flow,cost;
} edge[MAXM*];
int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N;//节点总个数,节点编号从0~N-1
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] = false;
pre[i] = -;
}
dis[s] = ;
vis[s] = true;
q.push(s);
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = false;
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] = true;
q.push(v);
}
}
}
}
if(pre[t] == -)return false;
else return true;
}
//返回的是最大流,cost存的是最小费用
int minCostMaxflow(int s,int t,int &cost)
{
int flow = ;
cost = ;
while(spfa(s,t))
{
int Min = INF;
for(int i = pre[t]; i != -; i = pre[edge[i^].to])
{
if(Min > edge[i].cap - edge[i]. flow)
Min = edge[i].cap - edge[i].flow;
}
for(int i = pre[t]; i != -; i = pre[edge[i^].to])
{
edge[i].flow += Min;
edge[i^].flow -= Min;
cost += edge[i]. cost * Min;
}
flow += Min;
}
return flow;
}
int main(){
int n,m,sta;
while(scanf("%d%d",&n,&m)!=EOF){
memset(pre,,sizeof(pre));
memset(dis,,sizeof(dis));
memset(vis,false,sizeof(vis));
memset(edge,,sizeof(edge));
init(n+);
int u,v,w;
for(int i=;i<m;i++){
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,,w);
addedge(v,u,,w); }
int ans1=;
addedge(,,,);
addedge(n,n+,,);
int temp=minCostMaxflow(,n+,ans1);
printf("%d\n",ans1); }
}
poj2135最小费用最大流经典模板题的更多相关文章
- hdu 1533 Going Home 最小费用最大流 (模板题)
Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- POJ2135 最小费用最大流模板题
练练最小费用最大流 此外此题也是一经典图论题 题意:找出两条从s到t的不同的路径,距离最短. 要注意:这里是无向边,要变成两条有向边 #include <cstdio> #include ...
- [POJ2135]最小费用最大流
一直由于某些原因耽搁着...最小费用最大流没有搞会. 今天趁着个人状态正佳,赶紧去看看,果然30min不到看会了算法+模板并且A掉了一道题. 感觉最小费用最大流在学过了最大流之后还是挺好理解的.找到从 ...
- HDU 1533 最小费用最大流(模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1533 这道题直接用了模板 题意:要构建一个二分图,家对应人,连线的权值就是最短距离,求最小费用 要注意void ...
- POJ 2195 - Going Home - [最小费用最大流][MCMF模板]
题目链接:http://poj.org/problem?id=2195 Time Limit: 1000MS Memory Limit: 65536K Description On a grid ma ...
- 网络流--最小费用最大流MCMF模板
标准大白书式模板 #include<stdio.h> //大概这么多头文件昂 #include<string.h> #include<vector> #includ ...
- Minimum Cost(最小费用最大流,好题)
Minimum Cost http://poj.org/problem?id=2516 Time Limit: 4000MS Memory Limit: 65536K Total Submissi ...
- 【luogu P3381 最小费用最大流】 模板
题目链接:https://www.luogu.org/problemnew/show/P3381 把bfs变成spfa #include <queue> #include <cstd ...
- poj_2195Going Home(最小费用最大流)
poj_2195Going Home(最小费用最大流) 标签: 最小费用最大流 题目链接 题意: 有n*m的矩阵,H表示这个点是一个房子,m表示这个点是一个人,现在每一个人需要走入一个房间,已经知道的 ...
随机推荐
- HDU 4283 You Are the One (区间DP,经典)
题意: 某校举行一场非诚勿扰,给定一个出场序列,表示n个人的屌丝值,如果他是第k个出场的,他的不满意度为(k-1)*diao[i].为了让所有人的屌丝值之和更小,导演设置一个栈,可以将部分人装进栈中, ...
- Java TCP通信
1.Socket原理 1)Socket简介 socket通常称作“套接字”,用于描述IP地址和端口号,是一个通信链的句柄.在Internet上的主机一般运行了多个服务软件,同时提供几种服务.每种服务都 ...
- 三维GIS-室内寻径功能实现
期末,要交一个大作业,正巧之前跑国图借书的时候,晕头转向的,国图内居然没有导航!!!借这个机会做一个室内导航的demo,只是半成品,还需要加入室内定位,匹配一下坐标才能在实际中使用. demo:利用蜂 ...
- Ubuntu 16.04 换国内源
官方渠道,图形界面,操作简单,可以说对新手及其友好!! 依次打开:搜索,软件与更新,第一个和第三个勾上,下载自,其它,然后在中国条目下选择你想使用的镜像站点,然后点“选择服务器”,然乎点击“关闭”,选 ...
- 2017乌鲁木齐网络赛 j 题
题目连接 : https://nanti.jisuanke.com/t/A1256 Life is a journey, and the road we travel has twists and t ...
- Paper: 《Bert》
Bert: Bidirectional Encoder Representations from Transformers. 主要创新点:Masked LM 和 Next sentence predi ...
- 说说TCP的三次握手
在说这个问题之前,先说说IP协议和TCP协议 问题:IP协议能做什么?不能做什么? 我们都知道IP协议是无连接的通信协议,它不会占用两个正在通信的计算机的通信线路,这样就降低了IP对网络传输中的需求, ...
- Word2vec资料
Word2vec 很好的资料 Word2Vec-知其然知其所以然 https://www.zybuluo.com/Dounm/note/591752 Word2Vec数学原理讲解 http://w ...
- 从输入URL到页面加载完成的过程中都发生了什么事情?
为了便于理解,我将整个过程分为了六个问题来展开. 第一个问题:从输入 URL 到浏览器接收的过程中发生了什么事情? 从触屏到 CPU 首先是「输入 URL」,大部分人的第一反应会是键盘,不过为了与时俱 ...
- ubuntu 16.04 连接 wifi
我的电脑是win10+ubuntu16.04双系统.在ubuntu下无法连接wifi,一直用usb连接的手机流量,不太方便.现在来用安装无线驱动,顺便翻个墙. https://blog.csdn.ne ...