Farm Tour

Time Limit: 1000ms
Memory Limit: 65536KB

This problem will be judged on PKU. Original ID: 2135
64-bit integer IO format: %lld      Java class name: Main

 
When FJ's friends visit him on the farm, he likes to show them around.
His farm comprises N (1 <= N <= 1000) fields numbered 1..N, the
first of which contains his house and the Nth of which contains the big
barn. A total M (1 <= M <= 10000) paths that connect the fields
in various ways. Each path connects two different fields and has a
nonzero length smaller than 35,000.

To show off his farm in the best way, he walks a tour that starts at
his house, potentially travels through some fields, and ends at the
barn. Later, he returns (potentially through some fields) back to his
house again.

He wants his tour to be as short as possible, however he doesn't
want to walk on any given path more than once. Calculate the shortest
tour possible. FJ is sure that some tour exists for any given farm.

Input

* Line 1: Two space-separated integers: N and M.

* Lines 2..M+1: Three space-separated integers that define a path: The starting field, the end field, and the path's length.

Output

A single line containing the length of the shortest tour.

Sample Input

4 5
1 2 1
2 3 1
3 4 1
1 3 2
2 4 2

Sample Output

6

  这题就是在一个无向图中找出两条从点1到点n的路径,同时要求路程最短。
  于是贴最小费用最大流模板就AC啦。
 #include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int INF=;
const int maxn=,maxm=;
int cnt,fir[maxn],nxt[maxm],to[maxm],cap[maxm],val[maxm],dis[maxn],path[maxn]; void addedge(int a,int b,int c,int v)
{
nxt[++cnt]=fir[a];to[cnt]=b;cap[cnt]=c;val[cnt]=v;fir[a]=cnt;
}
int S,T;
int Spfa()
{
queue<int>q;
memset(dis,,sizeof(dis));
q.push(S);dis[S]=;
while(!q.empty())
{
int node=q.front();q.pop();
for(int i=fir[node];i;i=nxt[i])
if(cap[i]&&dis[node]+val[i]<dis[to[i]]){
dis[to[i]]=val[i]+dis[node];
path[to[i]]=i;
q.push(to[i]);
}
}
return dis[T]==dis[T+]?:dis[T];
} int Aug()
{
int p=T,f=INF;
while(p!=S)
{
f=min(f,cap[path[p]]);
p=to[path[p]^];
}
p=T;
while(p!=S)
{
cap[path[p]]-=f;
cap[path[p]^]+=f;
p=to[path[p]^];
}
return f;
} int MCMF()
{
int ret=,d;
while(d=Spfa())
ret+=Aug()*d;
return ret;
} void Init(int n)
{
cnt=;S=;T=n+;
for(int i=;i<=n;i++)fir[i]=;
} int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
Init(n);
int a,b,v;
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&v);
addedge(a,b,,v);
addedge(b,a,,-v);
addedge(b,a,,v);
addedge(a,b,,-v);
}
addedge(S,,,);
addedge(,S,,);
addedge(n,T,,);
addedge(T,n,,);
printf("%d\n",MCMF());
}
return ;
}
												

网络流(最小费用最大流):POJ 2135 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 (最小费用最大流模板)

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

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

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

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

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

  6. HDU 6118 度度熊的交易计划(网络流-最小费用最大流)

    度度熊参与了喵哈哈村的商业大会,但是这次商业大会遇到了一个难题: 喵哈哈村以及周围的村庄可以看做是一共由n个片区,m条公路组成的地区. 由于生产能力的区别,第i个片区能够花费a[i]元生产1个商品,但 ...

  7. POJ 2135 Farm Tour(最小费用最大流,变形)

    题意:给一个无向图,FJ要从1号点出发到达n号点,再返回到1号点,但是路一旦走过了就会销毁(即回去不能经过),每条路长度不同,那么完成这趟旅行要走多长的路?(注:会有重边,点号无序,无向图!) 思路: ...

  8. poj 2135 Farm Tour 最小费用最大流建图跑最短路

    题目链接 题意:无向图有N(N <= 1000)个节点,M(M <= 10000)条边:从节点1走到节点N再从N走回来,图中不能走同一条边,且图中可能出现重边,问最短距离之和为多少? 思路 ...

  9. POJ 2135 Farm Tour 最小费用流

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

随机推荐

  1. Rouh set 入门知识2(基础定义篇)

    接上一篇,简单说明一下知识库的关系,设K1=(U,S1)和K2=(U,S2)为知识库 1.如果IND(S1)=IND(S2),即U/IND(S1)=U/IND(S2),则知识库K1与知识库K2是等价的 ...

  2. css 实现页面加载中等待效果

    <!DOCTYPE html> <html> <head> <title>css实现页面加载中,请稍候效果</title> <meta ...

  3. vs中debug和release版本的区别(转)

    vs中的程序有debug和release两个版本,Debug通常称为调试版本,通过一系列编译选项的配合,编译的结果通常包含调试信息,而且不做任何优化,以为开发 人员提供强大的应用程序调试能力.而Rel ...

  4. SlidingMenu侧换菜单的导入

    对于Adt-22.3有一种使用SlidingMenu(侧滑菜单的方式),直接加你放到lib文件夹下

  5. 把cygwin加入右键菜单

    第一步:修改windows注册表 1·开始->运行(或者win键+R),输入REGEDIT,回车,打开注册表编辑器: 2·找到HKEY_CLASSES_ROOT\Directory\Backgr ...

  6. java_设计模式_观察者模式_Observer Pattern(2016-07-27)

    看了好几篇文章,最终还是觉得<Head First 设计模式>举得例子比较符合观察者模式. 观察者模式概述: 观察者模式有时被称作发布/订阅模式,它定义了一种一对多的依赖关系,让多个观察者 ...

  7. 谢尔排序/缩减增量排序(C++)

    谢尔排序/缩减增量排序(C++) 谢尔排序/缩减增量排序: 他通过比较相距一定间隔的元素来工作,各趟比较所用的距离随着算法的进行而减小,直到只比较相邻元素的最后一趟排序为止.(好复杂) 看了一下实现代 ...

  8. WPF WebBrowser 不可见问题的解析[转]

    问题概述: 1.在Xaml中加入WebBrowser(不论是WPF中的控件,还是Winform中的控件) 2.设置Window Background="Transparent" A ...

  9. .NET Framework(二)

    在上一篇的随笔中,我们在理论层面上大致说明了.NET Framework的工作机制,内容的确比较晦涩难懂,但是还是希望大家有时候可以看看.我个人觉得,编程不是一味的敲代码,当自己遇到瓶颈的时候,可以多 ...

  10. 根据打开页面加载不同Js

    根据打开页面加载不同Js //根据打开页面加载不同JS $(document).ready(function(){ var href = document.URL; /*获取当前页面的URL*/ if ...