Farm Tour

题目描述

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.

输入

* 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.

输出

A single line containing the length of the shortest tour.

样例输入

4 5
1 2 1
2 3 1
3 4 1
1 3 2
2 4 2

样例输出

6

代码:
 #include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
int total;
const int MAXN = ;
const int INF = ;
struct Edge
{
int u, v, cap, cost;
int next;
} edge[];
int edgenum;
int head[MAXN], dist[MAXN], pre[MAXN];
bool vis[MAXN];
void init()
{
edgenum = ;
memset(head, -, sizeof(head));
}
void addedge(int u, int v, int cap, int cost)
{
edge[edgenum].u = u;
edge[edgenum].v = v;
edge[edgenum].cap = cap;
edge[edgenum].cost = cost;
edge[edgenum].next = head[u];
head[u] = edgenum++;
edge[edgenum].u = v;
edge[edgenum].v = u;
edge[edgenum].cap = ;
edge[edgenum].cost = -cost;
edge[edgenum].next = head[v];
head[v] = edgenum++;
}
bool spfa(int s, int t, int n)//找到一条增广路
{
int i, u, v;
queue <int> qu;
memset(vis, false, sizeof(vis));
memset(pre, -, sizeof(pre));
for(i = ; i <= n; i++) dist[i] = INF;
vis[s] = true;
dist[s] = ;
qu.push(s);
while(!qu.empty())
{
u = qu.front();
qu.pop();
vis[u] = false;
for(i = head[u]; i != -; i = edge[i].next)
{
v = edge[i].v;
if(edge[i].cap && dist[v] > dist[u] + edge[i].cost)
{
dist[v] = dist[u] + edge[i].cost;
pre[v] = i;
if(!vis[v])
{
qu.push(v);
vis[v] = true;
}
}
}
}
if(dist[t] == INF) return false;
return true;
}
int min_cost_max_flow(int s, int t, int n)
{
int flow = ; // 总流量
int i, minflow, mincost;
mincost = ;
while(spfa(s, t, n))
{
minflow = INF + ;
for(i = pre[t]; i != -; i = pre[edge[i].u])
if(edge[i].cap < minflow)
minflow = edge[i].cap;
flow += minflow;
for(i = pre[t]; i != -; i = pre[edge[i].u])
{
edge[i].cap -= minflow;
edge[i^].cap += minflow;
}
mincost += dist[t] * minflow;
}
total = flow; // 最大流
return mincost;
}
int main()
{
int n, m;
int u, v, c;
while (scanf("%d%d", &n, &m) != -)
{
init();
int s = ;
int t = n + ;
while (m--)
{
scanf("%d%d%d", &u, &v, &c);
addedge(u, v , , c);
addedge(v, u , , c);
}
addedge(s, , , );
addedge(n, t, , );
int ans = min_cost_max_flow(s, t, n + );
printf("%d\n", ans);
}
return ;
}

最新文章

  1. maven环境配置+eclipse环境配置
  2. 过目不忘JS正则表达式
  3. android与网络的交互
  4. SqlServer_Sql防止注入
  5. Bundle、Intent、SharedPreferences
  6. mac 终端常见指令
  7. 《CSS网站布局实录》学习笔记(五)
  8. tribonacci
  9. 初次接触VC++载入自己定义LIB 即静态链接
  10. URL参数中有 特殊符号或加密数据 的问题解决
  11. 【百度地图API】如何区分地址解析和智能搜索?
  12. glusterfs——volume管理
  13. debian安装mongoDB
  14. LeetCode(65):有效数字
  15. Leetcode: Best Time to Buy and Sell Stock I, II
  16. Firebird SEQUENCE
  17. windows10 蓝牙(Bluetooth&#174;)设备删除失败解决方案
  18. MySQL查看所有用户及拥有权限
  19. QueryHelper插件类(hql)
  20. 我在一个前端项目中用js整理的一些通用方法,其中使用到的思想,主要就是约定了。

热门文章

  1. Delphi2009之TImage
  2. linux SVN命令
  3. C/C++中的volatile简单描述
  4. 2017-2018-2 20165312 实验四《Android程序设计》实验报告
  5. MII、GMII、RMII、SGMII、XGMII 接口区别
  6. python数据格式化之pprint
  7. base64图片
  8. Django文件存储(一)默认存储系统
  9. Django之路由系统 Dj
  10. leetcode101