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 4 5 Sample Output 6 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< ...
随机推荐
- D - Vanya and Fence
Problem description Vanya and his friends are walking along the fence of height h and they do not wa ...
- Python启动浏览器Firefox\Chrome\IE
# -*- coding:utf-8 -*- import os import selenium from selenium import webdriver from selenium.webdri ...
- (转)JavaScript深入之从原型到原型链
构造函数创建对象 我们先使用构造函数创建一个对象: function Person() { } var person = new Person(); person.name = 'Kevin'; co ...
- Android上UDP组播无法接收数据的问题
最近,想做一个跨平台的局域网的文件传输软件,思路是组播设备信息,TCP连接传输文件.于是进行了一次简单的UDP组播测试,发现Android对于UDP组播接收数据的支持即极为有限. 部分代码如下 pac ...
- Android读写文件
1.从resource中的raw文件夹中获取文件并读取数据(资源文件只能读不能写) String res = ""; try{ InputStream in = getResour ...
- Repeater + 分页控件 AspNetPager 研究
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs ...
- mysql安装包下载地址
1.打开mysql官网 :https://dev.mysql.com/ 2.选择 downlad-->windows-->MySQL Installer -->滑动至页面底部,选择第 ...
- ubuntu18.0安装RabbitMQ
RabbitMQ是一个消息队列,用于实现应用程序的异步和解耦.生产者将生产消息传送到队列,消费中从队列中拿取消息并处理.生产者不用关心是谁来消费,消费者不用关系是谁在生产消息,从而达到解耦的目的.本文 ...
- BZOJ 2959: 长跑 LCT_并查集_点双
真tm恶心...... Code: #include<bits/stdc++.h> #define maxn 1000000 using namespace std; void setIO ...
- 前端开发—HTML
HTML介绍 web服务的本质 import socket sk = socket.socket() sk.bind(("127.0.0.1", 8080)) sk.listen( ...