Farm Tour

题目:

约翰有N块地,家在1号,而N号是个仓库。农场内有M条道路(双向的),道路i连接这ai号地和bi号地,长度为ci。

约翰希望依照从家里出发,经过若干地后达到仓库。然后再返回家中。假设要求往返不能经过同一条道路两次,求參观路线总长度最小值。

算法分析:

用最短路求解然后在删除第一次最短路中的边在求解一次最短路。这样是否可行?应该立即就能找到反例证明该方法不能总得到最优结果吧。

于是我们放弃把问题当作去和回的这样的想法,转而将问题当作求从1号顶点到N号顶点的两条没有公共边的路径有怎样?这样转换之后。不就是求流量为2的最小费用了,由于道路是双向的。

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std; /* 流量限制为f下。求解最小费用
时间复杂度:O(F mlogn) */
typedef pair<int,int> P;
const int INF = 1 << 30;
const int MAXN = 1000 + 10; struct Edge{
int to,cap,cost,rev;
Edge(){};
Edge(int _to,int _cap,int _cost,int _rev)
:to(_to),cap(_cap),cost(_cost),rev(_rev){};
}; int V; //顶点
int N,M,S,T;
vector<Edge> G[MAXN];
int h[MAXN]; //顶点的势
int dist[MAXN]; //最短距离
int prevv[MAXN],preve[MAXN]; //最短路中德前驱节点和相应的边 void init(){
S = 1; T = N; V = T + 1;
for(int i = 0;i <= V;++i)
G[i].clear();
} //从图中添加一条从from到to容量为cap费用为cost的边
void addEdge(int from,int to,int cap,int cost){
G[from].push_back(Edge(to,cap,cost,G[to].size()));
G[to].push_back(Edge(from,0,-cost,G[from].size() - 1));
} //求解从s到t流量为f的最小费用流
//假设没有流量为f的流。则返回-1
int min_cost_flow(int s,int t,int f){ //s:起点 t:终点 f:流量限制
int res = 0;
fill(h,h + V,0); //初始化h
while(f > 0){ //使用Dijkstra算法更新h
priority_queue<P,vector<P>,greater<P> > Q;
fill(dist,dist + V,INF);
dist[s] = 0;
Q.push(P(0,s));
while(!Q.empty()){
P p = Q.top(); Q.pop();
int v = p.second;
if(dist[v] < p.first) continue;
for(int i = 0;i < G[v].size();++i){
Edge& e = G[v][i];
int tmp = dist[v] + e.cost + h[v] - h[e.to];
if(e.cap > 0 && dist[e.to] > tmp){
dist[e.to] = tmp;
prevv[e.to] = v;
preve[e.to] = i;
Q.push(P(dist[e.to],e.to));
}
}
} //while //不能增广
if(dist[t] == INF){
return -1;
}
for(int v = 1;v <= V;++v) h[v] += dist[v];
int d = f;
for(int v = t;v != s;v = prevv[v]){
d = min(d,G[prevv[v]][preve[v]].cap);
}
f -= d;
res += d * h[t];
for(int v = t; v != s;v = prevv[v]){
Edge& e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
} int main()
{
//freopen("Input.txt","r",stdin); while(~scanf("%d%d",&N,&M)){
init();
int a,b,c;
for(int i = 0;i < M;++i){
scanf("%d%d%d",&a,&b,&c);
addEdge(a,b,1,c);
addEdge(b,a,1,c);
}
printf("%d\n",min_cost_flow(S,T,2));
}
return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

POJ Farm Tour的更多相关文章

  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 2351 Farm Tour (最小费用最大流)

    Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17230   Accepted: 6647 Descri ...

  5. POJ2135 Farm Tour

      Farm Tour Time Limit: 2MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Description ...

  6. POJ2135 Farm Tour —— 最小费用最大流

    题目链接:http://poj.org/problem?id=2135 Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  7. [网络流]Farm Tour(费用流

    Farm Tour 题目描述 When FJ's friends visit him on the farm, he likes to show them around. His farm compr ...

  8. Farm Tour(最小费用最大流模板)

    Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18150   Accepted: 7023 Descri ...

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

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

随机推荐

  1. 02C#基础(1)

    1.关键字 C#中定义了很多关键字,关键字是构成C#基本语法的,不用去背,用的多就记住了 2.标识符 标识符是用来给类.方法.变量等命名的 命名规则:  (1)由字母.中文(不推荐).数字.下划线组成 ...

  2. 知识点总结之HTML篇

    1.标签语义化: ①.在不依赖样式的情况下,页面能够呈现清晰的结构. ②.如果使用者有视觉障碍,屏幕阅读器会完全根据你的标记来选择读取你的网页. ③.有利于搜索引擎依赖于标记来确定上下文和各个关键字的 ...

  3. POJ 2010 Moo University - Financial Aid( 优先队列+二分查找)

    POJ 2010 Moo University - Financial Aid 题目大意,从C头申请读书的牛中选出N头,这N头牛的需要的额外学费之和不能超过F,并且要使得这N头牛的中位数最大.若不存在 ...

  4. equals和hashcode

    java当中所有的类都继承于Object这个基类,在object中的基类定义了一个equals方法,public boolean equals(Object obj) {     return (th ...

  5. Java获取昨天的时间

    Calendar   cal   =   Calendar.getInstance();  cal.add(Calendar.DATE,   -1);  String yesterday = new ...

  6. C++ Primer 5th 第10章 泛型算法

    练习10.1:头文件algorithm中定义了一个名为count的函数,它类似find,接受一对迭代器和一个值作为参数.count返回给定值在序列中出现的次数.编写程序,读取int序列存入vector ...

  7. ActiveX相关

    ActiveX 1.创建ActiveXhttp://blog.csdn.net/fww330666557/article/details/6533118 继承IObjectSafety接口http:/ ...

  8. Python自动化运维之21、CSS

    一.css简介 CSS 是 Cascading Style Sheets的缩写,称为层叠样式表,用来设计网页的样式布局,以及大小来适应不同的屏幕等,使网页的样式和网页数据分离, 二.导入css 导入c ...

  9. LightOJ 1317 第六周比赛A题

    A - A Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu   Description Y ...

  10. jquery ajax用例样板

    $.ajax({ url: '${managerPath}/customer/updateOrder.do', type: 'POST', async: false, data: { id: date ...