累了就要写题解,近期总是被虐到没脾气。

来回最短路问题貌似也能够用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 &amp;&amp; HDU 2686 Matrix &amp;&amp; HDU 3376 Matrix Again 费用流求来回最短路的更多相关文章

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

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

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

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

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

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

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

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

  5. POJ 2135 Farm Tour (费用流)

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

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

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

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

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

  8. POJ 2135 Farm Tour 最小费用流

    两条路不能有重边,既每条边的容量是1.求流量为2的最小费用即可. //#pragma comment(linker, "/STACK:1024000000,1024000000") ...

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

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

随机推荐

  1. Java编程的逻辑 (72) - 显式条件

    ​本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...

  2. Linux系统运维笔记(五),CentOS 6.4安装java程序

    Linux系统运维笔记(五),CentOS 6.4安装java程序 用eclipse编译通的java程序,现需要实施到服务器.实施步骤: 一,导出程序成jar包. 1,在主类编辑界面点右健,选  ru ...

  3. List集合去除重复对象及equals()、hashCode()方法的作用

    原文:https://blog.csdn.net/freelander_j/article/details/52211010 在java中,要将一个集合中重复的对象除去,如果这个集合中的数据类型是基本 ...

  4. Webpack按需加载一切皆模块

    前言 在学习 Webpack 之前,我们需要了解一个概念:模块. 何为模块? 如果你曾学过 Java , C# 之类的语言,一定会知道 Java 中的 import 或 C# 中的 using 吧? ...

  5. PHP 数字序数&字母序数 相互转化

    序数从1开始  即 A=1  而非 A=0 /** * 数字序列转字母序列 * @param $int * @param int $start * @return string|bool */ fun ...

  6. 数据包注入重放工具aireplay-ng

    数据包注入重放工具aireplay-ng   aireplay-ng是aircrack-ng组件包的一个工具.它可以注入和重放数据帧,用于后期的WEP.WPA-PSK破解.它提供九种攻击模式,包括死亡 ...

  7. 运行程序,解读this指向---case6

    function Parent() { this.a = 1; this.b = [1, 2, this.a]; this.c = { ckey: 5 }; this.show = function ...

  8. leetcode 二叉搜索树中第K小的元素 python

          二叉搜索树中第K小的元素     给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元 ...

  9. ant design Modal关闭时清除数据的解决方案

    背景:modal组件关闭时不清除数据,原来输入的数据还存在 解决方案: 1.modal的api:destroyOnClose 2.手动控制modal的销毁 this.state = { destroy ...

  10. 如何快速将Linux文件系统迁移到Azure存储

    概述 前一段时间一直在给一个客户将原先搭载在Linux(客户使用的是CentOS 7.0)上的NFS快速迁移到Azure存储上,并且为了保证数据完整性还需要另开一个存储做冷备,架构图如下: 通过Cli ...