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

题目大意

约翰家有 N 间牛棚,M 条双向道路连接了这些牛棚,第 i 条道路连接了第 A i 间牛棚和第 B i 间牛棚,长度为 L i 。所有牛棚中最好的是第一间和最后一间,所以当有朋友来访时,他会带着朋友从第一间牛棚走到第 N 间牛棚,然后再回到第一间牛棚。约翰想让朋友多看看乡村不同的景色,所以希望来回的路上不重复经过任何一条道路,不过重复经过一间牛棚是允许的。请帮助约翰选择一条路线,使得往返路径的总长度最短。输入数据保证路线总是存在的。

题解

就是流量为$2$的最小费用流。

每条边流量为$1$,建立原点连向1号节点,流量为$2$。

 #include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
const int N=;
const int M=; int n,m,u,v,c;
struct tt
{
int to,next,cost,cap;
}edge[M*+];
int path[N+],top=-;
int pre[N+]; void Add(int u,int v,int cost,int cap)
{
edge[++top].to=v;
edge[top].next=path[u];
edge[top].cost=cost;
edge[top].cap=cap;
path[u]=top;
} int SPFA()
{
int dist[N+];
memset(dist,/,sizeof(dist));dist[]=;
int INF=dist[];
bool vis[N+]={};vis[]=;
queue<int>Q;
while (!Q.empty()) Q.pop();
Q.push();
while (!Q.empty())
{
  int u=Q.front();Q.pop();vis[u]=;
  for (int i=path[u];i!=-;i=edge[i].next)
   {
   int v=edge[i].to;
   if (dist[v]>dist[u]+edge[i].cost&&edge[i].cap>)
   {
     dist[v]=dist[u]+edge[i].cost;
     pre[v]=i;
    if (!vis[v])
     {
     vis[v]=;
     Q.push(v);
     }
   }
   }
}
return dist[n]==INF ? :dist[n];
} void change(int r)
{
if (!r) return;
edge[pre[r]].cap--;
edge[pre[r]^].cap++;
change(edge[pre[r]^].to);
} int min_cost_flow()
{
int tolcost=;
int tmp;
while (tmp=SPFA()) tolcost+=tmp,change(n);
return tolcost;
} int main()
{
memset(path,-,sizeof(path));
scanf("%d%d",&n,&m);
Add(,,,);
Add(,,,);
for (int i=;i<=m;i++)
{
   scanf("%d%d%d",&u,&v,&c);
   Add(u,v,c,);
   Add(v,u,-c,);
   Add(v,u,c,);
   Add(u,v,-c,);
}
printf("%d\n",min_cost_flow());
return ;
}

[USACO 03FEB]Farm Tour的更多相关文章

  1. POJ2135 Farm Tour

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

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

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

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

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

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

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

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

    Farm Tour Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: ...

  6. POJ Farm Tour

    Farm Tour 题目: 约翰有N块地,家在1号,而N号是个仓库.农场内有M条道路(双向的),道路i连接这ai号地和bi号地,长度为ci. 约翰希望依照从家里出发,经过若干地后达到仓库.然后再返回家 ...

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

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

  8. POJ 2135 Farm Tour (网络流,最小费用最大流)

    POJ 2135 Farm Tour (网络流,最小费用最大流) Description When FJ's friends visit him on the farm, he likes to sh ...

  9. poj 2135 Farm Tour 【无向图最小费用最大流】

    题目:id=2135" target="_blank">poj 2135 Farm Tour 题意:给出一个无向图,问从 1 点到 n 点然后又回到一点总共的最短路 ...

随机推荐

  1. 『BUG』Android Studio 64位 始终提示 JVM 启动不了,JDK配置失败,error code -1

    前几天 安装了 Android Studio 2.2. 本来一切都好,但是当我修改了 配置文件 studio64.exe.vmoptions 想修改 最大内存(保证运行流畅)后,AS 就再也运行不了了 ...

  2. Hibernate——配置并访问数据库

    Hibernate,对于java来说很重要的一个东西,用于持久层.之前看了很多配置的,都不行,自己来写一个配置成功的. 环境:jdk1.8,eclipse-jee-oxygen,mysql-conne ...

  3. 2018上C语言程序设计(高级)作业- 初步计划

    C语言程序设计(高级)36学时,每周4学时,共9周.主要学习指针.结构和文件三部分内容.整个课程作业计划如下: PTA和博客的使用指南 若第一次使用PTA和博客,请务必先把PTA的使用简介和教师如何在 ...

  4. Alpha冲刺博客集

    传送门 冲刺随笔 Alpha冲刺day1 (10.31):第一天博客地址 Alpha冲刺day2 (11.01):第二天博客地址 Alpha冲刺day3 (11.02):第三天博客地址 Alpha冲刺 ...

  5. 学号:201621123032 《Java程序设计》第7周学习总结

    1:本周学习总结 1.1:思维导图:Java图形界面总结 2:书面作业 2.1: GUI中的事件处理 2.1.1: 写出事件处理模型中最重要的几个关键词 事件:如鼠标单击,滑动,输入汉字等. 事件源: ...

  6. MySQL的小Tips

    交集和差集 MySQL中没有这两个运算,但是有并集运算,所以可以利用这个来间接实现. 差集: SELECT ID FROM ( SELECT DISTINCT A.AID AS ID FROM TAB ...

  7. TCP/IP协议复习

  8. JAVAGUI设计步骤

    ①创建容器 首先要创建一个GUI应用程序,需要创建一个用于容纳所有其它GUI组件元素的载体,Java中称为容器.典型的包括窗口(Window).框架(Frame/JFrame).对话框(Dialog/ ...

  9. 验证码进阶(TensorFlow--基于卷积神经网络的验证码识别)

    本人的第一个深度学习实战项目,参考了网络上诸多牛人的代码,在此谢过,因时间久已,不记出处,就不一一列出,罪过罪过. 我的数据集是我用脚本在网页上扒的,标签是用之前写的验证码识别方法打的.大概用了400 ...

  10. AngularJS1.X学习笔记11-服务

    如果我没记错的话,spring里边有个service层.什么是服务呢?个人理解就是很多地方要用的,可以跨越控制器甚至是跨越模块的工具.AngularJS也为我们提供了服务这种机制,这让我们可以将一些不 ...