poj2135最小费用流
裸题,就是存个模板
最小费用流是用spfa求解的,目的是方便求解负环,spfa类似于最大流中的bfs过程
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cassert>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define C 0.5772156649
#define pi acos(-1.0)
#define ll long long
#define mod 20090717
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1 using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f3f; struct edge{
int to,Next,c;
int cost;
}e[maxn<<];
int cnt,head[N];
int s,t;
int dis[N],pre[N],path[N];
void add(int u,int v,int c,int cost)
{
e[cnt].to=v;
e[cnt].c=c;
e[cnt].cost=cost;
e[cnt].Next=head[u];
head[u]=cnt++;
e[cnt].to=u;
e[cnt].c=;
e[cnt].cost=-cost;
e[cnt].Next=head[v];
head[v]=cnt++;
}
bool spfa()
{
memset(pre,-,sizeof pre);
memset(dis,inf,sizeof dis);
dis[s]=;
queue<int>q;
q.push(s);
while(!q.empty())
{
int x=q.front();
q.pop();
for(int i=head[x];~i;i=e[i].Next)
{
int te=e[i].to;
if(e[i].c>&&dis[x]+e[i].cost<dis[te])
{
dis[te]=dis[x]+e[i].cost;
pre[te]=x;
path[te]=i;
q.push(te);
}
}
}
return pre[t]!=-;
}
int mincostmaxflow()
{
int cost=,flow=;
while(spfa())
{
int f=inf;
for(int i=t;i!=s;i=pre[i])
if(e[path[i]].c<f)
f=e[path[i]].c;
flow+=f;
cost+=dis[t]*f;
for(int i=t;i!=s;i=pre[i])
{
e[path[i]].c-=f;
e[path[i]^].c+=f;
}
}
return cost;
}
void init()
{
memset(head,-,sizeof head);
cnt=;
}
int main()
{
/* ios::sync_with_stdio(false);
cin.tie(0);*/
int n,m;
while(~scanf("%d%d",&n,&m))
{
init();
s=,t=n+;
for(int i=;i<m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,,c);
add(b,a,,c);
}
add(,,,);
add(n,n+,,);
printf("%d\n",mincostmaxflow());
}
return ;
}
/******************** ********************/
poj2135最小费用流的更多相关文章
- poj2135 最小费用流
添加超级源点(与点1之间的边容量为2,权值为0)和超级汇点(与点N之间的边容量为2,权值为0),求流量为2的最小费用流.注意是双向边. #include <iostream> #inclu ...
- [poj2135]Farm Tour(最小费用流)
解题关键:最小费用流 代码一:bellma-ford $O(FVE)$ bellman-ford求最短路,并在最短路上增广,速度较慢 #include<cstdio> #include& ...
- poj2135(简单的最小费用流问题)
题目链接:http://poj.org/problem?id=2135 Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- POJ2195 最小费用流
题目:http://poj.org/problem?id=2195 处理出每个人到每个门的曼哈顿距离,分别建立容量为1费用为曼哈顿距离的边,在源点和每个人人之间建立容量为1费用为0的边,在门和汇点之间 ...
- HDU 4067 hdoj 4067 Random Maze 最小费用流
给出n个点,m条边,入口s和出口t,对于每条边有两个值a,b,如果保留这条边需要花费:否则,移除这条边需要花费b. 题目要求用最小费用构造一个有向图满足以下条件: 1.只有一个入口和出口 2.所有路都 ...
- POJ 2516:Minimum Cost(最小费用流)
https://vjudge.net/problem/11079/origin 题意:有N个商店和M个供应商和K种物品,每个商店每种物品有一个需求数,每个供应商每种物品有一个供应量,供应商到商店之间的 ...
- POJ-2175 Evacuation Plan 最小费用流、负环判定
题意:给定一个最小费用流的模型,根据给定的数据判定是否为最优解,如果不为最优解则给出一个比给定更优的解即可.不需要得出最优解. 解法:由给定的数据能够得出一个残图,且这个图满足了最大流的性质,判定一个 ...
- Going Home (hdu 1533 最小费用流)
集训的图论都快结束了,我才看懂了最小费用流,惭愧啊. = = 但是今天机械键盘到了,有弄好了自行车,好高兴\(^o^)/~ 其实也不是看懂,就会套个模板而已.... 这题最重要的就是一个: 多组输入一 ...
- POJ 2195 Going Home 最小费用流 裸题
给出一个n*m的图,其中m是人,H是房子,.是空地,满足人的个数等于房子数. 现在让每个人都选择一个房子住,每个人只能住一间,每一间只能住一个人. 每个人可以向4个方向移动,每移动一步需要1$,问所有 ...
随机推荐
- win7安装composer
安装前请务必确保已经正确安装了 PHP.打开命令行窗口并执行 php -v 查看是否正确输出版本号. 开始安装前需要把open_ssl扩展打开 打开命令行并依次执行下列命令安装最新版本的 Compos ...
- Linux中的系统挂载文件/etc/fstab
[root@localhost ~]# cat /etc/fstab ## /etc/fstab# Created by anaconda on Wed Oct 5 15:21:46 2016## A ...
- python cookbook第三版学习笔记二十一:利用装饰器强制函数上的类型检查
在演示实际代码前,先说明我们的目标:能对函数参数类型进行断言,类似下面这样: @typeassert(int, int) ... def add(x, y): ... return x + y ...
- SpringBoot连接PostgreSQL
这个 org.postgresql.jdbc.PgConnection.createClob() 方法尚未被实作 application.properties spring.datasource.pl ...
- Libsvm在matlab环境下使用指南
一.安装 http://www.csie.ntu.edu.tw/~cjlin/libsvm/matlab/.在这个地址上可以下的包含matlab接口的源程序.下载完后可以放到放到任意的盘上解压,最好建 ...
- Kafka配置参数说明
配置文件目录:/usr/local/kafka/config配置文件server.propertis参数说明:broker.id=0每一个broker在集群中的唯一标识,要求是正数,当该服务器的IP地 ...
- C#(ASP.NET)隐藏或显示Excel中指定列
今天写的一个方法,实现Excel指定列的隐藏和显示: 环境:VS2010,OFFICE 2010 代码:#region 隐藏和显示Excel中的一列 /// <summary> ...
- django-admin 设计User外键,设计model
设置外键 class profile_user(AbstractBaseUser, PermissionsMixin): company = models.ForeignKey(Company, de ...
- formatblock 块及
有标签,执行标签替换,只是替换标签,属性不改变. 在无标签外部添加标签
- Loadrunner脚本篇——从文件中读取内容并参数化
直接代码展示: char* testfn() { int count, total = 0; char * buffer = NULL; int filelenth = 0; long file_st ...