poj2135(模板题)

本文介绍了一种解决特定旅行商问题的方法,即寻找从家到谷仓再回家的最短路径,确保每条路径仅使用一次。通过使用SPFA算法进行最短路径计算,并通过循环迭代更新路径权重,最终找到最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Farm Tour
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14270 Accepted: 5441

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

Output

6

你从1到n点,再回到1的最小花费,

题意:#include<stdio.h>00 #include<iostream> #define min(a,b) ((a)<(b)?(a):(b)) using namespace std; const int nMax = 1050; const int eMax = 40050; const int inf = 99999999; struct{ int v, cap, cost, next, re; }edge[eMax]; int n, m, ans; int k, edgeHead[nMax]; int sta[nMax], pre[nMax], dis[nMax]; bool vis[nMax]; void addEdge(int u, int v, int ca, int co){ edge[k].v = v; edge[k].cap = ca; edge[k].cost = co; edge[k].next = edgeHead[u]; edge[k].re = k + 1; edgeHead[u] = k ++; edge[k].v = u; edge[k].cap = 0; edge[k].cost = -co; edge[k].next = edgeHead[v]; edge[k].re = k - 1; edgeHead[v] = k ++; } bool spfa(){ int i, top = 0; for(i = 0; i <= n; i ++){ dis[i] = inf; vis[i] = false; } dis[0] = 0; sta[++ top] = 0; vis[0] = true; while(top){ int u = sta[top --]; for(i = edgeHead[u]; i != 0; i = edge[i].next){ int v = edge[i].v; if(edge[i].cap && dis[v] > dis[u] + edge[i].cost){ dis[v] = dis[u] + edge[i].cost; pre[v] = i; if(!vis[v]){ vis[v] = true; sta[++ top] = v; } } } vis[u] = false; } if(dis[n] == inf) return false; return true; } void end(){ int u, p, sum = inf; for(u = n; u != 0; u = edge[edge[p].re].v){ p = pre[u]; sum = min(sum, edge[p].cap); } for(u = n; u != 0; u = edge[edge[p].re].v){ p = pre[u]; edge[p].cap -= sum; edge[edge[p].re].cap += sum; ans += sum * edge[p].cost; } } int main(){ k = 1; scanf("%d%d", &n, &m); while(m --){ int u, v, w; scanf("%d%d%d", &u, &v, &w); addEdge(u, v, 1, w); addEdge(v, u, 1, w); } addEdge(0, 1, 2, 0); addEdge(n, n+1, 2, 0); n ++; ans = 0; while(spfa()) end(); cout << ans << endl; return 0; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值