[USACO 03FEB]Farm Tour
Description
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
* Lines 2..M+1: Three space-separated integers that define a path: The starting field, the end field, and the path's length.
Output
Sample Input
4 5
1 2 1
2 3 1
3 4 1
1 3 2
2 4 2
Sample Output
6
题目大意
题解
就是流量为$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的更多相关文章
- POJ2135 Farm Tour
Farm Tour Time Limit: 2MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Description ...
- Farm Tour(最小费用最大流模板)
Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18150 Accepted: 7023 Descri ...
- POJ2135 Farm Tour —— 最小费用最大流
题目链接:http://poj.org/problem?id=2135 Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- poj 2351 Farm Tour (最小费用最大流)
Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17230 Accepted: 6647 Descri ...
- 网络流(最小费用最大流):POJ 2135 Farm Tour
Farm Tour Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: ...
- POJ Farm Tour
Farm Tour 题目: 约翰有N块地,家在1号,而N号是个仓库.农场内有M条道路(双向的),道路i连接这ai号地和bi号地,长度为ci. 约翰希望依照从家里出发,经过若干地后达到仓库.然后再返回家 ...
- [网络流]Farm Tour(费用流
Farm Tour 题目描述 When FJ's friends visit him on the farm, he likes to show them around. His farm compr ...
- POJ 2135 Farm Tour (网络流,最小费用最大流)
POJ 2135 Farm Tour (网络流,最小费用最大流) Description When FJ's friends visit him on the farm, he likes to sh ...
- poj 2135 Farm Tour 【无向图最小费用最大流】
题目:id=2135" target="_blank">poj 2135 Farm Tour 题意:给出一个无向图,问从 1 点到 n 点然后又回到一点总共的最短路 ...
随机推荐
- C语言字符数组作业
一.PTA实验作业 题目1:7-1 字符串转换成十进制整数 1. 本题PTA提交列表 2. 设计思路 3.代码截图 4.本题调试过程碰到问题及PTA提交列表情况说明. 1.一开始我没想到怎么判断正负的 ...
- C语言程序设计第三次作业--选择结构(1)
Deadline: 2017-10-29 22:00 一.学习要点 掌握关系运算符和关系表达式 掌握如何判断两个实数相等 掌握常用数学函数的使用 掌握逻辑运算符和逻辑表达式 理解逻辑运算的短路特性 掌 ...
- SaaS的那些事儿
前两年... 大一大二期间,不知道软件架构.云服务器.数据库为何物,偶尔听过却从未用过.天天学的写的东西都是一些命令行代码,所幸在学完<数据结构>和<算法导论>后能够独立实 ...
- 随机ID添加
var http = require("http"); var fs = require("fs"); var server = http.createServ ...
- Struts2之Struts2的下载与安装
Struts2的下载 登陆struts的官网 下载Full Distribution这个选项的struts2的包. 这是Struts2的完整版,里面包括Struts2的实例应用,空实例应用,核心库,源 ...
- 完美解决ubuntu Desktop 16.04 中文版firefox在非root用户不能正常启动的问题
ubuntu安装好后,默认安装有firefox浏览器,不过,非root的账户登录,双击firefox图标,居然出现如下提示:Your Firefox profile cannot be loaded. ...
- node框架express
见识到原生nodeJs服务器的恶心后,我们来用下简单好用的框架吧~ 服务器无非主要提供接口和静态文件读取,直接上代码: const express = require('express'); cons ...
- 第二章 JavaScript核心语法
第二章 avaScript核心语法 一.变量的声明和赋值 JavaScript是一种弱类型语言,没有明确的数据类型,也就是在声明变量时不需要指定数据类型,变量的类型由赋给变量的值决定. 在Java ...
- jenkins 简单实现php集成上线部署
基于公司git版本控制,搭建jenkins实现php集成部署(没有用gitlab,测试服配置较低,gitlab卡的不要不要的了-) 一.安装jenkins相关依赖 wget -O /etc/yum.r ...
- Spring Security 入门(1-8)Spring Security 的配置文件举例