网络流 poj 2135
n个点 m条边
给m条边
求1->n n->1 最小花费,每条边最多走一次
两个最短路显然不行 会影响另外一条
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
#include<math.h> using namespace std; #define inf 100000000
#define MAXN 50000
#define MAXN1 1500 struct node
{
int fr,to,next,fl,w; }x[MAXN];
int cnt,S,T;
int head[MAXN1],dis[MAXN1],pre[MAXN1];
bool vis[MAXN1]; void add(int u,int v,int fl,int w)
{
x[cnt].fr=u;
x[cnt].next=head[u];
x[cnt].to=v;
x[cnt].fl=fl;
x[cnt].w=w;
head[u]=cnt++;
}
queue<int>q1; int spfa() //最短路的话就是可以增广 费用最少
{
memset(vis,,sizeof(vis));
memset(pre,-,sizeof(pre));
int i;
for(i=;i<=T;i++)
dis[i]=inf;
dis[S]=;
vis[S]=;
q1.push(S);
while(!q1.empty())
{
int now=q1.front();
q1.pop();
vis[now]=;
for(i=head[now];i!=-;i=x[i].next)
{
if(dis[now]+x[i].w<dis[x[i].to]&&x[i].fl)
{
dis[x[i].to]=dis[now]+x[i].w;
pre[x[i].to]=i;
if(vis[x[i].to]==)
{
q1.push(x[i].to);
vis[x[i].to]=;
}
}
}
}
int a=pre[T];
while(a!=-) //要结束了也可以更新流量
{
x[a].fl-=;
x[a^].fl+=;
a=pre[x[a].fr];
}
return pre[T]!=-;
} int main()
{
int n,m; while(scanf("%d%d",&n,&m)!=EOF)
{
int i;
cnt=;
memset(head,-,sizeof(head));
S=,T=n+;
for(i=;i<=m;i++)
{
int a,b,w; scanf("%d%d%d",&a,&b,&w);
add(a,b,,w),add(b,a,,-w); //-w 如果有一条更优的路 可以走回来+(-w);
add(b,a,,w),add(a,b,,-w);
}
add(S,,,),add(,S,,); //其实就是2次 1->n
add(n,T,,),add(T,n,,);
int ans=;
while(spfa())
ans=ans+dis[T];
printf("%d\n",ans);
}
return ;
}
网络流 poj 2135的更多相关文章
- POJ 2135 Farm Tour (网络流,最小费用最大流)
POJ 2135 Farm Tour (网络流,最小费用最大流) Description When FJ's friends visit him on the farm, he likes to sh ...
- POJ 2135 Farm Tour (最小费用最大流模板)
题目大意: 给你一个n个农场,有m条道路,起点是1号农场,终点是n号农场,现在要求从1走到n,再从n走到1,要求不走重复路径,求最短路径长度. 算法讨论: 最小费用最大流.我们可以这样建模:既然要求不 ...
- poj 2135 Farm Tour 【无向图最小费用最大流】
题目:id=2135" target="_blank">poj 2135 Farm Tour 题意:给出一个无向图,问从 1 点到 n 点然后又回到一点总共的最短路 ...
- 网络流(最小费用最大流):POJ 2135 Farm Tour
Farm Tour Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: ...
- 【网络流#9】POJ 2135 Farm Tour 最小费用流 - 《挑战程序设计竞赛》例题
[题意]给出一张无向图,从1开始到n,求两条没有公共边的最短路,使得路程总和最小 每条边的权值设为费用,最大流量设为1,然后就是从源点到汇点流量为2的最小费用流. 因为是规定了流量,新建一个源点和一个 ...
- Poj(2135),MCMF,模板
题目链接:http://poj.org/problem?id=2135 Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- POJ 2135 Farm Tour (费用流)
[题目链接] http://poj.org/problem?id=2135 [题目大意] 有一张无向图,求从1到n然后又回来的最短路 同一条路只能走一次 [题解] 题目等价于求从1到n的两条路,使得两 ...
- POJ - 2135最小费用流
题目链接:http://poj.org/problem?id=2135 今天学习最小费用流.模板手敲了一遍. 产生了一个新的问题:对于一条无向边,这样修改了正向边容量后,反向边不用管吗? 后来想了想, ...
- POJ 2135 Farm Tour
题目大意:有一个无向图..农夫从1号点出发..要到达N号点..然后回到1号点..来回不能走相同的路径..问最短的距离是多少. 题解:又是不能走重复路径!基本图论算法直接扔掉上网络流.不能相同就边限1, ...
随机推荐
- 第10章 同步设备I/O和异步设备I/O(2)_同步IO和异步IO基础
10.3 执行同步设备I/O (1)对设备读写操作的函数 ①ReadFile/WriteFile函数 参数 描述 hFile 文件句柄 pvBuffer 指向要接收文件数据的缓冲区或把缓冲区数据写入设 ...
- poj1001 Exponentiation 大数的幂
Description Problems involving the computation of exact values of very large magnitude and precision ...
- jprofiler安装图解
环境: 1.sun jdk1.6.0 2.jprofiler_windows_6_0_2.exe 安装 1. jdk, 安装略... 2. jprofiler安装 一路next 到Enter lice ...
- 转:GCC,LLVM,Clang编译器对比
GCC,LLVM,Clang编译器对比 转自: http://www.cnblogs.com/qoakzmxncb/archive/2013/04/18/3029105.html 在XCode中, ...
- C# 文件下载四方法
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...
- php配置rewrite模块
转 (1) 启用rewrite模块,在默认情况下,没有启用 修改httpd.conf文件 #启动rewrite模块 LoadModule rewrite_module modules/mod_r ...
- php常用自定义函数
1,写一个函数,算出两个文件的相对路径 有两种方法,一种是利用array的相关方法,如例1,另外一种是使用?:;运算符 先看第一种方法 function getrelativepath2($path1 ...
- VS2013 Web项目添加引用项目后,引用上有黄色的感叹号小图标
RT,重新生成还是不行,然后重新打开VS,VS2013,还是不行. 最后,右键引用-属性-已解析 False. 为什么会未解析呢.终于找到问题根源了. 当前项目.net 4.0版,而引用的项目.net ...
- Web Api其中的PUT功能演示
Insus.NET这几天均在学习Web API知识,并练习.怎样获取数据,提交数据或是保存数据.你可以温习一下<Post model至Web Api创建或是保存数据>http://www. ...
- NOI2018准备Day2
昨天雄心壮志了一番,今天就有点儿松懈了,是生于忧患,死于安乐吗 刷了15道大水题,5道字符串,5道多维数组,5道顺序查找,9个小时,平均40分钟一道水题,目标10分钟一道......昨天才刷了20道. ...