Farm Tour

题目描述

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.

输入

* 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.

输出

A single line
containing the length of the shortest tour.

样例输入

4 5
1 2 1
2 3 1
3 4 1
1 3 2
2 4 2

样例输出

6

最小费用最大流模板

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
struct data{
int to,cap,cost,next;
}g[80001];
int h[1101],d[1101],k=1,used[1101],que[100000],head,tail,last[1101],ans=0;
int INF;
void add(int from,int to,int cap,int cost)
{
g[++k].next=h[from];h[from]=k;g[k].to=to;g[k].cap=cap;g[k].cost=cost;
g[++k].next=h[to];h[to]=k;g[k].to=from;g[k].cap=0;g[k].cost=-cost;
}
bool spfa(int s,int t)
{
memset(last,0,sizeof(last));
memset(d,127/3,sizeof(d));INF=d[0];
memset(used,0,sizeof(used));
head=tail=50000;que[tail++]=s;used[s]=1;d[s]=0;
while(head<=tail)
{
int u=que[head++];
for(int i=h[u];i;i=g[i].next)
{
if(g[i].cap&&d[u]+g[i].cost<d[g[i].to])
{
d[g[i].to]=d[u]+g[i].cost;last[g[i].to]=i;
if(!used[g[i].to])
{
if(d[g[i].to]<d[que[head]])que[--head]=g[i].to;
else que[tail++]=g[i].to;used[g[i].to]=1;
}
}
}
used[u]=0;
}
if(d[t]==INF)return false;
return true;
}
void mcf(int t)
{
int minn=INF;
for(int i=last[t];i;i=last[g[i^1].to])minn=min(minn,g[i].cap);
for(int i=last[t];i;i=last[g[i^1].to])
{
ans+=g[i].cost*minn;
g[i].cap-=minn;g[i^1].cap+=minn;
}
}
int main()
{
int n,m;scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int x,y,z;scanf("%d%d%d",&x,&y,&z);
add(x,y,1,z);add(y,x,1,z);
}
add(n+1,1,2,0);add(n,n+2,2,0);
while(spfa(n+1,n+2))mcf(n+2);
printf("%d",ans);
return 0;
}

时间: 2024-10-14 12:25:57

Farm Tour的相关文章

POJ 2135 Farm Tour &amp;&amp; HDU 2686 Matrix &amp;&amp; HDU 3376 Matrix Again 费用流求来回最短路

累了就要写题解,最近总是被虐到没脾气. 来回最短路问题貌似也可以用DP来搞,不过拿费用流还是很方便的. 可以转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1,然后连一条流量为1(花费根据题意来定) 的边来控制每个点只能通过一次. 额外添加source和sink来控制满流为2. 代码都雷同,以HDU3376为例. #include <algorithm> #include <iostream> #include <cstring> #in

POJ 2135.Farm Tour 最小费用流

Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17307   Accepted: 6687 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

POJ 2135 Farm Tour

Farm Tour Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 213564-bit integer IO format: %lld      Java class name: Main When FJ's friends visit him on the farm, he likes to show them around. His farm comprise

POJ 2135 Farm Tour(费用流)

POJ 2135 Farm Tour 题目链接 题意:给定一个无向图,边有权值,求从1到n再从n到1的最短路 思路:费用流,连边容量为1(注意是无向图),然后源点和1连容量2,n和汇点连容量是2 代码: #include <cstdio> #include <cstring> #include <vector> #include <queue> #include <algorithm> using namespace std; const int

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

Farm Tour Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 2135 64-bit integer IO format: %lld      Java class name: Main When FJ's friends visit him on the farm, he likes to show them around. His farm compris

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

题目:poj 2135 Farm Tour 题意:给出一个无向图,问从 1 点到 n 点然后又回到一点总共的最短路. 分析:这个题目不读仔细的话可能会当做最短路来做,最短路求出来的不一定是最优的,他是两条分别最短,但不一定是和最短. 我们可以用费用流来很轻易的解决,建边容量为1,费用为边权,然后源点s连 1 ,费用0 ,容量 2 ,n点连接汇点,容量2,费用0,,就可以了. 注意这个题目是无向图,所以要建双向边. AC代码: #include <iostream> #include <a

POJ2135 Farm Tour 【最小费用最大流】

Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11782   Accepted: 4393 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

POJ Farm Tour

Farm Tour 题目: 约翰有N块地,家在1号,而N号是个仓库.农场内有M条道路(双向的),道路i连接这ai号地和bi号地,长度为ci. 约翰希望依照从家里出发,经过若干地后达到仓库.然后再返回家中.假设要求往返不能经过同一条道路两次,求參观路线总长度最小值. 算法分析: 用最短路求解然后在删除第一次最短路中的边在求解一次最短路.这样是否可行?应该立即就能找到反例证明该方法不能总得到最优结果吧. 于是我们放弃把问题当作去和回的这样的想法,转而将问题当作求从1号顶点到N号顶点的两条没有公共边的

POJ 2135 Farm Tour (dinic算法,网络流)

构图方法: 注意题目中的边为无向边.新建源点s 和 汇点t 每两条道路连一条容量为1,费用为w的边.s到1连一条容量为1,费用为0 的边,n到 t 连一条容量为1,费用为0 的边,求最大流. #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #include <queue> #include

解题报告 之 POJ2135 Farm Tour

解题报告 之 POJ2135 Farm Tour 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.