POJ 2135 Farm Tour && HDU 2686 Matrix && HDU 3376 Matrix Again 费用流求来回最短路
累了就要写题解,近期总是被虐到没脾气。
来回最短路问题貌似也能够用DP来搞。只是拿费用流还是非常方便的。
能够转化成求满流为2 的最小花费。一般做法为拆点,对于 i 拆为2*i 和 2*i+1。然后连一条流量为1(花费依据题意来定) 的边来控制每一个点仅仅能通过一次。
额外加入source和sink来控制满流为2。
代码都雷同,以HDU3376为例。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cmath>
#include <stack>
#include <map> #pragma comment(linker, "/STACK:1024000000");
#define EPS (1e-8)
#define LL long long
#define ULL unsigned long long
#define _LL __int64
#define INF 0x3f3f3f3f
#define Mod 6000007 using namespace std; const int EDGE = 6000000,POINT = 730000; struct E
{
int Max,cost,v,next;
}edge[EDGE]; int head[POINT]; int Top; void Link(int u,int v,int w,int cost)
{
edge[Top].v = v;
edge[Top].Max = w;
edge[Top].cost = cost;
edge[Top].next = head[u];
head[u] = Top++;
} int Map[610][610]; int dis[POINT],cur[POINT],flow[POINT];
bool mark[POINT]; void Updata(int site,int flow,int &cost)
{
for(;cur[site] != -1; site = edge[cur[site]^1].v)
{
edge[cur[site]].Max -= flow;
edge[cur[site]^1].Max += flow;
cost += edge[cur[site]].cost * flow;
}
} queue<int> q; int spfa(int S,int T,int &cost)
{
memset(mark,false,sizeof(mark));
memset(dis,INF,sizeof(dis)); cur[S] = -1,dis[S] = 0,flow[S] = INF; q.push(S); int f,t; while(q.empty() == false)
{
f = q.front();
q.pop();
mark[f] = false; for(int p = head[f];p != -1; p = edge[p].next)
{
t = edge[p].v;
if(edge[p].Max && dis[t] > dis[f] + edge[p].cost)
{
dis[t] = dis[f] + edge[p].cost;
cur[t] = p;
flow[t] = min(flow[f],edge[p].Max); if(mark[t] == false)
{
mark[t] = true;
q.push(t);
}
}
}
} if(dis[T] == INF)
return 0;
Updata(T,flow[T],cost);
return flow[T];
} int Cal_Max_Flow_Min_Cost(int S,int T,int n)
{
int temp,flow = 0,cost = 0; do
{
temp = spfa(S,T,cost);
flow += temp;
}while(temp);
return cost;
} inline int Cal(int x,int y,int n)
{
return ((x-1)*n+y)*2-1;
} int main()
{
int n;
int i,j; while(scanf("%d",&n) != EOF)
{
memset(head,-1,sizeof(head));
Top = 0; for(i = 1;i <= n; ++i)
{
for(j = 1;j <= n; ++j)
{
scanf("%d",&Map[i][j]);
if(i == j && (i == 1 || i == n))
Link(Cal(i,j,n),Cal(i,j,n)+1,2,-Map[i][j]);
else
Link(Cal(i,j,n),Cal(i,j,n)+1,1,-Map[i][j]);
Link(Cal(i,j,n)+1,Cal(i,j,n),0,Map[i][j]);
}
} for(i = 1;i <= n; ++i)
{
for(j = 1;j <= n; ++j)
{
if(j < n)
{
Link(Cal(i,j,n)+1,Cal(i,j+1,n),1,0);
Link(Cal(i,j+1,n),Cal(i,j,n)+1,0,0);
}
if(i < n)
{
Link(Cal(i,j,n)+1,Cal(i+1,j,n),1,0);
Link(Cal(i+1,j,n),Cal(i,j,n)+1,0,0);
}
}
} printf("%d\n",-Cal_Max_Flow_Min_Cost(1,n*n*2,n*n*2) - Map[1][1] - Map[n][n]);
}
return 0;
}
POJ 2135 Farm Tour && HDU 2686 Matrix && HDU 3376 Matrix Again 费用流求来回最短路的更多相关文章
- 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 (费用流)
[题目链接] http://poj.org/problem?id=2135 [题目大意] 有一张无向图,求从1到n然后又回来的最短路 同一条路只能走一次 [题解] 题目等价于求从1到n的两条路,使得两 ...
- 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 消负圈法最小费用最大流
Evacuation Plan Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4914 Accepted: 1284 ...
- POJ 2135 Farm Tour 最小费用流
两条路不能有重边,既每条边的容量是1.求流量为2的最小费用即可. //#pragma comment(linker, "/STACK:1024000000,1024000000") ...
- POJ 2135 Farm Tour [最小费用最大流]
题意: 有n个点和m条边,让你从1出发到n再从n回到1,不要求所有点都要经过,但是每条边只能走一次.边是无向边. 问最短的行走距离多少. 一开始看这题还没搞费用流,后来搞了搞再回来看,想了想建图不是很 ...
随机推荐
- GPU下train 模型出现nan
When training on GPU, the error "Model diverged with loss = NaN" is often caused by a sotm ...
- Java实现继承过程概述
super(); 在调用子类的构造器的时候,如果没有显示的写出 super(); ,那么,编译器会在佛那个加上 super(); 无参构造器 如果想调用父类的有参构造器,那么,必须显示的调用,编译器不 ...
- 压缩跟踪Compressive Tracking(转)
这位博主总结的实在太好了,从原理到论文到代码,连论文都不用看:论文:http://blog.csdn.net/zouxy09/article/details/8118360 代码部分:http://b ...
- js中的事件委托或是事件代理详解(转载)
起因: 1.这是前端面试的经典题型,要去找工作的小伙伴看看还是有帮助的: 2.其实我一直都没弄明白,写这个一是为了备忘,二是给其他的知其然不知其所以然的小伙伴们以参考: 概述: 那什么叫事件委托呢?它 ...
- Java 内存模型 ,一篇就够了!
Java 虚拟机 我们都知道 Java 语言的可以跨平台的,这其中的核心是因为存在 Java 虚拟机这个玩意.虚拟机,顾名思义就是虚拟的机器,这不是真实存在的硬件,但是却可以和不同的底层平台进行交 ...
- python标准库--functools.partial
官方相关地址:https://docs.python.org/3.6/library/functools.html 一.简单介绍: functools模块用于高阶函数:作用于或返回其他函数的函 ...
- BZOJ3779 : 重组病毒
一个点的感染时间为它到根路径上虚边数+1. 用Link-Cut Tree模拟虚实边切换,每次切换时等价于在一段或两段DFS序区间更新,线段树维护即可. 时间复杂度$O(n\log^2n)$. #inc ...
- bzoj 2209 括号序列
反转操作 + 翻转操作 = 对称操作 因为上面三个操作都是自己的逆操作,所以我们只需要实现对称操作和反转操作,就可以搞定翻转操作. #include <cstdio> #include & ...
- 【学习笔记】python的代码块(吐槽)
曾经我以为python是像pascal那样begin开始end结束, 直到今天…… 我才知道python是用缩进作为代码段标识的…… >>> def test(n): ... if ...
- 如果想使用GIT Extentions的解决冲突窗口,安装时必须勾选KDIFF3
因为第一次安装时,没有选择同时安装KDIFF3,所以遇到冲突时,点击合并,始终无法弹出合并窗口. 还有一个问题,就是在安装时,要选择OpenSSH,不要选择PuTTY.