[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 点然后又回到一点总共的最短路 ...
随机推荐
- 【IntelliJ IDEA】使用idea解决新建jsp文件而找不到jsp文件模版的新建选项
使用idea解决新建jsp文件而找不到jsp文件模版的新建选项,这样每次创建一个新的jsp文件岂不是很耗时间? 解决办法: 就是要让idea知道你需要在这个目录下创建jsp文件 左上角,file中点击 ...
- C语言第二次作业---分支结构
一.PTA实验作业 题目1:计算分段函数[2] 1.实验代码 double x,y; scanf("%lf",&x); if(x>=0){ y=sqrt(x); } ...
- bisect 二分查找
先说明的是,使用这个模块的函数前先确保操作的列表是已排序的. 先看看 insort 函数: 其插入的结果是不会影响原有的排序. 再看看 bisect 函数: 其目的在于查找该数值将会插入的位置并返 ...
- 第201621123043 《Java程序设计》第13周学习总结
1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 为你的系统增加网络功能(购物车.图书馆管理.斗地主等)-分组完成 系统还在创建中..... 为了让你 ...
- Linux进程管理之task_struct结构体
进程是处于执行期的程序以及它所管理的资源(如打开的文件.挂起的信号.进程状态.地址空间等等)的总称.注意,程序并不是进程,实际上两个或多个进程不仅有可能执行同一程序,而且还有可能共享地址空间等资源. ...
- python简单路由系统
# 输入模块名/函数 url = input('请输入网址:') module,func = url.split('/') m = __import__('lib.'+module,fromlist= ...
- Scrum 冲刺 第七日
Scrum 冲刺 第七日 站立式会议 燃尽图 今日任务安排 项目发布说明 站立式会议 返回目录 燃尽图 返回目录 今日任务安排 返回目录 项目发布说明 本版本的新功能 不只是简单打地鼠,还有一些不能打 ...
- python 操作Memcached
启动Memcached memcached -d -m 10 -u root -l 10.211.55.4 -p 12000 -c 256 -P /tmp/memcached.pid 参数说明: -d ...
- 为SRS流媒体服务器添加HLS加密功能(附源码)
为SRS流媒体服务器添加HLS加密功能(附源码) 之前测试使用过nginx的HLS加密功能,会使用到一个叫做nginx-rtmp-module的插件,但此插件很久不更新了,网上搜索到一个中国制造的叫做 ...
- ELK学习总结(2-3)Mget获取多个文档
mget 获取多个文档 1.curl 命令格式:mget获取多个文档: curl 'localhost:9200/_mget' -d '{ "docs":[ { " ...