题目链接:http://poj.org/problem?id=2135

Farm Tour
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10862   Accepted: 4024

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
1 2 1
2 3 1
3 4 1
1 3 2
2 4 2

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(简单的最小费用流问题)的更多相关文章

  1. [poj2135]Farm Tour(最小费用流)

    解题关键:最小费用流 代码一:bellma-ford $O(FVE)$  bellman-ford求最短路,并在最短路上增广,速度较慢 #include<cstdio> #include& ...

  2. poj2135最小费用流

    裸题,就是存个模板 最小费用流是用spfa求解的,目的是方便求解负环,spfa类似于最大流中的bfs过程 #include<map> #include<set> #includ ...

  3. poj2135 最小费用流

    添加超级源点(与点1之间的边容量为2,权值为0)和超级汇点(与点N之间的边容量为2,权值为0),求流量为2的最小费用流.注意是双向边. #include <iostream> #inclu ...

  4. POJ2135 来回最短路(简单费用流)

    题意:       就是从1走到n然后再走回来,一条边只能走一次,要求路径最短. 思路:       比较水,可以直接一遍费用流,不解释了,具体的看看代码,敲这个题就是为了练 练手,好久不敲了,怕比赛 ...

  5. [转]从入门到精通: 最小费用流的“zkw算法”

    >>>> 原文地址:最小费用流的“zkw算法” <<<< 1. 网络流的一些基本概念 很多同学建立过网络流模型做题目, 也学过了各种算法, 但是对于基本 ...

  6. POJ 2195Going Home(网络流之最小费用流)

    题目地址:id=2195">POJ2195 本人职业生涯费用流第一发!!快邀请赛了.决定还是多学点东西.起码碰到简单的网络流要A掉.以后最大流费用流最小割就一块刷. 曾经费用流在我心目 ...

  7. 详解zkw算法解决最小费用流问题

    网络流的一些基本概念 很多同学建立过网络流模型做题目, 也学过了各种算法, 但是对于基本的概念反而说不清楚. 虽然不同的模型在具体叫法上可能不相同, 但是不同叫法对应的思想是一致的. 下面的讨论力求规 ...

  8. HDOJ:1533-Going Home(最小费用流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533 解题心得: 第一次写最小费用流的题,去hdoj上找了一个入门级题目,建图比较简单,用了spfa和 ...

  9. POJ 2195 Going Home 最小费用流

    POJ2195 裸的最小费用流,当然也可以用KM算法解决,但是比较难写. 注意反向边的距离为正向边的相反数(因此要用SPFA) #include<iostream> #include< ...

随机推荐

  1. 此文章介绍vue-cli脚手架config目录下index.js配置文件

    此配置文件是用来定义开发环境和生产环境中所需要的参数 关于注释 当涉及到较复杂的解释我将通过标识的方式(如(1))将解释写到单独的注释模块,请自行查看 上代码 // see http://vuejs- ...

  2. Python 38 初识数据库

    数据库 1.什么是mysql,什么是数据库? 文件处理就可以将数据永久存储 问题 1.管理不方便 2.文件操作效率问题 3.一个程序不太可能仅运行在同一台电脑上 提高计算机性能的方式 1.垂直扩展  ...

  3. Redis hash结构 和常用命令

    Redis 数据结构 -- 哈希 hash 是 一个 String 类型的field 和 value 的映射表 hash 的键值 对在内存中的一种无序的状态 命令 说明 备注 hdel key fie ...

  4. 基于mybatis向oracle中插入数据的性能对比

    数据库表结构: 逐条插入sql语句: <insert id="insert" parameterType="com.Structure"> INSE ...

  5. hibernate_05_单表操作_对象类型

    本篇使用hibernate输出一个对象(图片) 先写一个java类 package com.imooc.hibernate; import java.sql.Blob; import java.uti ...

  6. dubbo之多版本

    当一个接口实现,出现不兼容升级时,可以用版本号过渡,版本号不同的服务相互间不引用. 可以按照以下的步骤进行版本迁移: 在低压力时间段,先升级一半提供者为新版本 再将所有消费者升级为新版本 然后将剩下的 ...

  7. C# 前一个数是后一个数的父级

    private void button2_Click(object sender, EventArgs e) { var str = "1 2 3 4 5 6 7 8 9 10 11 12 ...

  8. java操作Excel的poi 创建一个sheet页

    package com.java.poi; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.us ...

  9. Django 路由视图FBV/CBV

    路由层  url路由层结构 from django.conf.urls import url from django.contrib import admin from app01 import vi ...

  10. PAT_A1119 Pre- and Post-order Traversals

    Source: PAT A1119 Pre- and Post-order Traversals (30 分) Description: Suppose that all the keys in a ...