poj 2351 Farm Tour (最小费用最大流)

时间:2021-10-17 23:36:22
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 &lbrack;最小费用最大流&rsqb;

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

  3. &lbrack;poj&rsqb; 1235 Farm Tour &vert;&vert; 最小费用最大流

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

  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&lpar;最小费用最大流&rpar;

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

  6. Farm Tour&lpar;最小费用最大流模板&rpar;

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

  7. POJ 2157 Evacuation Plan &lbrack;最小费用最大流&rsqb;&lbrack;消圈算法&rsqb;

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

  8. POJ 2195 - Going Home - &lbrack;最小费用最大流&rsqb;&lbrack;MCMF模板&rsqb;

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

  9. POJ 3680&colon; Intervals【最小费用最大流】

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

随机推荐

  1. 虚拟机NAT网络配置

    今天虚拟机NAT模式配置网络遇到一个奇葩问题.主机能ping同虚拟机时,虚拟机不能ping同主机.相反虚拟机ping通主机时,主机ping不通虚拟机. 最后花了一个小时,终于可以互通了,做一个记录: ...

  2. &lbrack;问题2014S06&rsqb; 复旦高等代数II(13级)每周一题(第六教学周)

    [问题2014S06]  试用有理标准型理论证明13级高等代数I期末考试最后一题: 设 \(V\) 为数域 \(K\) 上的 \(n\) 维线性空间,  \(\varphi\) 为 \(V\) 上的线 ...

  3. Android 手机卫士12--进程管理

    1.本进程不能被选中,所以先将checkbox隐藏掉--手机卫士 不能自杀 if(getItem(position).packageName.equals(getPackageName())){ ho ...

  4. react初识

    如下是在研究中记录的笔记: 1,作用:局部的更新dom结构;虚拟dom保证性能2,和mvc不同,mvc是对于技术上的分离(分类),而react是组件上的分离,每个视图模块分离,复用,以视图模块为单位3 ...

  5. 【转】到底EJB是什么

    [转]到底EJB是什么 到底EJB是什么?被口口相传的神神秘秘的,百度一番,总觉得没有讲清楚的,仍觉得一头雾水.百度了很久,也从网络的文章的只言片语中,渐渐有了头绪. 用通俗话说,EJB就是:&quo ...

  6. javascript mvc 简单例子

    <!DOCTYPE html> <html> <head> </head> <body> <input type="text ...

  7. &amp&semi;lt&semi;源代码&amp&semi;gt&semi;FTPclient追加方式上传自己定义信息

    实现功能:向FTPserver以追加方式上传自己定义信息(例程中为:2014-10-08 13:47:15 test.) 源代码下载(免积分):http://download.csdn.net/det ...

  8. leetcode Merge Two Sorted Lists python

    # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...

  9. React之组件通信

    组件通信无外乎,下面这三种父子组件,子父组件,平行组件(也叫兄弟组件)间的数据传输.下面我们来分别说一下: 父子组件: var Demo=React.createClass({ getInitialS ...

  10. Spring Boot【快速入门】

    Spring Boot 概述 Build Anything with Spring Boot:Spring Boot is the starting point for building all Sp ...