解题关键:最小费用流

代码一: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. Python基础(5)_字符编码、文件处理

    一.文件读取过程: 1. 文本编辑器存取文件的原理(nodepad++,pycharm,word) 打开编辑器就打开了启动了一个进程,是在内存中的,所以在编辑器编写的内容也都是存放与内存中的,断电后数 ...

  2. spring 注解事务

    前提:在applicationContext.xml中配置<tx:annotation-driven transaction-manager="transactionManager&q ...

  3. 模仿jquery框架源码---网络

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Untitled Page& ...

  4. 【leetnode刷题笔记】Maximum Depth of binary tree

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  5. debian下使用ft232为stm32f429i-discovery烧写uboot和uImage

    操作系统:debian 软件: openocd  minicom 硬件:  MiniUSB线.stm32f429i-discovery, WaveShare FT232串口模块(可以在淘宝上买到) 关 ...

  6. TP里的关联查询

    $res = $db->join('b on a.ex_id = b.pe_eid')->select(); 注意:这里的表a.表b需要带表前缀

  7. JavaScript中call、apply个人理解

    JavaScript中call.apply个人理解 一句话即通俗的说:call.apply 是为了改变this的状态而存在的 }; } function personInfo(name,age){ t ...

  8. jQuery 开发一个简易插件

    jQuery 开发一个简易插件 //主要内容 $.changeCss = function(options){ var defaults = { color:'blue', ele:'text', f ...

  9. 提醒程序员注意的一些事项--R

    经验丰富的程序员通常会发现R语言的某些方面不太寻常.以下是这门语言中你需要了解的一些特性. 对象名称中的句点(.)没有特殊意义.但美元符号($)却有着和其他语言中的句点类似的含义,即指定一个对象中的 ...

  10. python3与Redis连接操作

    Python3之redis使用   简介 redis是一个key-value存储系统,和Memcache类似,它支持存储的value类型相对更多,包括string(字符串),list(链表),set( ...