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

Description

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

Source

题意:n个点 m条路 每条路都有一个长度 现在定义从1节点出发 到n节点  再从n节点回1节点
这两条路不能有重边 问你最短路多少
其实 这可以简化成一个求做小费用的网络流,每条边的流量设为1 长度为他们的费用 
自己定义一个汇点和出发点 答案就是求流量为2的最小费用最大流 (这题目要主要 这是无向边 我也不知道为什么  反正没建就错了)
套个板子就可以了
 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string.h>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const double PI=acos(-1.0);
const double eps=0.0000000001;
const int INF=0x3f3f3f3f;
const int N=+;
int head[N];
int dis[N];
int pre[N];
int vis[N];
int tot;
int m,n;
struct node{
int from,to,next,flow,cost;
}edge[N<<];
void init(){
memset(head,-,sizeof(head));
tot=;
}
void add(int u,int v,int c,int cost){
edge[tot].from=u;
edge[tot].to=v;
edge[tot].flow=c;
edge[tot].cost=cost;
edge[tot].next=head[u];
head[u]=tot++;
edge[tot].from=v;
edge[tot].to=u;
edge[tot].flow=;
edge[tot].cost=-cost;
edge[tot].next=head[v];
head[v]=tot++;
}
int spfa(int s,int t){
memset(pre,-,sizeof(pre));
memset(dis,INF,sizeof(dis));
memset(vis,,sizeof(vis));
queue<int>q;
dis[s]=;
vis[s]=;
q.push(s);
while(!q.empty()){
int x=q.front();
q.pop();
vis[x]=;
for(int i=head[x];i!=-;i=edge[i].next){
int v=edge[i].to;
if(edge[i].flow&&dis[v]>dis[x]+edge[i].cost){
dis[v]=edge[i].cost+dis[x];
pre[v]=i;
if(vis[v]==){
vis[v]=;
q.push(v);
} }
}
}
if(pre[t]==-)return ;
return ;
}
int MCMF(int s,int t){
int flow=;
int cost=;
while(spfa(s,t)){
int minn=INF;
//cout<<3<<endl;
for(int i=pre[t];i!=-;i=pre[edge[i].from]){
minn=min(minn,edge[i].flow);
}
for(int i=pre[t];i!=-;i=pre[edge[i].from]){
edge[i].flow=edge[i].flow-minn;
edge[i^].flow=edge[i^].flow+minn;
cost=edge[i].cost+cost;
}
flow=flow+minn;
if(flow==)return cost;
}
return cost;
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
init();
add(,,,);
add(n,n+,,);
for(int i=;i<=m;i++){
int u,v,cost;
scanf("%d%d%d",&u,&v,&cost);
add(u,v,,cost);
add(v,u,,cost);
}
cout<<MCMF(,n+)<<endl;
}
}

poj 2351 Farm Tour (最小费用最大流)的更多相关文章

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

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

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

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

  3. [poj] 1235 Farm Tour || 最小费用最大流

    原题 费用流板子题. 费用流与最大流的区别就是把bfs改为spfa,dfs时把按deep搜索改成按最短路搜索即可 #include<cstdio> #include<queue> ...

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

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

  5. TZOJ 1513 Farm Tour(最小费用最大流)

    描述 When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 &l ...

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

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

  7. POJ 2157 Evacuation Plan [最小费用最大流][消圈算法]

    ---恢复内容开始--- 题意略. 这题在poj直接求最小费用会超时,但是题意也没说要求最优解. 根据线圈定理,如果一个跑完最费用流的残余网络中存在负权环,那么顺着这个负权环跑流量为1那么会得到更小的 ...

  8. POJ 2195 - Going Home - [最小费用最大流][MCMF模板]

    题目链接:http://poj.org/problem?id=2195 Time Limit: 1000MS Memory Limit: 65536K Description On a grid ma ...

  9. POJ 3680: Intervals【最小费用最大流】

    题目大意:你有N个开区间,每个区间有个重量wi,你要选择一些区间,使得满足:每个点被不超过K个区间覆盖的前提下,重量最大 思路:感觉是很好想的费用流,把每个区间首尾相连,费用为该区间的重量的相反数(由 ...

随机推荐

  1. CSS——img

    img标签初始化:在低版本的ie浏览器会自带边框,所以建议border:0px.

  2. Python三方库xlrd,xlwd-Excel读写

    恩,我是翻译汪,主要内容来自http://www.python-excel.org/ 在xlrd,xlwt这两个库中,Excel的结构表示为workbook整个Excel对象,sheet工作表,row ...

  3. (原创)如何对APP服务端进行压力测试

    版权声明:本文为原创文章,转载请先联系并标明出处 APP性能测试分为客户端性能测试和服务端性能测试,客户端的性能测试主要是针对启动快慢.耗电量.耗流量.内存使用等指标进行评估,目前主流的APP客户端性 ...

  4. Redis 之消息发布与订阅(publish、subscribe)

    使用办法: 订阅端: Subscribe 频道名称 发布端: publish 频道名称 发布内容 一般做群聊,聊天室,发布公告信息等.

  5. Windows系统设置与北京Internet时间同步

    找到提供Ntp服务器的网址:http://support.ntp.org/我从中找到东北大学的: ntp.neu.edu.cn  ( 202.118.1.46 ) 长话短说,下面开始设置: 修改后的值 ...

  6. iptables详解(5):iptables匹配条件总结之二(常用扩展模块)

    所属分类:IPtables  Linux基础 在本博客中,从理论到实践,系统的介绍了iptables,如果你想要从头开始了解iptables,可以查看iptables文章列表,直达链接如下 iptab ...

  7. ReportNG 替换testng获得测试报告

    1.导入reportng相关jar包

  8. oracle数据库审计

    Oracle使用大量不同的审计方法来监控使用何种权限,以及访问哪些对象.审计不会防止使用这些权限,但可以提供有用的信息,用于揭示权限的滥用和误用. 下表中总结了Oracle数据库中不同类型的审计. 审 ...

  9. Fedora15下安装Android开发环境

    Fedora15下安装Android开发环境需要以下步骤: 完整步骤. 1. 安装正确版本的JDK. 2. 安装Eclipse. 3.  安装ADT. 4.  安装Android SDK. 5.  安 ...

  10. 在Centos安装oracle_11gR2进度68%"Error in invoking target mkldflags ntcontab.o nnfgt.o of makefile..”

    http://www.xwood.net/_site_domain_/_root/5870/5874/t_c265367.html