POJ2135 Farm Tour

本文介绍了一种解决特定最短路径问题的方法——利用费用流算法。问题要求找到从起点到终点并返回的最短路径,且路径不重复经过同一边。通过构造特殊的费用流网络,可以有效地求解该问题。

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

一叶落寞,万物失色。

传送门:http://poj.org/problem?id=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

题意:求区域1~n里从1-》n和n-》1的最短路,而且不能重复;
我的思路是用费用流,对于一条边可以把它的流量定为1费用为边权,再建立一个源点它到1的流量为2,花费为0,建立汇点,到它流量为2,花费为0;做一次费用流就可以啦
代码:
#include<cstdio>
#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、付费专栏及课程。

余额充值