两条路不能有重边,既每条边的容量是1。求流量为2的最小费用即可。

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define pb(a) push(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define PI 3.1415926535898
template<class T> T min(const T& a,const T& b,const T& c)
{
return min(min(a,b),min(a,c));
}
template<class T> T max(const T& a,const T& b,const T& c)
{
return max(max(a,b),max(a,c));
}
void debug()
{
#ifdef ONLINE_JUDGE
#else
freopen("data.in","r",stdin);
// freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch()
{
int ch;
while((ch=getchar())!=EOF)
{
if(ch!=' '&&ch!='\n')return ch;
}
return EOF;
} struct Edge
{
int from,to,cost,cap;
};
const int maxn = ; vector<int> g[maxn];
vector<Edge> edge;
int n,m,s,t;
void init()
{
for(int i = ; i <= n; i++)
g[i].clear();
edge.clear();
}
void add(int from, int to, int cost, int cap)
{
edge.push_back((Edge){from, to, cost, cap});
g[from].push_back(edge.size() - );
edge.push_back((Edge){to, from, -cost, });
g[to].push_back(edge.size() - );
} int d[maxn];
int inq[maxn];
int road[maxn]; int SPFA()
{
queue<int> q;
q.push(s);
memset(d, INF, sizeof(d));
memset(inq, , sizeof(inq));
inq[s] = true;
d[s] = ;
road[s] = -;
while(!q.empty())
{
int x = q.front(); q.pop();
inq[x] = false;
for(int i = ; i < g[x].size(); i++)
{
Edge &e = edge[g[x][i]];
if(e.cap> && d[x] + e.cost < d[e.to])
{
d[e.to] = d[x] + e.cost;
road[e.to] = g[x][i]; if(!inq[e.to])
{
inq[e.to] = true;
q.push(e.to);
}
}
}
}
return d[t];
}
int max_cost_flow()
{
int flow = ;
int cost = ;
while(flow)
{
int d = SPFA();
int f = flow;
for(int e = road[t]; e != -; e = road[edge[e].from])
{
Edge &E = edge[e];
f = min(f, E.cap);
}
flow -= f;
cost += d * f;
for(int e = road[t]; e != -; e = road[edge[e].from])
{
edge[e].cap -= f;
edge[e^].cap += f;
}
}
return cost;
}
int main()
{
debug();
while(scanf("%d%d", &n, &m) != EOF)
{
init();
for(int i = ; i <= m; i++)
{
int from,to,cost;
scanf("%d%d%d", &from, &to, &cost);
add(from, to, cost, );
add(to, from, cost, );
}
s=;
t=n;
printf("%d\n", max_cost_flow());
}
return ;
}

POJ 2135 Farm Tour 最小费用流的更多相关文章

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

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

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

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

  3. poj 2135 Farm Tour 【无向图最小费用最大流】

    题目:id=2135" target="_blank">poj 2135 Farm Tour 题意:给出一个无向图,问从 1 点到 n 点然后又回到一点总共的最短路 ...

  4. 网络流(最小费用最大流):POJ 2135 Farm Tour

    Farm Tour Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: ...

  5. POJ 2135 Farm Tour (最小费用最大流模板)

    题目大意: 给你一个n个农场,有m条道路,起点是1号农场,终点是n号农场,现在要求从1走到n,再从n走到1,要求不走重复路径,求最短路径长度. 算法讨论: 最小费用最大流.我们可以这样建模:既然要求不 ...

  6. POJ 2135.Farm Tour 消负圈法最小费用最大流

    Evacuation Plan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4914   Accepted: 1284   ...

  7. POJ 2135 Farm Tour(最小费用最大流)

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

  8. POJ 2135 Farm Tour (费用流)

    [题目链接] http://poj.org/problem?id=2135 [题目大意] 有一张无向图,求从1到n然后又回来的最短路 同一条路只能走一次 [题解] 题目等价于求从1到n的两条路,使得两 ...

  9. POJ 2135 Farm Tour [最小费用最大流]

    题意: 有n个点和m条边,让你从1出发到n再从n回到1,不要求所有点都要经过,但是每条边只能走一次.边是无向边. 问最短的行走距离多少. 一开始看这题还没搞费用流,后来搞了搞再回来看,想了想建图不是很 ...

随机推荐

  1. 关于subGradent descent和Proximal gradient descent的迭代速度

    clc;clear; D=1000;N=10000;thre=10e-8;zeroRatio=0.6; X = randn(N,D); r=rand(1,D); r=sign(1-2*r).*(2+2 ...

  2. [JS]Ext最新GPL版

    https://www.sencha.com/legal/gpl/ Sencha Ext JS.Sencha Touch.Sencha GXT http://cdn.sencha.com/ext/gp ...

  3. Weblogic页面应用查询oracle数据库后台报错或页面日期格式显示错误

    问题:在生产环境中有两台WEB服务器,分别为227和228,部署的应用代码都是每日同步的,两边完全一致,但是某些页面查询数据时,227无结果,并且后台报java数组越界的错误,而228一切正常.经开发 ...

  4. 5.openssl dgst

    该伪命令用于生成文件的信息摘要,也可以进行数字签名,验证数字签名. 首先要明白,要进行数字签名,需要计算出特征码即数字摘要,然后使用私钥对数字摘要进行签名.特征码使用md5,sha等计算出. 对象只能 ...

  5. React Native 实现页面动态切换

    第一步. 初始化子View constructor(props){ super(props); this.state = { isChange : true, itemView : (<Text ...

  6. SQL 高效分页查询

    declare @page int --页码 declare @pagecount int;--每一页显示的记录数 select * from (select *,row_number() over( ...

  7. table变色

    <!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...

  8. 《IT蓝豹》挑战独立开发项目能力

    做了5年的android开发,今天没事写写刚入行不久的时候第一次独立开发项目的心得体会,    当时我刚工作8个月,由于公司运营不善倒闭了,在2011年3月份我开始准备跳槽,    看了一周andro ...

  9. 使用Linux碎解一

    一.安装VMwarestation. 二.安装Linux系统(这里是CentOS7) 步骤: #==========================part1===================== ...

  10. Vue.JS入门学习随笔

    PS:先说说学习Vue的缘由吧,学习完了React之后,突然发现又出了一款叫做vue的框架,而且据说可以引领又一波新框架的潮流,我容易吗我!!!   Vue.js(读音 /vjuː/, 类似于view ...