hdu 3488 Tour HDU (费用流)

本文介绍如何使用最小费用流算法解决一个特定的旅行路线设计问题,即在一个由多个城市组成的王国中寻找包含至少一个环路且经过每个城市恰好一次的最短路径。

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



In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 30000) one-way roads connecting them. You are lucky enough to have a chance to have a tour in the kingdom. The route should be designed as: The route should contain one or more loops. (A loop is a route like: A->B->……->P->A.) 
Every city should be just in one route. 
A loop should have at least two cities. In one route, each city should be visited just once. (The only exception is that the first and the last city should be the same and this city is visited twice.) 
The total distance the N roads you have chosen should be minimized. 
Input
An integer T in the first line indicates the number of the test cases. 
In each test case, the first line contains two integers N and M, indicating the number of the cities and the one-way roads. Then M lines followed, each line has three integers U, V and W (0 < W <= 10000), indicating that there is a road from U to V, with the distance of W. 
It is guaranteed that at least one valid arrangement of the tour is existed. 
A blank line is followed after each test case.
Output
For each test case, output a line with exactly one integer, which is the minimum total distance.
Sample Input
1
6 9
1 2 5
2 3 5
3 1 10
3 4 12
4 1 8
4 6 11
5 4 7
5 6 9
6 5 4
Sample Output
42

2、最开始看到需要走一个圈的时候可能稍微有点绕,不知道从何下手比较好,其实并不难,首先我们对应想到如果从一个点出发,再回到这个点,如果对应有流需要流过的话,其实对应我们可以将点i拆成两个点i,i+n,一个用来表示起点,一个用来表示终点。


3、那么问题转化到费用流上来:

①建立源点S,将源点连入各个节点(1-n),费用为0,容量为1.表示一个点只能走一次。

②建立汇点T,将各个拆点连入汇点(n+1-2n),费用为0,容量为1。

③对应m条有向边,将u连入v+n,费用为其权值,容量为INF。

那么对应一个简单的三个点组成的环的建图情况:


很明显,跑完费用流的maxflow=3;

那么如果少了3->1这条有向边,很明显maxlfow=2;


4、那么我们得到结论:建好图之后跑费用流,因为题目中保证了一定有解,那么直接输出最小花费即可。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int N = 1e3+10;
const int inf = 0x3f3f3f3f;
typedef pair<int,int>P;
char str[N];
struct node
{
    int x, y;
} v1[N], v2[N];

struct edge
{
    int to, cap, cost, rev;
};
vector<edge>p[N];
void add(int u,int v,int cap,int w)
{
    edge e;
    e.to=v, e.cap=cap, e.cost=w, e.rev=p[v].size();
    p[u].push_back(e);
    e.to=u, e.cap=0, e.cost=-w, e.rev=p[u].size()-1;
    p[v].push_back(e);
    return ;
}

int d[N], h[N], prev1[N], pree[N];

int min_cost_flow(int s,int t,int f)
{
    int res=0;
    memset(h,0,sizeof(h));
    while(f>0)
    {
        fill(d,d+t+1,inf);
        priority_queue< P, vector<P>, greater<P> >q;
        q.push(P(0,s));
        d[s]=0;
        while(!q.empty())
        {
            P x=q.top();
            q.pop();
            int w=x.first, u=x.second;
            if(d[u]<w) continue;
            for(int i=0; i<p[u].size(); i++)
            {
                int v=p[u][i].to;
                w=p[u][i].cost;
                if(p[u][i].cap>0&&d[v]>d[u]+w+h[u]-h[v])
                {
                    d[v]=d[u]+w+h[u]-h[v];
                    prev1[v]=u, pree[v]=i;
                    q.push(P(d[v],v));
                }
            }
        }
        if(d[t]==inf) return -1;
        for(int i=0; i<=t; i++) h[i]+=d[i];
        int flow=f;
        for(int i=t; i!=s; i=prev1[i])
        {
            flow=min(flow,p[prev1[i]][pree[i]].cap);
        }
        f-=flow, res+=(h[t]*flow);
        for(int i=t; i!=s; i=prev1[i])
        {
            edge &e=p[prev1[i]][pree[i]];
            e.cap-=flow;
            p[e.to][e.rev].cap+=flow;
        }
    }
    return res;
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int n, m;
        scanf("%d %d", &n, &m);
        int s=0, e=2*n+1;
        for(int i=1; i<=n; i++)
            add(s,i,1,0),add(n+i,e,1,0);
        for(int i=0; i<m; i++)
        {
            int x, y, z;
            scanf("%d %d %d", &x, &y, &z);
            add(x,n+y,inf,z);
        }
        int x=min_cost_flow(s, e, n);
        printf("%d\n",x);
        for(int i=0; i<=e; i++) p[i].clear();
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值