Farm Tour

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18150   Accepted: 7023

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

 
 
//题意:n 个点, m 条无向边,要求从 1 走到 n 点,再从 n 走到 1 点,不能走重复路,求最短路径
//可以看成从 1 走到 n 用两种路径,因为只能走一遍,所以边的容量为 1
所以建立一个附加的源点容量为2,费用为 0 到1点,附加的汇点容量为 2 ,费用为 0 到 n+1 点跑最小费用最大流即可
 //# include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
# define eps 1e-
# define INF 0x3f3f3f3f
# define pi acos(-1.0)
# define MXN
# define MXM struct Edge{
int from, to, flow, cost, cap;
}edges[MXM*]; //有向边数*2 struct MCMF{
int n, m, idx; //点,边,边数
int flow, cost;
vector<int> G[MXN]; //记录边
int inq[MXN]; //BFS用
int dis[MXN]; //层次
int pre[MXN]; //上一条弧
int adf[MXN]; //增量 void Init(){
idx=;
for (int i=;i<=n+;i++) G[i].clear(); //有附加点时要注意
} void Addedge(int u,int v,int cost,int cap){
edges[idx++] = (Edge){u,v,,cost,cap};
edges[idx++] = (Edge){v,u,,-cost,};
G[u].push_back(idx-);
G[v].push_back(idx-);
} int Bellman(int s, int t)
{
memset(dis,0x3f,sizeof(dis)); //有附加点时要注意
memset(inq,,sizeof(inq));
dis[s]=, inq[s]=, adf[s]=INF;
queue<int> Q;
Q.push(s);
while (!Q.empty())
{
int u = Q.front(); Q.pop();
inq[u]=;
for (int i=;i<(int)G[u].size();i++)
{
Edge &e = edges[G[u][i]];
if (dis[e.to] > dis[u] + e.cost && e.cap > e.flow)
{
dis[e.to] = dis[u] + e.cost;
adf[e.to] = min(adf[u], e.cap-e.flow);
pre[e.to] = G[u][i];
if (!inq[e.to])
{
Q.push(e.to);
inq[e.to]=;
}
}
}
}
if (dis[t]==INF) return false; flow+=adf[t];
cost+=adf[t]*dis[t];
int x=t;
while(x!=s)
{
edges[pre[x]].flow+=adf[t];
edges[pre[x]^].flow-=adf[t];
x=edges[pre[x]].from;
}
return true;
} int MinCost(int s,int t)
{
flow = , cost = ;
while(Bellman(s, t));
return cost;
}
}F; int main()
{
scanf("%d%d",&F.n,&F.m);
F.Init();
for (int i=;i<=F.m;i++)
{
int u,v,cost;
scanf("%d%d%d",&u,&v,&cost);
F.Addedge(u,v,cost,);
F.Addedge(v,u,cost,);
}
F.Addedge(, , , );
F.Addedge(F.n, F.n+, , );
printf("%d\n",F.MinCost(,F.n+));
return ;
}
 

Farm Tour(最小费用最大流模板)的更多相关文章

  1. TZOJ 1513 Farm Tour(最小费用最大流)

    描述 When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 &l ...

  2. POJ2135 Farm Tour —— 最小费用最大流

    题目链接:http://poj.org/problem?id=2135 Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  3. poj 2351 Farm Tour (最小费用最大流)

    Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17230   Accepted: 6647 Descri ...

  4. poj 2135 Farm Tour 最小费用最大流建图跑最短路

    题目链接 题意:无向图有N(N <= 1000)个节点,M(M <= 10000)条边:从节点1走到节点N再从N走回来,图中不能走同一条边,且图中可能出现重边,问最短距离之和为多少? 思路 ...

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

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

  6. [poj] 1235 Farm Tour || 最小费用最大流

    原题 费用流板子题. 费用流与最大流的区别就是把bfs改为spfa,dfs时把按deep搜索改成按最短路搜索即可 #include<cstdio> #include<queue> ...

  7. 图论算法-最小费用最大流模板【EK;Dinic】

    图论算法-最小费用最大流模板[EK;Dinic] EK模板 const int inf=1000000000; int n,m,s,t; struct node{int v,w,c;}; vector ...

  8. HDU3376 最小费用最大流 模板2

    Matrix Again Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)To ...

  9. 洛谷P3381 最小费用最大流模板

    https://www.luogu.org/problem/P3381 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用 ...

随机推荐

  1. crm查询和删除审核历史记录

    using System;     using System.Linq;     using Microsoft.Xrm.Sdk;     using Microsoft.Xrm.Sdk.Query; ...

  2. ssl生成证书

    凝雨 - Yun 快乐编程每一天 - Happy Coding Every Days HOME ARCHIVES CATEGORIES TAGS ABOUT Openssl生成自签名证书,简单步骤 P ...

  3. Linux学习笔记 (七)挂载命令

    在Linux中,光盘,U盘,硬盘在使用之前必须进行挂载,挂载类似windows中的分配盘符. 一.查看挂载和自动挂载 1.mount:直接输入mount表示查看系统中所有的挂载点. 2.mount - ...

  4. win8.1休眠状态下不能进入系统

    win8.1下进入睡眠状态出现的问题: 1.合上盖子或者是点击睡眠状态后唤醒进入锁屏界面.可是仅仅能鼠标移动,键盘全然输入不了,出现假死现象,仅仅能强制重新启动. 2.合上盖子再打开无法唤醒屏幕,必须 ...

  5. C语言循环中降低推断——————【Badboy】

    为了让编译器更好地优化循环,应该尽量让循环中降低推断,方法之中的一个是将推断语句整合进表达式.还是这个样例: for (int i = 0; i < 1000*10; i++) { sum += ...

  6. JSON——JavaScript 中的使用

    由于JSON非常简单,很快就风靡Web世界,并且成为ECMA标准.几乎所有编程语言都有解析JSON的库,而在JavaScript中,我们可以直接使用JSON,因为JavaScript内置了JSON的解 ...

  7. NIO之DatagramChannel

    Java NIO中的DatagramChannel是一个能收发UDP包的通道.操作步骤: 1)打开 DatagramChannel 2)接收/发送数据 同样它也支持NIO的非阻塞模式操作,例如: pu ...

  8. VS2013-解决VS2013 4996错误

    由于微软在VS2013中不建议再使用C的传统库函数scanf,strcpy,sprintf等,所以直接使用这些库函数会提示C4996错误,在源文件中添加以下指令就可以避免这个错误提示. )

  9. stylelint — css书写规范

    sass lint guidance 一.安装:npm intsall -g stylelint 二.配置:http://stylelint.io/user-guide/rules/ (以下规则文件配 ...

  10. HDU高精度总结(java大数类)

      HDU1002   A + B Problem II [题意]大数相加 [链接]http://acm.hdu.edu.cn/showproblem.php?pid=1002 Sample Inpu ...