POJ 2135 Farm Tour (费用流)
【题目链接】 http://poj.org/problem?id=2135
【题目大意】
有一张无向图,求从1到n然后又回来的最短路
同一条路只能走一次
【题解】
题目等价于求从1到n的两条路,使得两条路的总长最短
那么就等价于求总流量为2的费用流
【代码】
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <utility>
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int> P;
struct edge{int to,cap,cost,rev;};
const int MAX_V=1000;
int V,h[MAX_V],dist[MAX_V],prevv[MAX_V],preve[MAX_V];
vector<edge> G[MAX_V];
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;
}
const int MAX_M=10000;
int N,M;
int a[MAX_M],b[MAX_M],c[MAX_M];
void init(){
for(int i=0;i<M;i++)scanf("%d%d%d",&a[i],&b[i],&c[i]);
}
void solve(){
int s=0,t=N-1;
V=N;
for(int i=0;i<N;i++)G[i].clear();
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]);
}
printf("%d\n",min_cost_flow(s,t,2));
}
int main(){
while(~scanf("%d%d",&N,&M)){
init();
solve();
}return 0;
}
POJ 2135 Farm Tour (费用流)的更多相关文章
- poj 2135 Farm Tour 费用流
题目链接 给一个图, N个点, m条边, 每条边有权值, 从1走到n, 然后从n走到1, 一条路不能走两次,求最短路径. 如果(u, v)之间有边, 那么加边(u, v, 1, val), (v, u ...
- POJ 2135 Farm Tour (网络流,最小费用最大流)
POJ 2135 Farm Tour (网络流,最小费用最大流) Description When FJ's friends visit him on the farm, he likes to sh ...
- poj 2135 Farm Tour 【无向图最小费用最大流】
题目:id=2135" target="_blank">poj 2135 Farm Tour 题意:给出一个无向图,问从 1 点到 n 点然后又回到一点总共的最短路 ...
- 网络流(最小费用最大流):POJ 2135 Farm Tour
Farm Tour Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: ...
- POJ 2135 Farm Tour (最小费用最大流模板)
题目大意: 给你一个n个农场,有m条道路,起点是1号农场,终点是n号农场,现在要求从1走到n,再从n走到1,要求不走重复路径,求最短路径长度. 算法讨论: 最小费用最大流.我们可以这样建模:既然要求不 ...
- POJ 2135 Farm Tour [最小费用最大流]
题意: 有n个点和m条边,让你从1出发到n再从n回到1,不要求所有点都要经过,但是每条边只能走一次.边是无向边. 问最短的行走距离多少. 一开始看这题还没搞费用流,后来搞了搞再回来看,想了想建图不是很 ...
- POJ 2135 Farm Tour && HDU 2686 Matrix && HDU 3376 Matrix Again 费用流求来回最短路
累了就要写题解,近期总是被虐到没脾气. 来回最短路问题貌似也能够用DP来搞.只是拿费用流还是非常方便的. 能够转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1.然后连 ...
- poj 2135 Farm Tour【 最小费用最大流 】
第一道费用流的题目--- 其实---还是不是很懂,只知道沿着最短路找增广路 建图 源点到1连一条容量为2(因为要来回),费用为0的边 n到汇点连一条容量为2,费用为0的边 另外的就是题目中输入的了 另 ...
- POJ 2135 Farm Tour(最小费用最大流)
Description When FJ's friends visit him on the farm, he likes to show them around. His farm comprise ...
随机推荐
- Python全栈(一)编程语言介绍
一.编程语言介绍 程序是计算机能读懂的语言,是人和计算机沟通的方式. 计算机无法理解符号,只能理解0,1的二进制. 计算机内的运行状态就像灯泡的开关一样来表示各庄状态,两个灯泡能表示4种状态,无数的灯 ...
- 牛客网暑期ACM多校训练营(第一场):J-Different Integers(分开区间不同数+树状数组)
链接:J-Different Integers 题意:给出序列a1, a2, ..., an和区间(l1, r1), (l2, r2), ..., (lq, rq),对每个区间求集合{a1, a2, ...
- vue常用片段
事件处理 基本骨架 子组件 axios v-if v-for 路由 设置style 计算属性 动态class 路由跳转 store nextTick 事件处理: 直接写表达式: @click=&q ...
- glance参数
Image service property keys https://docs.openstack.org/python-glanceclient/latest/cli/property-keys. ...
- Struts2+DAO层实现实例03——添加监听器跟踪用户行为
实例说明 根据上两次的成品进行二次加工. 加入Listener,监听用户的登陆注销情况. 所用知识说明 采用SessionBindingListener对Session进行监听. 同时,Action中 ...
- Application_Start事件中用Timer做一个循环任务
protected void Application_Start(object sender, EventArgs e) { System.Timers.Timer timer = new Syste ...
- string 与 byte[] 互转时的注意事项
1.string 转 byte[] //为UTF8编码 byte[] midbytes=isoString.getBytes("UTF8"); //为ISO-8859-1编码,其中 ...
- Oracle设置用户密码永不过期
1.查看用户的profile是那个,一般是default: select username, profile from dba_users; 2.查看指定概要文件(如default)的密码有效期设置: ...
- weex 开发 (已放弃了)
关于weex 开发 本菜已放弃使用了,当初选择使用weex 是为了同时支持h5 和 android / ios 三端:想法很不错,深入之后 突然发现,开发起来并没有很轻松,因为weex 中有些方法, ...
- AtCoder keyence2019 E Connecting Cities
keyence2019_e $N$ 个节点的无向图 $G$,节点 $i,j$ 之间的边权值为 $|i - j| \times D + A_i + A_j$ . 求最小生成树(Minimum Spann ...