poj2135(简单的最小费用流问题)
题目链接:http://poj.org/problem?id=2135
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. 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
Sample Output
Source |
[Submit] [Go Back] [Status]
[Discuss]
将问题转换为从1号节点到N号节点的两条没有公共边的路径就可以。
这样就转换成了。求流量为2的最小费用流。
code:
- #include<iostream>
- #include<algorithm>
- #include<cstdio>
- #include<cmath>
- #include<cstring>
- #include<vector>
- #include<queue>
- using namespace std;
- const int MAXV=10010;
- const int MAXM=10010;
- const int INF=1<<30;
- typedef pair<int,int> P;
- struct edge{int to,cap,cost,rev;};
- int V;
- vector<edge> G[MAXV];
- int h[MAXV];
- int dist[MAXV];
- int prevv[MAXV],preve[MAXV];
- void add_edge(int from,int to,int cap,int cost)
- {
- G[from].push_back((edge){to,cap,cost,G[to].size()});
- G[to].push_back((edge){from,0,-cost,G[from].size()-1});
- }
- int min_cost_flow(int s,int t,int f)
- {
- int res=0;
- fill(h,h+V,0);
- while(f>0)
- {
- priority_queue<P,vector<P>,greater<P> >que;
- fill(dist,dist+V,INF);
- dist[s]=0;
- que.push(P(0,s));
- while(!que.empty())
- {
- P p=que.top();
- que.pop();
- int v=p.second;
- if(dist[v]<p.first)
- {
- continue;
- }
- for(int i=0;i<G[v].size();i++)
- {
- edge &e=G[v][i];
- if(e.cap>0&&dist[e.to]>dist[v]+e.cost+h[v]-h[e.to])
- {
- dist[e.to]=dist[v]+e.cost+h[v]-h[e.to];
- prevv[e.to]=v;
- preve[e.to]=i;
- que.push(P(dist[e.to],e.to));
- }
- }
- }
- if(dist[t]==INF)
- {
- return -1;
- }
- for(int v=0;v<V;v++) h[v]+=dist[v];
- int d=f;
- for(int v=t;v!=s;v=prevv[v])
- {
- d=min(d,G[prevv[v]][preve[v]].cap);
- }
- f-=d;
- res+=d*h[t];
- for(int v=t;v!=s;v=prevv[v])
- {
- edge &e=G[prevv[v]][preve[v]];
- e.cap-=d;
- G[v][e.rev].cap+=d;
- }
- }
- return res;
- }
- int N,M;
- int a[MAXM],b[MAXM],c[MAXM];
- void solve()
- {
- int s=0,t=N-1;
- V=N;
- for(int i=0;i<M;i++)
- {
- add_edge(a[i]-1,b[i]-1,1,c[i]);
- add_edge(b[i]-1,a[i]-1,1,c[i]);
- }
- cout<<min_cost_flow(s,t,2)<<endl;
- }
- int main()
- {
- while(cin>>N>>M)
- {
- for(int i=0;i<M;i++)
- {
- scanf("%d%d%d",&a[i],&b[i],&c[i]);
- }
- solve();
- }
- return 0;
- }
poj2135(简单的最小费用流问题)的更多相关文章
- [poj2135]Farm Tour(最小费用流)
解题关键:最小费用流 代码一:bellma-ford $O(FVE)$ bellman-ford求最短路,并在最短路上增广,速度较慢 #include<cstdio> #include& ...
- poj2135最小费用流
裸题,就是存个模板 最小费用流是用spfa求解的,目的是方便求解负环,spfa类似于最大流中的bfs过程 #include<map> #include<set> #includ ...
- poj2135 最小费用流
添加超级源点(与点1之间的边容量为2,权值为0)和超级汇点(与点N之间的边容量为2,权值为0),求流量为2的最小费用流.注意是双向边. #include <iostream> #inclu ...
- POJ2135 来回最短路(简单费用流)
题意: 就是从1走到n然后再走回来,一条边只能走一次,要求路径最短. 思路: 比较水,可以直接一遍费用流,不解释了,具体的看看代码,敲这个题就是为了练 练手,好久不敲了,怕比赛 ...
- [转]从入门到精通: 最小费用流的“zkw算法”
>>>> 原文地址:最小费用流的“zkw算法” <<<< 1. 网络流的一些基本概念 很多同学建立过网络流模型做题目, 也学过了各种算法, 但是对于基本 ...
- POJ 2195Going Home(网络流之最小费用流)
题目地址:id=2195">POJ2195 本人职业生涯费用流第一发!!快邀请赛了.决定还是多学点东西.起码碰到简单的网络流要A掉.以后最大流费用流最小割就一块刷. 曾经费用流在我心目 ...
- 详解zkw算法解决最小费用流问题
网络流的一些基本概念 很多同学建立过网络流模型做题目, 也学过了各种算法, 但是对于基本的概念反而说不清楚. 虽然不同的模型在具体叫法上可能不相同, 但是不同叫法对应的思想是一致的. 下面的讨论力求规 ...
- HDOJ:1533-Going Home(最小费用流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533 解题心得: 第一次写最小费用流的题,去hdoj上找了一个入门级题目,建图比较简单,用了spfa和 ...
- POJ 2195 Going Home 最小费用流
POJ2195 裸的最小费用流,当然也可以用KM算法解决,但是比较难写. 注意反向边的距离为正向边的相反数(因此要用SPFA) #include<iostream> #include< ...
随机推荐
- Spark深入之RDD
目录 Part III. Low-Level APIs Resilient Distributed Datasets (RDDs) 1.介绍 2.RDD代码 3.KV RDD 4.RDD Join A ...
- 【DP悬线法】奶牛浴场
虽然还是悬线法,但是这道题可不能轻易地套模板了,而是要换一种思路,横着扫一遍,竖着扫一遍,时间复杂度依旧是O(n^2),然而空间复杂度有一定的优化 如果用原来的方法,显然时间空间都会炸(如果你想用ma ...
- wap网测一道题目
1. 给定一个字符串s, 1 <= len(s) <= 3000, 定义odd palindrome string为长度为奇数的回文串, 求s中该奇回文串的个数. 比如axbcba , 结 ...
- 2.sql server的管理
sql server的管理:需要安装sql server 2005或者sql server 2008,若要使用sqlserver管理工具进行开发还要安装sql server management st ...
- Redis hash结构 和常用命令
Redis 数据结构 -- 哈希 hash 是 一个 String 类型的field 和 value 的映射表 hash 的键值 对在内存中的一种无序的状态 命令 说明 备注 hdel key fie ...
- less 安装和webstorm的使用
1.less 的安装 npm install -g less 2.less安装成功 3.less安装成功后,在webstorm中进行配置.file——>settings:弹出settings框, ...
- SLAM:ORB-SLAM 位姿优化描述
只知道算法描述和代码,而不知道原理是比较扯的事情,还是把原理转载一下. 原文链接: http://www.cnblogs.com/luyb/p/5447497.html ORB-SLAM作为单目SLA ...
- 【sqli-labs】 less23 Error based - strip comments (GET型基于错误的去除注释的注入)
. 加单引号报错 加# http://localhost/sqli-labs-master/Less-23/?id=1'%23 错误没有改变,推测过滤了# 查看源码发现# -- 都被替换掉了 那么可用 ...
- Arduino控制继电器模块
一.实物图 二.例子代码 每隔5s切换断开 接通状态
- C# 写入二进制文件
写入整型25 文件在MiniHex中显示 写入字符串I am happy 0A 6D - 6D - 这一行数据是C#把字符串转换为16进制形式 不知道为啥用MiniHex打开多了个0A 写入空&quo ...