poj 2135 Farm Tour 【无向图最小费用最大流】
题目: id=2135" target="_blank">poj 2135 Farm Tour
题意:给出一个无向图,问从 1 点到 n 点然后又回到一点总共的最短路。
分析:这个题目不读细致的话可能会当做最短路来做,最短路求出来的不一定是最优的,他是两条分别最短,但不一定是和最短。
我们能够用费用流来非常轻易的解决,建边容量为1,费用为边权。然后源点s连 1 。费用0 。容量 2 ,n点连接汇点,容量2,费用0,,就能够了。
注意这个题目是无向图,所以要建双向边。
AC代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
const int N = 1050;
const int inf = 0x3f3f3f3f;
#define Del(a,b) memset(a,b,sizeof(a))
struct Node
{
int from,to,cap,flow,cost;
};
vector<int> v[N];
vector<Node> e;
void add_Node(int from,int to,int cap,int cost)
{
e.push_back((Node){from,to,cap,0,cost});
e.push_back((Node){to,from,0,0,-cost});
int len = e.size()-1;
v[to].push_back(len);
v[from].push_back(len-1);
}
int vis[N],dis[N];
int father[N],pos[N];
bool BellManford(int s,int t,int& flow,int& cost)
{
Del(dis,inf);
Del(vis,0);
queue<int> q;
q.push(s);
vis[s]=1;
father[s]=-1;
dis[s] = 0;
pos[s] = inf;
while(!q.empty())
{
int f = q.front();
q.pop();
vis[f] = 0;
for(int i=0; i<v[f].size(); i++)
{
Node& tmp = e[v[f][i]];
if(tmp.cap>tmp.flow && dis[tmp.to] > dis[f] + tmp.cost)
{
dis[tmp.to] = dis[f] + tmp.cost;
father[tmp.to] = v[f][i];
pos[tmp.to] = min(pos[f],tmp.cap - tmp.flow);
if(vis[tmp.to] == 0)
{
vis[tmp.to]=1;
q.push(tmp.to);
}
}
}
}
if(dis[t] == inf)
return false;
flow += pos[t];
cost += dis[t]*pos[t];
for(int u = t; u!=s ; u = e[father[u]].from)
{
e[father[u]].flow += pos[t];
e[father[u]^1].flow -= pos[t];
}
return true;
}
int Mincost(int s,int t)
{
int flow = 0, cost = 0;
while(BellManford(s,t,flow,cost)){}
return cost;
}
void Clear(int x)
{
for(int i=0; i<=x; i++)
v[i].clear();
e.clear();
} int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
for(int i=0;i<m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add_Node(x,y,1,z);
add_Node(y,x,1,z);
}
int s = 0 ,t = n+1;
add_Node(s,1,2,0);
add_Node(n,t,2,0);
int ans = Mincost(s,t);
printf("%d\n",ans);
Clear(n+1);
}
return 0;
}
poj 2135 Farm Tour 【无向图最小费用最大流】的更多相关文章
- POJ 2135 Farm Tour (最小费用最大流模板)
题目大意: 给你一个n个农场,有m条道路,起点是1号农场,终点是n号农场,现在要求从1走到n,再从n走到1,要求不走重复路径,求最短路径长度. 算法讨论: 最小费用最大流.我们可以这样建模:既然要求不 ...
- 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(最小费用最大流,变形)
题意:给一个无向图,FJ要从1号点出发到达n号点,再返回到1号点,但是路一旦走过了就会销毁(即回去不能经过),每条路长度不同,那么完成这趟旅行要走多长的路?(注:会有重边,点号无序,无向图!) 思路: ...
- poj 2135 Farm Tour【 最小费用最大流 】
第一道费用流的题目--- 其实---还是不是很懂,只知道沿着最短路找增广路 建图 源点到1连一条容量为2(因为要来回),费用为0的边 n到汇点连一条容量为2,费用为0的边 另外的就是题目中输入的了 另 ...
- POJ2135 Farm Tour(最小费用最大流)
题目问的是从1到n再回到1边不重复走的最短路,本质是找1到n的两条路径不重复的尽量短的路. #include<cstdio> #include<cstring> #includ ...
- 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
Farm Tour Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: ...
- POJ 2135.Farm Tour 消负圈法最小费用最大流
Evacuation Plan Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4914 Accepted: 1284 ...
- POJ 2195 Going Home(最小费用最大流)
http://poj.org/problem?id=2195 题意 : N*M的点阵中,有N个人,N个房子.让x个人走到这x个房子中,只能上下左右走,每个人每走一步就花1美元,问当所有的人都归位了之 ...
随机推荐
- String,创建对象问题
String str=new String("aaa"); 这行代码究竟创建了几个String对象呢?答案是2个,而不是3个.由于new String("aaa" ...
- VTK初始化New返回Null问题
原文链接:http://www.cppblog.com/mythma/archive/2013/08/02/vtk-6-new-null.html 在使用VTK6.0时候,会遇到X::New()返回为 ...
- Arduino ULN2009驱动步进电机
一.实物图 二.例子代码 注:代码来自老外 http://www.4tronix.co.uk/arduino/Stepper-Motors.php 功能:控制电机正反转 // This Arduino ...
- C 预处理程序指令(CPP)
#include 文件 提供的东西 stdio 提供 FILE.stdin.stdout.stderr 和 fprintf() 函数系列 stdlib 提供 malloc().calloc()和 re ...
- xpath定位实列
这次将讲Xpath定位! 什么是Xpath: Path就是路径,xpath也类似,就像 在DOS中 D:\Auto\jack. Xpath的使用: 1:在火狐下载 FirePath 插件(图1),然后 ...
- eas之数据融合
1.如何进行自由融合自由融合无须指定区域,KDTable将根据指定的融合模式,融合相邻且值相等的单元.// 自由行融合table.getMergeManager().setMergeMode(KDTM ...
- 【剑指Offer】56、删除链表中重复的结点
题目描述: 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针. 例如,链表1->2->3->3->4->4-> ...
- 目录-Linux
Linux文件系统: Linux: glibc 程序编译方式: 动态链接 静态编译 进程的类型: 终端:硬件设备,关联一个用户接口 与终端相关:通过终端启动 与终端无关:操作引导启动过程当中自动启动 ...
- Problem 19
Problem 19 You are given the following information, but you may prefer to do some research for yours ...
- python爬虫08 | 你的第二个爬虫,要过年了,爬取豆瓣最受欢迎的250部电影慢慢看
马上就要过年啦 过年在家干啥咧 准备好被七大姑八大姨轮番「轰炸」了没? 你的内心 os 是这样的 但实际上你是这样的 应付完之后 闲暇时刻不妨看看电影 接下来咱们就来爬取豆瓣上评分最高的 250部电影 ...