[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 点然后又回到一点总共的最短路 ...
随机推荐
- Java虚拟机之Java内存区域
Java虚拟机运行时数据区域 ⑴背景:对于c/c++来说程序员来说,需要经常去关心内存运行情况,但对于Java程序员,只需要在必要时关心内存运行情况,这是因为在Java虚拟机自动内存管理机制的帮助下, ...
- 【Alpha版本】冲刺阶段 - Day6 - 乘风
今日进展 袁逸灏:1.实现了碰撞的判定:2.代码规范化:3.解决了项目基本代码.(7h) 刘伟康:补充了上次未完成的任务,即检查代码规范,增加AS规范并整理上传至码云.除此之外,学习了部分 Andro ...
- alpha-咸鱼冲刺day3
一,合照 emmmmm.自然还是没有的. 二,项目燃尽图 三,项目进展 今天把数据库的表给建好了,学长那边把登陆跟注册页面也做好了(纯页面,html5+css的那种) 四,问题困难 日常啥都不会,百度 ...
- socketpair创建双向通信的管道(全双工通信)
Linux下socketpair介绍: socketpair创建了一对无名的套接字描述符(只能在AF_UNIX域中使用),描述符存储于一个二元数组,例如sv[2] .这对套接字可以进行双工通信,每一个 ...
- python 操作PostgreSQL
pip install psycopg Python psycopg2 模块APIs 以下是psycopg2的重要的的模块例程可以满足Python程序与PostgreSQL数据库的工作. S.N. A ...
- 九、Python发送QQ邮件(SMTP)
看了廖雪峰老师的教程: 一封电子邮件的旅程就是 发件人 -> MUA -> MTA -> MTA -> 若干个MTA -> MDA <- MUA <- 收件人 ...
- ELK学习总结(1-2)安装ElasticSearch
1.下载安装 Centos6.4 jdk1.8.20以上 elasticsearch::https://www.elastic.co/downloads/elasticsearch ...
- Python学习之dict和set
#coding=utf-8 # dict dict= {'bob': 40, 'andy': 30} print dict['bob'] # 通过dict提供的get方法,如果key不存在,可以返回N ...
- linux下查看mysql日志文件的方法
查看mysql日志方法: mysql默认不允许我们查看日志.需要更改一些设置 1 vi 更改配置文件 允许用户查看日志文件 sudo vi /etc/mysql/mysql.conf.d/mysqld ...
- 在Android项目中使用Java8
前言 在过去的文章中我介绍过Java8的一些新特性,包括: Java8新特性第1章(Lambda表达式) Java8新特性第2章(接口默认方法) Java8新特性第3章(Stream API) 之前由 ...