解题关键:最小费用流

代码一:bellma-ford $O(FVE)$  bellman-ford求最短路,并在最短路上增广,速度较慢

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<vector>
#define inf 0x3f3f3f3f
#define MAX_V 10010
using namespace std;
typedef long long ll;
struct edge{int to,cap,cost,rev;};
int V;
vector<edge>G[MAX_V];
int dist[MAX_V];
int prevv[MAX_V],preve[MAX_V]; void add_edge(int from,int to,int cap,int cost){
G[from].push_back((edge){to,cap,cost,G[to].size()});
G[to].push_back((edge){from,,-cost,G[from].size()-});
} int min_cost_flow(int s,int t,int f){
int res=;
while(f>){
fill(dist,dist+V,inf);
dist[s]=;
bool update=true;
while(update){
update=false;
for(int v=;v<V;v++){
if(dist[v]==inf) continue;
for(int i=;i<G[v].size();i++){
edge &e=G[v][i];
if(e.cap>&&dist[e.to]>dist[v]+e.cost){
dist[e.to]=dist[v]+e.cost;
prevv[e.to]=v;
preve[e.to]=i;
update=true;
}
}
}
}
if(dist[t]==inf) return -;
int d=f;
for(int v=t;v!=s;v=prevv[v]) d=min(d,G[prevv[v]][preve[v]].cap);
f-=d;
res+=d*dist[t];
for(int v=t;v!=s;v=prevv[v]){
edge &e=G[prevv[v]][preve[v]];
e.cap-=d;
G[v][e.rev].cap+=d;
}
}
return res;
}
int n,m,t1,t2,t3;
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
memset(G,,sizeof G);
V=n;
for(int i=;i<m;i++){
scanf("%d%d%d",&t1,&t2,&t3);
add_edge(t1-,t2-,,t3);
add_edge(t2-,t1-,,t3);
}
printf("%d\n",min_cost_flow(,n-,));
}
return ;
}

代码二:dijkstra,$O(FElogV)$

这里是通过一个定理

s到v的最短距离<=s到u的最短距离+dis(e)

s到u的最短距离+dis(e)-s到v的最短距离>=0

将原先的距离转化为上述的等效距离,即可保证图中无负权边,所以可以用dijkstra算法堆优化来保证复杂度。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<vector>
#include<queue>
#define inf 0x3f3f3f3f
#define MAX_V 10010
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
struct edge{int to,cap,cost,rev;};
int V;
vector<edge>G[MAX_V];
int h[MAX_V],dist[MAX_V],prevv[MAX_V],preve[MAX_V];
void add_edge(int from,int to,int cap,int cost){
G[from].push_back((edge){to,cap,cost,G[to].size()});
G[to].push_back((edge){from,,-cost,G[from].size()-});
} int min_cost_flow(int s,int t,int f){
int res=;
fill(h,h+V,);
while(f>){
priority_queue<P,vector<P>,greater<P> >que;
fill(dist,dist+V,inf);
dist[s]=;
que.push(P(,s));
while(!que.empty()){
P p=que.top();que.pop();
int v=p.second;
if(dist[v]<p.first) continue;
for(int i=;i<G[v].size();i++){
edge &e=G[v][i];
if(e.cap>&&dist[e.to]>dist[v]+e.cost+h[v]-h[e.to]){
dist[e.to]=dist[v]+e.cost+h[v]-h[e.to];
prevv[e.to]=v;
preve[e.to]=i;
que.push(P(dist[e.to],e.to));
}
}
}
if(dist[t]==inf) return -;
for(int v=;v<V;v++) h[v]+=dist[v]; //增广
int d=f;
for(int v=t;v!=s;v=prevv[v]) d=min(d,G[prevv[v]][preve[v]].cap);
f-=d;
res+=d*h[t];
for(int v=t;v!=s;v=prevv[v]){
edge &e=G[prevv[v]][preve[v]];
e.cap-=d;
G[v][e.rev].cap+=d;
}
}
return res;
}
int n,m,t1,t2,t3;
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
memset(G,,sizeof G);
V=n;
for(int i=;i<m;i++){
scanf("%d%d%d",&t1,&t2,&t3);
add_edge(t1-,t2-,,t3);
add_edge(t2-,t1-,,t3);
}
printf("%d\n",min_cost_flow(,n-,));
}
return ;
}

