博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj2135--Farm Tour(最小费用最大流)
阅读量:6580 次
发布时间:2019-06-24

本文共 3823 字,大约阅读时间需要 12 分钟。

Farm Tour
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14219   Accepted: 5421

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

Input

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

Output

A single line containing the length of the shortest tour. 

Sample Input

4 51 2 12 3 13 4 11 3 22 4 2

Sample Output

6

Source

 
农场主有N块地, 求从房子出发到某地,  再回到房子, 所用最短路程(每条边走一次)。 建源点和汇点 跑最短路。
数组开小为毛判T;
#include 
#include
#include
using namespace std;typedef int Type;const int MAXNODE = 1010;const int MAXEDGE = 10010;const int INF = 0x3f3f3f3f;struct Edge{ int u, v, next; Type cap, flow, cost; Edge() {} Edge(int u, int v, Type cap, Type flow, Type cost, int next): u(u), v(v), cap(cap), flow(flow), cost(cost), next(next) {}};struct MXMF{ int n, m, s, t; Edge edges[MAXEDGE*2]; int head[MAXNODE]; int p[MAXNODE]; Type dis[MAXNODE]; Type a[MAXNODE]; bool vis[MAXNODE]; void init(int n) { this->n=n; memset(head, -1, sizeof(head)); m = 0; } void addEdge(int u, int v, Type cap, Type cost) { edges[m]=Edge(u, v, cap, 0, cost, head[u]); head[u]=m++; edges[m]=Edge(v, u, cap, 0, cost, head[v]); head[v]=m++; } bool BellmanFord(int s, int t, Type &flow, Type &cost) { for(int i=0; i
n+1; memset(vis, 0, sizeof(vis)); dis[s]=0; vis[s]=true; p[s]=0; a[s]=INF; queue
Q; Q.push(s); while(!Q.empty()) { int u=Q.front(); Q.pop(); vis[u]=false; for(int i=head[u]; i!=-1; i=edges[i].next) { Edge &e=edges[i]; if(e.cap>e.flow && dis[e.v]>dis[u]+e.cost) { dis[e.v]=dis[u]+e.cost; p[e.v]=i; a[e.v]=min(a[u], e.cap-e.flow); if(!vis[e.v]) { Q.push(e.v); vis[e.v]=true;} } } } if(dis[t]== INF) return false; flow += a[t]; cost += a[t]*dis[t]; int u=t; while(u != s) { edges[p[u]].flow += a[t]; edges[p[u] ^ 1].cost = -edges[p[u] ^ 1].cost; u = edges[p[u]].u; } return true; } Type minCost(int s, int t) { this->s=s; this->t; Type flow=0, cost=0; while(BellmanFord(s, t, flow, cost)); return cost; }} mcmf; int n, m;void init(){ int source=1, sink=n; //imp; mcmf.init(n+2); int u, v, c; for(int i=0; i< m; i++) { scanf("%d%d%d", &u, &v, &c); mcmf.addEdge(u, v, 1, c); } mcmf.addEdge(0, source, 2, 0); mcmf.addEdge(sink, n+1, 2, 0); printf("%d\n", mcmf.minCost(0, n+1));}int main(){ while(scanf("%d%d", &n, &m) != EOF){ init(); } return 0;}

 

 

转载于:https://www.cnblogs.com/soTired/p/5312455.html

你可能感兴趣的文章
解决执行脚本报syntax error: unexpected end of file或syntax error near unexpected token `fi'错误的问题...
查看>>
[BZOJ3312][USACO]不找零(状压DP)
查看>>
页面之间的传值和大量参数的传递
查看>>
python学习之路-5 基础进阶篇
查看>>
gtp转换mbr
查看>>
django rest framework
查看>>
登录注册界面
查看>>
poj1985 求树的直径
查看>>
Python PyPI中国镜像
查看>>
centos 设置静态IP
查看>>
[Angularjs]系列——学习与实践
查看>>
js -- canvas img 封装
查看>>
转 我们工作的动力是什么 工作最终是为了什么?
查看>>
测试一个网站的最大并发量并发数并发用户
查看>>
适配器模式(数据库方面)支持不同的数据库连接
查看>>
Jenkins(二) 安装、新建Jobs与删除及SVN配置(转)
查看>>
CF456B Fedya and Maths 找规律
查看>>
touch修改mtime和atime
查看>>
nodejs安装及windows环境配置
查看>>
转载:Beginning WF 4.0翻译——第三章(流程图工作流)
查看>>