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,不要求所有点都要经过,但是每条边只能走一次.边是无向边. 问最短的行走距离多少. 一开始看这题还没搞费用流,后来搞了搞再回来看,想了想建图不是很 ...
随机推荐
- JS post提交表单
js post方式提交表单有两种办法,1:AJAX提交 2:在JS里拼出一个form,然后submit 第二种办法的代码 //这个主要是解决给password MD5 var email = 'ema ...
- ubuntu下root和安装mysql
sudo password创建新的root密码: 1.用当前登录用户打开终端,在终端输入命令 sudo passwd,输入当前用户的密码然后回车 2.会提示输入新密码,输入完成后回车(http://w ...
- Eclipse添加SVN插件:导入项目+上传项目+更新项目
首先在Eclipse中安装SVN插件,方法同安装Pydev相同 首先点击help,然后点击Install New Software 然后在弹出的窗口中点击Add,再在新弹出的窗口中的url栏输入如下内 ...
- 011.KVM-V2V迁移
一 虚拟化存储池 1.1 创建虚拟化存储池 [root@kvm-host ~]# mkdir -p /data/vmfs 1.2 定义存储池与目录 [root@kvm-host ~]# virsh p ...
- JSONObject 自定义过滤配置
一.自定义过滤器说明 PropertyPreFilter 根据PropertyName判断是否序列化 PropertyFilter 根据PropertyName和PropertyValue来判断是否 ...
- jstat命令总结
jvm统计信息监控工具 一. jstat是什么 jstat是JDK自带的一个轻量级小工具.全称"Java Virtual Machine statistics monitoring tool ...
- 初识thinkphp(2)
thinkphp的url路径的表示格式为 http://ip/tp/public/index.php/模块/控制器/操作 这里url最后的操作就是类里面的函数. 0x01:url访问格式 官方文档中有 ...
- 漏洞利用查询工具sandi
漏洞利用查询工具sandi 在渗透测试中,一旦发现漏洞,就需要查找该漏洞的利用方式.由于漏洞众多,就需要渗透测试人员从海量的漏洞信息找出可用攻击载荷.Kali Linux内置了一个查询工具sand ...
- 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem A. Alien Visit 计算几何
Problem A. Alien Visit 题目连接: http://codeforces.com/gym/100714 Description Witness: "First, I sa ...
- VMware 使用本机代理上网
灰机使用方法 VMware 安装方法 首先解决主机的配置 1.查询本机 IP 地址,使用 ipconfig /all 2.更改小灰机的设置 3.虚拟机设置 4.Ubuntu 设置