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. axiso实战问题

    this.axios({ method: 'get', url: '/api/projectmgt/project/Project/list', withCredentials : true, hea ...

  2. MQTT研究之mosquitto:【环境搭建】

    环境信息: 1. Linux Centos7.2 环境,CPU 2核,内存8G. 2. mosquitto版本:mosquitto-1.5.4 官网:http://mosquitto.org/down ...

  3. 关于java做题时需要注意的事项

    1.要熟悉eclipse的使用 2.用java提交时只能有一个public class 且类名只能为Main 3.提交时不能提交包名 4.提交时要将引入的包一起提交 5.虽然java提供了很多的函数, ...

  4. 关于分布式uuid的一点设想

    在一次公开课上,听别人讲过全局分布式uuid的设计,听过twitter的snowflake的设计.也听过,如果使用单独的计数器服务,不可能每次都保存当前计数器到文本,自己想到应该可以每隔一些数,例如1 ...

  5. 知识点:Mysql 基本用法之函数

    函数 MySQL中提供了许多内置函数 例如: sql 内置函数: 一.数学函数 ROUND(x,y) 返回参数x的四舍五入的有y位小数的值 RAND() 返回0到1内的随机值,可以通过提供一个参数(种 ...

  6. sharepoint环境安装过程中几点需要注意的地方

    写在前面 上篇文章也说明了,在安装sharepoint环境的时候,确实吃了不少苦头,这里纪录一下安装过程中遇到的几个问题. 安装环境 windows server 2012 r2 standard x ...

  7. @SuppressLint("HandlerLeak"),或Handler使用有警告;

    随手写个Handler,然后飘黄,看着挺难受(黄色警告的大概意思:Handler可能会内存泄漏,推荐你用静态内部类+实例化弱引用): This Handler class should be stat ...

  8. RabbitMQ--windows10环境下的RabbitMQ安装步骤(转)

    https://blog.csdn.net/weixin_39735923/article/details/79288578

  9. 性能测试day05_Jmeter学习

    今天来学习下jmeter这个性能测试工具,虽然说性能测试最主要的是整个性能的思路,但是也少不了工具的帮忙,从以前主流的LR到jmeter的兴起,不过对于性能测试来说,个人感觉jmeter比较适合接口性 ...

  10. 在BootStrap的modal中使用Select2搜索框无法输入

    用modal来show一个对话框 dialog.modal({ backdrop:true, keyboard:true, show:true }); 1 2 3 4 5 然后再modal中初始化se ...