题目链接

给一个图, N个点, m条边, 每条边有权值, 从1走到n, 然后从n走到1, 一条路不能走两次,求最短路径。

如果(u, v)之间有边, 那么加边(u, v, 1, val), (v, u, 1, val), val是路的长度,代表费用, 1是流量。

 #include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {, }, {, }, {, -}, {, } };
const int maxn = 4e5+;
int num, head[maxn*], s, t, n, m, nn, dis[maxn], flow, cost, cnt, cap[maxn], q[maxn], cur[maxn], vis[maxn];
struct node
{
int to, nextt, c, w;
node(){}
node(int to, int nextt, int c, int w):to(to), nextt(nextt), c(c), w(w) {}
}e[maxn*];
int spfa() {
int st, ed;
st = ed = ;
mem2(dis);
++cnt;
dis[s] = ;
cap[s] = inf;
cur[s] = -;
q[ed++] = s;
while(st<ed) {
int u = q[st++];
vis[u] = cnt-;
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to, c = e[i].c, w = e[i].w;
if(c && dis[v]>dis[u]+w) {
dis[v] = dis[u]+w;
cap[v] = min(c, cap[u]);
cur[v] = i;
if(vis[v] != cnt) {
vis[v] = cnt;
q[ed++] = v;
}
}
}
}
if(dis[t] == inf)
return ;
cost += dis[t]*cap[t];
flow += cap[t];
for(int i = cur[t]; ~i; i = cur[e[i^].to]) {
e[i].c -= cap[t];
e[i^].c += cap[t];
}
return ;
}
int mcmf() {
flow = cost = ;
while(spfa())
;
return cost;
}
void add(int u, int v, int c, int val) {
e[num] = node(v, head[u], c, val); head[u] = num++;
e[num] = node(u, head[v], , -val); head[v] = num++;
}
void init() {
mem1(head);
num = cnt = ;
mem(vis);
}
void input() {
int x, y, w;
while(m--) {
scanf("%d%d%d", &x, &y, &w);
add(x, y, , w);
add(y, x, , w);
}
add(s, , , );
add(n, t, , );
}
int main()
{
while(~scanf("%d%d", &n, &m)) {
init();
s = , t = n+;
input();
int ans = mcmf();
printf("%d\n", ans);
}
return ;
}

poj 2135 Farm Tour 费用流的更多相关文章

  1. POJ 2135 Farm Tour (网络流,最小费用最大流)

    POJ 2135 Farm Tour (网络流,最小费用最大流) Description When FJ's friends visit him on the farm, he likes to sh ...

  2. poj 2135 Farm Tour 【无向图最小费用最大流】

    题目:id=2135" target="_blank">poj 2135 Farm Tour 题意:给出一个无向图,问从 1 点到 n 点然后又回到一点总共的最短路 ...

  3. 网络流(最小费用最大流):POJ 2135 Farm Tour

    Farm Tour Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: ...

  4. POJ 2135 Farm Tour (最小费用最大流模板)

    题目大意: 给你一个n个农场,有m条道路,起点是1号农场,终点是n号农场,现在要求从1走到n,再从n走到1,要求不走重复路径,求最短路径长度. 算法讨论: 最小费用最大流.我们可以这样建模:既然要求不 ...

  5. POJ 2135 Farm Tour (费用流)

    [题目链接] http://poj.org/problem?id=2135 [题目大意] 有一张无向图,求从1到n然后又回来的最短路 同一条路只能走一次 [题解] 题目等价于求从1到n的两条路,使得两 ...

  6. POJ 2135 Farm Tour [最小费用最大流]

    题意: 有n个点和m条边,让你从1出发到n再从n回到1,不要求所有点都要经过,但是每条边只能走一次.边是无向边. 问最短的行走距离多少. 一开始看这题还没搞费用流,后来搞了搞再回来看,想了想建图不是很 ...

  7. POJ 2135 Farm Tour &amp;&amp; HDU 2686 Matrix &amp;&amp; HDU 3376 Matrix Again 费用流求来回最短路

    累了就要写题解,近期总是被虐到没脾气. 来回最短路问题貌似也能够用DP来搞.只是拿费用流还是非常方便的. 能够转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1.然后连 ...

  8. poj 2135 Farm Tour【 最小费用最大流 】

    第一道费用流的题目--- 其实---还是不是很懂,只知道沿着最短路找增广路 建图 源点到1连一条容量为2(因为要来回),费用为0的边 n到汇点连一条容量为2,费用为0的边 另外的就是题目中输入的了 另 ...

  9. POJ 2135 Farm Tour(最小费用最大流)

    Description When FJ's friends visit him on the farm, he likes to show them around. His farm comprise ...

随机推荐

  1. mac下通过xcodebuild使用oclint

    step1 :下载oclint并安装 下载地址: http://oclint.org/downloads.html 选择mac os x或者darwin的包,下载到本地. 文件夹类似以下: oclin ...

  2. 通过案例掌握Spring 管理事务的步骤及配置

    案例描述  通过完成生成订单业务,掌握事务处理.  需要d_order表和d_item表  订单生成时的业务逻辑:向d_order插入1条数据的同时,向t_item中插入若干条数据  这就是一个独立的 ...

  3. Xposed学习

    刚接触,不是太懂,就昨天root荣耀6就花了一天时间,其实root早就ok,只是因为Xposed框架总是提示红色警告,以为不好用,后来试了几次发现,跟手机也有很大关系,有的不能很好的支持框架,有的模块 ...

  4. telnet测试端口号

    telnet Ip 端口号 如:telnet localhost 1433 详见此链接(转) http://www.3lian.com/edu/2012/11-08/43232.html

  5. List用法

    定义一个类: using System;using System.Collections.Generic;using System.Linq;using System.Web; namespace W ...

  6. oracle sql developer 使用技巧

    1.配置自动提示快捷键: 选中completion insight 然后选中新建快捷方式,然后按alt+/,再选中分配即可:

  7. 任务栏流量监测工具 NetSpeedMonitor 在Windows 8下的安装使用

    这个是给不喜欢360等提供的桌面浮动网络监控的园友准备的,NetSpeedMonitor 是一个可以在任务栏监控流量的小工具,集成在任务栏上显示,可以手动设置单位.文字大小等.还支持监控日志,相比其他 ...

  8. MacOS copy图标shell脚本

    不会shell  同学做的... 可以看见在当前文件夹下创建了一个icons文件夹 最后还压缩了文件夹 #!/bin/bash # readPlist [plist] [key] function r ...

  9. sqlite 查询数据 不用回调

    int main( void ){    sqlite3 *db=NULL;    char *zErrMsg = 0;    int rc;    //打开数据库连接    rc = sqlite3 ...

  10. 自定义TWebBrowser浏览器控制遇到的一些问题

    最近工作需要,要将TWebBrowser样式改头换面,包括菜单,滚动条等,都要换掉. 由于滚动条已经屏蔽掉,所以关于页面滚动等,全部都需要自己写代码来模拟了.模拟的过程中发现获得页面的客户区大小Cli ...