POJ 2135 Farm Tour 最小费用流
两条路不能有重边,既每条边的容量是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 最小费用流的更多相关文章
- 【网络流#9】POJ 2135 Farm Tour 最小费用流 - 《挑战程序设计竞赛》例题
[题意]给出一张无向图,从1开始到n,求两条没有公共边的最短路,使得路程总和最小 每条边的权值设为费用,最大流量设为1,然后就是从源点到汇点流量为2的最小费用流. 因为是规定了流量,新建一个源点和一个 ...
- 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 【无向图最小费用最大流】
题目: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: ...
- POJ 2135 Farm Tour (最小费用最大流模板)
题目大意: 给你一个n个农场,有m条道路,起点是1号农场,终点是n号农场,现在要求从1走到n,再从n走到1,要求不走重复路径,求最短路径长度. 算法讨论: 最小费用最大流.我们可以这样建模:既然要求不 ...
- POJ 2135.Farm Tour 消负圈法最小费用最大流
Evacuation Plan Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4914 Accepted: 1284 ...
- POJ 2135 Farm Tour(最小费用最大流)
Description When FJ's friends visit him on the farm, he likes to show them around. His farm comprise ...
- POJ 2135 Farm Tour (费用流)
[题目链接] http://poj.org/problem?id=2135 [题目大意] 有一张无向图,求从1到n然后又回来的最短路 同一条路只能走一次 [题解] 题目等价于求从1到n的两条路,使得两 ...
- POJ 2135 Farm Tour [最小费用最大流]
题意: 有n个点和m条边,让你从1出发到n再从n回到1,不要求所有点都要经过,但是每条边只能走一次.边是无向边. 问最短的行走距离多少. 一开始看这题还没搞费用流,后来搞了搞再回来看,想了想建图不是很 ...
随机推荐
- 数据库的NULL值讨论
有许多关于数据库设计中NULL的讨论,我个人的设计习惯是,不使用NULL值. 我所设计所有表都是Not Null的字段的,尤其是我主要做数据仓库的表设计.刚开始使用数据库时,就栽了一次.一个Group ...
- 执行Python "/bin/usr/python: bad interpreter: No such file or directory" 错误
今天在电脑上写了一个Python脚本,写好之后用ftp传上去,然后执行/var/www/cron.py,结果报错,/bin/usr/python: bad interpreter: No such f ...
- Geometry Stage in Rendering pipeline (读书笔记2 --- Real-Time rendering)
Geometry Stage一般包含下面几个阶段 1. Model & View Transform(模型和视图变换) --- 模型空间--> 世界空间 模型变换:每个模型经过模型变换来 ...
- php工作笔记7-摇动手机,发出请求响应
1.调用
- IE8下服务端获取客户端文件的路径为C:/fakePath问题的解决方案
上一篇文章上提到,IE8下服务端获取客户端文件的路径时,会变成C:/fakePath问题,于是乎通过文件路径去获得文件大小就失败了. 上网搜了一下,主要原因是IE8因为安全考虑,在上传文件时屏蔽了真实 ...
- Jmeter循环控制
Jmeter循环控制 很多时候,在做接口测试时,系统处理请求需要一段时间后才能返回信息,而下一个请求需要系统返回信息后才能进行处理,这时候通常需要加入循环控制器,来验证系统是否是否返回了处理后的信息. ...
- php获取时间问题,用默认配置读到本地时间。。。。。
用date获取到时间有8小时时差 因为php用date获取到的是格林威治时区的时间,而大陆时间和格林威治时间有8个小时时差,所以.... 修改:网上有各种修改方式: 比如:在程序中添加时间的初始化的语 ...
- html信息提示框
1.span <span title="提示信息" data-container="body" data-toggle="inf" d ...
- background-position还可以这样用
文章同步至微信公众号:http://mp.weixin.qq.com/s?__biz=MzAxMzgwNDU3Mg==&mid=401626453&idx=1&sn=6af07 ...
- JQuery 概况