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<queue>
#include<cstdio>
#include<cstring>
using namespace std;
int total;
const int MAXN = ;
const int INF = ;
struct Edge
{
int u, v, cap, cost;
int next;
} edge[];
int edgenum;
int head[MAXN], dist[MAXN], pre[MAXN];
bool vis[MAXN];
void init()
{
edgenum = ;
memset(head, -, sizeof(head));
}
void addedge(int u, int v, int cap, int cost)
{
edge[edgenum].u = u;
edge[edgenum].v = v;
edge[edgenum].cap = cap;
edge[edgenum].cost = cost;
edge[edgenum].next = head[u];
head[u] = edgenum++;
edge[edgenum].u = v;
edge[edgenum].v = u;
edge[edgenum].cap = ;
edge[edgenum].cost = -cost;
edge[edgenum].next = head[v];
head[v] = edgenum++;
}
bool spfa(int s, int t, int n)//找到一条增广路
{
int i, u, v;
queue <int> qu;
memset(vis, false, sizeof(vis));
memset(pre, -, sizeof(pre));
for(i = ; i <= n; i++) dist[i] = INF;
vis[s] = true;
dist[s] = ;
qu.push(s);
while(!qu.empty())
{
u = qu.front();
qu.pop();
vis[u] = false;
for(i = head[u]; i != -; i = edge[i].next)
{
v = edge[i].v;
if(edge[i].cap && dist[v] > dist[u] + edge[i].cost)
{
dist[v] = dist[u] + edge[i].cost;
pre[v] = i;
if(!vis[v])
{
qu.push(v);
vis[v] = true;
}
}
}
}
if(dist[t] == INF) return false;
return true;
}
int min_cost_max_flow(int s, int t, int n)
{
int flow = ; // 总流量
int i, minflow, mincost;
mincost = ;
while(spfa(s, t, n))
{
minflow = INF + ;
for(i = pre[t]; i != -; i = pre[edge[i].u])
if(edge[i].cap < minflow)
minflow = edge[i].cap;
flow += minflow;
for(i = pre[t]; i != -; i = pre[edge[i].u])
{
edge[i].cap -= minflow;
edge[i^].cap += minflow;
}
mincost += dist[t] * minflow;
}
total = flow; // 最大流
return mincost;
}
int main()
{
int n, m;
int u, v, c;
while (scanf("%d%d", &n, &m) != -)
{
init();
int s = ;
int t = n + ;
while (m--)
{
scanf("%d%d%d", &u, &v, &c);
addedge(u, v , , c);
addedge(v, u , , c);
}
addedge(s, , , );
addedge(n, t, , );
int ans = min_cost_max_flow(s, t, n + );
printf("%d\n", ans);
}
return ;
}

[网络流]Farm Tour(费用流的更多相关文章

  1. poj 2135 Farm Tour 费用流

    题目链接 给一个图, N个点, m条边, 每条边有权值, 从1走到n, 然后从n走到1, 一条路不能走两次,求最短路径. 如果(u, v)之间有边, 那么加边(u, v, 1, val), (v, u ...

  2. 【luogu1251】餐巾计划问题--网络流建模,费用流

    题目描述 一个餐厅在相继的 N 天里,每天需用的餐巾数不尽相同.假设第 iii 天需要 ri​块餐巾( i=1,2,...,N).餐厅可以购买新的餐巾,每块餐巾的费用为 p 分;或者把旧餐巾送到快洗部 ...

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

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

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

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

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

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

  6. poj2135 Farm Tour(费用流)

    Description When FJ's friends visit him on the farm, he likes to show them around. His farm comprise ...

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

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

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

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

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

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

随机推荐

  1. 第六届蓝桥杯省赛 java三羊献瑞

    将文字看作一个个变量.根据一开始确定的文字的值进行暴力循环. 三羊献瑞 观察下面的加法算式: 祥 瑞 生 辉 + 三 羊 献 瑞------------------- 三 羊 生 瑞 气 (如果有对齐 ...

  2. 文件-- 字节相互转换(word、图片、pdf...)

    方式一: /// <summary> /// word文件转换二进制数据(用于保存数据库) /// </summary> /// <param name="wo ...

  3. ionic3安卓版release发布

    1.进入到项目根目录 keytool -genkey -v -keystore your-full-keystore-name.keystore -alias your-lias-name -keya ...

  4. 关于Java流

  5. C++Primer第五版——习题答案详解(一)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第1章 开始&&第2章 变量和基本类型 练习1.3 #include&l ...

  6. C++Primer第五版——习题答案详解(十一)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第12章 动态内存 练习12.1 b1包含4个元素,b2被销毁 练习12.2 #incl ...

  7. 第一个Eureka程序,Eureka Client的自启动原理和简要过程

    https://blog.csdn.net/u011531425/article/details/81675289 在之前的Spring Cloud Config的基础上,搭建简单的Eureka Se ...

  8. 学习 MeteoInfo二次开发教程(二)

    1.注意TSB_Select_Click等几个名称要改为toolStripButton2_Click等. 2.以下代码的位置与public Form1()函数并行. ToolStripButton _ ...

  9. Android 最简单的MVP案例;

    随手撸个发出来: V:界面层 //界面层需要实现P.View方法,然后重写P.View中的方法:M层给的数据就在这些个方法的参数中: // 还要获取到P.Provide的实例,使用P.Provide去 ...

  10. Tcp三次挥手和四次挥手

    三次握手:  (1) 第一次握手:建立连接时,客户端A发送SYN包(SYN=j)到服务器B,并进入SYN_SEND状态,等待服务器B确认  (2) 第二次握手:服务器B收到SYN包,必须确认客户A的S ...