[poj2135]Farm Tour(最小费用流)的更多相关文章

  1. POJ2135 Farm Tour

      Farm Tour Time Limit: 2MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Description ...

  2. POJ2135 Farm Tour —— 最小费用最大流

    题目链接:http://poj.org/problem?id=2135 Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  3. poj2135 Farm Tour(费用流)

    Description When FJ's friends visit him on the farm, he likes to show them around. His farm comprise ...

  4. 【网络流#9】POJ 2135 Farm Tour 最小费用流 - 《挑战程序设计竞赛》例题

    [题意]给出一张无向图,从1开始到n,求两条没有公共边的最短路,使得路程总和最小 每条边的权值设为费用,最大流量设为1,然后就是从源点到汇点流量为2的最小费用流. 因为是规定了流量,新建一个源点和一个 ...

  5. POJ 2135 Farm Tour 最小费用流

    两条路不能有重边,既每条边的容量是1.求流量为2的最小费用即可. //#pragma comment(linker, "/STACK:1024000000,1024000000") ...

  6. POJ2135 Farm Tour(最小费用最大流)

    题目问的是从1到n再回到1边不重复走的最短路,本质是找1到n的两条路径不重复的尽量短的路. #include<cstdio> #include<cstring> #includ ...

  7. POJ Farm Tour

    Farm Tour 题目: 约翰有N块地,家在1号,而N号是个仓库.农场内有M条道路(双向的),道路i连接这ai号地和bi号地,长度为ci. 约翰希望依照从家里出发,经过若干地后达到仓库.然后再返回家 ...

  8. POJ 2135 Farm Tour (网络流,最小费用最大流)

    POJ 2135 Farm Tour (网络流,最小费用最大流) Description When FJ's friends visit him on the farm, he likes to sh ...

  9. poj2135(简单的最小费用流问题)

    题目链接:http://poj.org/problem?id=2135 Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

随机推荐

  1. Ubuntu安装SSH + Windows上配置Putty

    1. Ubuntu安装SSH 命令: # sudo apt-get install openssh-server 2. 启动SSH Server 命令: # sudo /etc/init.d/ssh ...

  2. Java多线程系列 JUC锁06 Condition条件

    Condition介绍 Condition中提供了一组类似于Object中的监视器方法.与Lock配合可以完成等待通知模式. Lock lock = new ReentrantLock(); Cond ...

  3. 第二篇、css尺寸和边框

    一.尺寸和标签图 二.尺寸单位 %:百分比 in:英寸 cm:厘米 mm:毫米 pt:磅(点)(1pt等于1/72英寸) px:像素(计算机屏幕上的一个点) em:1em等于当前的字体尺寸,2em等于 ...

  4. 剑指offer——树中两个节点的最低公共祖先

    代码来源与<剑指offer> 得到从根节点开始到输入的两个结点的两条,需要遍历两次树,每遍历一次的时间复杂度是O(n),得到的两条路径的长度在最差情况时是O(n),通常情况下两条路径的长度 ...

  5. hiho一下 第二十九周 最小生成树三·堆优化的Prim算法【14年寒假弄了好长时间没搞懂的prim优化:prim算法+堆优化 】

    题目1 : 最小生成树三·堆优化的Prim算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 回到两个星期之前,在成功的使用Kruscal算法解决了问题之后,小Ho产生 ...

  6. Spring Cloud之Swagger2 API接口管理

    随着微服务架构体系的发展和应用, 为了前后端能够更好的集成与对接,同时为了项目的方便交付,每个项目都需要提供相应的API文档. 来源:PC端.微信端.H5端.移动端(安卓和IOS端) 传统的API文档 ...

  7. Javascript中 节流函数 throttle 与 防抖函数 debounce

    问题的引出 在一些场景往往由于事件频繁被触发,因而频繁地进行DOM操作.资源加载,导致UI停顿甚至浏览器崩溃. 在这样的情况下,我们实际上的需求大多为停止改变大小n毫秒后执行后续处理:而其他事件大多的 ...

  8. WCF寄宿(Host)之自我寄宿(Self-Hosting)简单实例【Console应用为宿主】

    前言: 由于最近的项目 中需要用到WCF,所以又回头翻了翻,阅读了大量园中大神的博文,故而做个总结. 谬误之处,万望不吝指教! 闲话不叙! 一.寄宿(Host)WCF服务  1)一种是为一组WCF服务 ...

  9. 【leetcode刷题笔记】Longest Consecutive Sequence

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  10. linux导出Mysql数据sql脚本