Farm Tour POJ - 2135

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

题意很简单,说思路;

设源点为s,汇点为t;

然后建图的时候,s->1   容量为2,费用为0;

n->t,容量为2,费用为0;

这样就能从s出发,到t,然后再走不重复的路径的再回到s;

由于s->1,和n->t这两条路径费用为0,所以不影响最后结果。

这样直接跑一边最大费用最小流的模板就行了。

详见代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
const int MAXX=;
const int INF=0x3f3f3f3f; struct node
{
int to;
int next;
int cap;
int flow;
int cost;
}edge[MAXX]; int head[MAXX],tol;
int pre[MAXX],dis[MAXX];
bool vis[MAXX];
int N; void init(int n)
{
N=n;
tol=;
memset(head,-,sizeof(head));
} void addedge(int u,int v,int cap,int cost)
{
edge[tol].to=v;
edge[tol].cap=cap;
edge[tol].cost=cost;
edge[tol].flow=;
edge[tol].next=head[u];
head[u]=tol++;
edge[tol].to=u;
edge[tol].cap=;
edge[tol].cost=-cost;
edge[tol].flow=;
edge[tol].next=head[v];
head[v]=tol++;
} bool SPFA(int s,int t)
{
queue<int> q;
for(int i=;i<N;i++)
{
dis[i]=INF;
vis[i]=;
pre[i]=-;
}
dis[s]=;
vis[s]=;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].cap>edge[i].flow&&dis[v]>dis[u]+edge[i].cost)
{
dis[v]=dis[u]+edge[i].cost;
pre[v]=i;
if(!vis[v])
{
vis[v]=;
q.push(v);
}
}
}
}
if(pre[t]==-)return ;
return ;
} int minCostMaxFlow(int s,int t)
{
int cost=;
while(SPFA(s,t))
{
int minn=INF;
for(int i=pre[t];i!=-;i=pre[edge[i^].to])
{
if(minn>edge[i].cap-edge[i].flow)
minn=edge[i].cap-edge[i].flow;
}
for(int i=pre[t];i!=-;i=pre[edge[i^].to])
{
edge[i].flow+=minn;
edge[i^].flow-=minn;
cost+=edge[i].cost*minn;
}
}
return cost;
}
int main()
{ int n,m;
while(~scanf("%d%d",&n,&m))
{
int u,v,g;
init(n+);
for(int i=;i<m;i++)
{ scanf("%d%d%d",&u,&v,&g);
addedge(u,v,,g);
addedge(v,u,,g);
}
addedge(,,,);
addedge(n,n+,,);
printf("%d\n",minCostMaxFlow(,n+));
}
return ;
}

最新文章

  1. 预处理(防止sql注入的一种方式)
  2. 使用NodeList
  3. debug,trace,release项目配置区别
  4. [转]Maintain File Upload Control on Postbacks
  5. git变基、冲突解决
  6. 实例:怎样使用 Netty 下载文件
  7. ggplot2 geom设置—散点图
  8. superslide2插件
  9. Linux操作系统-命令-aptitude install unzip
  10. 创建pandas和sqlalchemy的j交互对象,方便于日常的数据库的增删改查(原创)
  11. Linux之系统优化
  12. nginx官方模块之http_random_index_module
  13. poj1155 依赖背包
  14. ES6基础语法
  15. js邏輯
  16. 【BZOJ3507】通配符匹配(哈希,动态规划)
  17. JavaScript 基础数组循环和迭代的几种方法
  18. 制作OpenStack云平台centos6.5镜像
  19. Codeforces Beta Round #31 (Div. 2, Codeforces format)
  20. vue项目经验:图形验证码接口get请求处理

热门文章

  1. Android studio一些设置项
  2. mac 下安装caffe(一)
  3. ios--plist
  4. c++中读写文件操作
  5. imagebutton 设置了src属性的图片更换
  6. [python基础] python生成wordcloud并保存
  7. JS/CSS 压缩的好处
  8. 40. combo的displayField和valueField属性
  9. Centos搭建图形界面VNC
  10. jeesite自定义主题