解题关键:最小费用流

代码一: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. CodeBackUP_node_find_serial

    /*************************************************************** ** Find the serial number Node ** R ...

  2. HDU - 5703 Desert 【找规律】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5703 题意 给出一杯容量为N的水 每次至少喝1个单位 有多少种不同的方式喝完 比如 给出3 就有4种方 ...

  3. nodejs模块之http&&url

    我们使用nodejs中的http模块来进行网络操作 一.什么是HTTP协议: 超文本传输协议(HyperText Transfer Protocol)HTTP假定其下层协议提供可靠传输. 因此,任何能 ...

  4. 20165101刘天野 2017-2018-2 《Java程序设计》第1周学习总结

    20165101刘天野 2017-2018-2 <Java程序设计>第1周学习总结 教材学习内容总结 Java的地位 Java的特点 安装JDK Java应用程序开发步骤 反编译 安装Gi ...

  5. mono上运行程序常见问题

    1. System.BadImageFormatException: Invalid method header local vars signature token 0x 65d5b2File na ...

  6. Jboss添加Windows服务,同时定期重启

    一.添加成Windows服务 进入目录 \wildfly-9.0.2s - All\bin\service\ 编辑 service.bat,修改一下参数 set SHORTNAME=SAMEXAppS ...

  7. Docker-Mac安装

    1. 下载安装包2. 安装3. 运行,允许docker获得系统权限,它需要将Mac网卡链接至Docker app.4. 验证 打开terminaldocker versionFengZhendeMac ...

  8. JavaWeb -- Cookie应用实例 -- 购物历史记录

    1. 页面一:主页面                                         页面二: 详细显示页面   Demo2 负责页面一, 显示商品清单和历史记录 Demo3负责页面二 ...

  9. cdoj 1256 昊昊爱运动 预处理

    昊昊爱运动 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) 昊昊喜欢运动 他NN ...

  10. 在解析XML时要注意解析元素和解析标签属性的区别

    解析元素时: Sting str = ele.elementText("name"); 而解析标签属性时: String key = ele.attributeValue(&quo ...