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

Farm Tour
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17672   Accepted: 6851

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

题解:

把问题转化为:最小费用最大流。

1.每一条边其容量为1, 其费用为距离。

2.可知题目要求两条不同的路径,那么对于这个网络流图,就是要求:在流量为2的状态下,1到n的最小费用。

3.那么怎么转化为最小费用最大流呢?设一个超级源点和一个超级汇点。且超级源点到1的容量为2,费用为0, n到超级汇点的容量为2,费用为0;且题目说明了必定有解,那么在这个条件下求超级源点到超级汇点的最小费用最大流,最大流就只能为2了。所以最小费用即为题目所求。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int MAXN = 1e3+; struct Edge
{
int to, next, cap, flow, cost;
}edge[<<];
int tot, head[MAXN];
int pre[MAXN], dis[MAXN];
bool vis[MAXN];
int N; void init(int n)
{
N = n;
tot = ;
memset(head, -, sizeof(head));
} void add(int u, int v, int cap, int cost)
{
edge[tot].to = v; edge[tot].cap = cap; edge[tot].cost = cost;
edge[tot].flow = ; edge[tot].next = head[u]; head[u] = tot++;
edge[tot].to = u; edge[tot].cap = ; edge[tot].cost = -cost;
edge[tot].flow = ; edge[tot].next = head[v]; head[v] = tot++;
} bool spfa(int s, int t)
{
queue<int>q;
for(int i = ; i<=N; i++)
{
dis[i] = INF;
vis[i] = false;
pre[i] = -;
} dis[s] = ;
vis[s] = true;
q.push(s);
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = false;
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] = true;
q.push(v);
}
}
}
}
if(pre[t]==-) return false;
return true;
} int minCostMaxFlow(int s, int t, int &cost)
{
int flow = ;
cost = ;
while(spfa(s,t))
{
int Min = INF;
for(int i = pre[t]; i!=-; i = pre[edge[i^].to])
{
if(Min>edge[i].cap-edge[i].flow)
Min = edge[i].cap-edge[i].flow;
}
for(int i = pre[t]; i!=-; i = pre[edge[i^].to])
{
edge[i].flow += Min;
edge[i^].flow -= Min;
cost += edge[i].cost*Min;
}
flow += Min;
}
return flow;
} int main()
{
int n, m;
scanf("%d%d",&n,&m);
init(n+);
for(int i = ; i<=m; i++)
{
int u, v, c;
scanf("%d%d%d",&u,&v,&c);
add(u,v,,c); //双向图,容量为1,花费即为距离c
add(v,u,,c);
}
int start = , end = n+;
add(start, , , ); //超级源点到1的边,单向。容量为2, 花费为0
add(n, end, , ); //n到超级汇点的边,单向。容量为2, 花费为0
int ans;
minCostMaxFlow(start, end, ans);
printf("%d\n", ans);
return ;
}

最新文章

  1. PHP之封装一些常用的工具类函数
  2. 自定义NSLog
  3. 关于前端CSS预处理器Sass的小知识!
  4. python【6】-函数式编程
  5. java中Object.equals()简单用法
  6. 一个类似backbone路由的纯净route ( 前端路由 客户端路由 backbone路由 )
  7. AlwaysOn的认识与相关理解
  8. 读&lt;jquery 权威指南&gt;[3]-动画
  9. IT 需要知道的一些专业名词和解释 (长期更新)
  10. RabbitMQ基本概念和使用
  11. c#读取文本文档实践1-File.ReadAllLines()
  12. 批处理:遍历输出指定后缀格式的文件名.bat
  13. 深入浅出Ajax(一)
  14. 今天出现了一个问题,Tomcat 进入localhost:8080正常,进入项目内别的页面都是空白页
  15. TCP协议详解(一)
  16. FFmpeg视频处理
  17. Java链式方法
  18. oracle 之 插入超长字段并包含&amp;字符的处理方法
  19. 对spark算子aggregateByKey的理解
  20. [python] pyCharm 右击出现run unittest 解决办法

热门文章

  1. 设置好uTorrent让你的下载速度飞起来
  2. AnyChart创建实时仪表
  3. 关于EOF,转自新浪微博
  4. SSH: Transferred 0 file(s) 解决
  5. Strom运行监控
  6. npm start 修改启动端口的不同方式
  7. 接口自动化测试之HTTP协议详解
  8. IntelliJ IDEA常用统一设置(Linux/Mac/Windows)
  9. CentOS 5.4 final下Systemtap的安装
  10. 一个炫酷的Actionbar效果