HDU 1853 Cyclic Tour 费用流(圈)

本博客探讨了一个关于城市间循环旅游路径的问题,即每个循环至少包含两个城市,且每个城市仅属于一个循环。目标是求解所有循环的总路径长度最小值。通过构建特殊的图模型并应用最大流算法解决此问题。

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

点击打开链接

 

Cyclic Tour

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)
Total Submission(s): 1048    Accepted Submission(s): 530


Problem Description
There are N cities in our country, and M one-way roads connecting them. Now Little Tom wants to make several cyclic tours, which satisfy that, each cycle contain at least two cities, and each city belongs to one cycle exactly. Tom wants the total length of all the tours minimum, but he is too lazy to calculate. Can you help him?
 


 

Input
There are several test cases in the input. You should process to the end of file (EOF).
The first line of each test case contains two integers N (N ≤ 100) and M, indicating the number of cities and the number of roads. The M lines followed, each of them contains three numbers A, B, and C, indicating that there is a road from city A to city B, whose length is C. (1 ≤ A,B ≤ N, A ≠ B, 1 ≤ C ≤ 1000).
 


 

Output
Output one number for each test case, indicating the minimum length of all the tours. If there are no such tours, output -1.
 


 

Sample Input
  
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 6 5 1 2 1 2 3 1 3 4 1 4 5 1 5 6 1
 


 

Sample Output
  
42 -1
Hint
In the first sample, there are two cycles, (1->2->3->1) and (6->5->4->6) whose length is 20 + 22 = 42.
 


 

Author
RoBa@TJU
 


 

Source
 


 

Recommend
lcy
 

 

 

题意是说有N个城市,M条路,从一个城市出发,终点必须返回该城市,要求必须走完所有的城市,让你求其最短路径。将所有城市连接一个源点,容量是1,费用是0.然后再将所有城市连接一个汇点,容量是1,费用是0,然后城市之间可以走的,连接起来,容量是1。如果是环的话那么入度等于出度。那么最大流等于顶点数n的时候,就有解了,否则输出-1.

#include <iostream>
#include <algorithm>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <memory.h>
#include <queue>
#include <vector>
#include <cmath>
using namespace std;
int sumFlow;
const int MAXN = 507;
const int MAXM = 10002;
const int INF = 1000000000;
struct Edge
{
    int u, v, cap, cost;
    int next;
} edge[MAXM<<2];
int NE;
int head[MAXN], dist[MAXN], pp[MAXN];
int m,n;
int g[MAXN][MAXN];
bool vis[MAXN];
void init()
{
    NE = 0;
    memset(head, -1, sizeof(head));
}
void addedge(int u, int v, int cap, int cost)
{
    edge[NE].u = u;
    edge[NE].v = v;
    edge[NE].cap = cap;
    edge[NE].cost = cost;
    edge[NE].next = head[u];
    head[u] = NE++;
    edge[NE].u = v;
    edge[NE].v = u;
    edge[NE].cap = 0;
    edge[NE].cost = -cost;
    edge[NE].next = head[v];
    head[v] = NE++;
}
bool SPFA(int s, int t, int n)
{
    int i, u, v;
    queue <int> qu;
    memset(vis,false,sizeof(vis));
    memset(pp,-1,sizeof(pp));
    for(i = 0; i <= n; i++) dist[i] = INF;
    vis[s] = true;
    dist[s] = 0;
    qu.push(s);
    while(!qu.empty())
    {
        u = qu.front();
        qu.pop();
        vis[u] = false;
        for(i = head[u]; i != -1; 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;
                pp[v] = i;
                if(!vis[v])
                {
                    qu.push(v);
                    vis[v] = true;
                }
            }
        }
    }
    if(dist[t] == INF) return false;
    return true;
}
int MCMF(int s, int t, int n) // minCostMaxFlow
{
    int flow = 0; // 总流量
    int i, minflow, mincost;
    mincost = 0;
    while(SPFA(s, t, n))
    {
        minflow = INF + 1;
        for(i = pp[t]; i != -1; i = pp[edge[i].u])
            if(edge[i].cap < minflow)
                minflow = edge[i].cap;
        flow += minflow;
        for(i = pp[t]; i != -1; i = pp[edge[i].u])
        {
            edge[i].cap -= minflow;
            edge[i^1].cap += minflow;
        }
        mincost += dist[t] * minflow;
    }
    sumFlow = flow; // 最大流
    return mincost;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int u, v, c;
        init();
        int S=0;//S是源点
        int T=2*n+1;//T是汇点
        int a,b,cc;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&a,&b,&cc);
            addedge(a,b+n,1,cc);
        }
        for(int i=1;i<=n;i++)
        {
            addedge(0,i,1,0);
            addedge(n+i,T,1,0);
        }

        int ans = MCMF(S, T, T+1);//T+1是点的个数
        if(sumFlow!=n)printf("-1\n");
        else
        printf("%d\n",ans);
    }
    return 0;
}


 

 

 

内容概要:本文介绍了多种开发者工具及其对开发效率的提升作用。首先,介绍了两款集成开发环境(IDE):IntelliJ IDEA 以其智能代码补全、强大的调试工具和项目管理功能适用于Java开发者;VS Code 则凭借轻量级和多种编程语言的插件支持成为前端开发者的常用工具。其次,提到了基于 GPT-4 的智能代码生成工具 Cursor,它通过对话式编程显著提高了开发效率。接着,阐述了版本控制系统 Git 的重要性,包括记录代码修改、分支管理和协作功能。然后,介绍了 Postman 作为 API 全生命周期管理工具,可创建、测试和文档化 API,缩短前后端联调时间。再者,提到 SonarQube 这款代码质量管理工具,能自动扫描代码并检测潜在的质量问题。还介绍了 Docker 容器化工具,通过定义应用的运行环境和依赖,确保环境一致性。最后,提及了线上诊断工具 Arthas 和性能调优工具 JProfiler,分别用于生产环境排障和性能优化。 适合人群:所有希望提高开发效率的程序员,尤其是有一定开发经验的软件工程师和技术团队。 使用场景及目标:①选择合适的 IDE 提升编码速度和代码质量;②利用 AI 编程助手加快开发进程;③通过 Git 实现高效的版本控制和团队协作;④使用 Postman 管理 API 的全生命周期;⑤借助 SonarQube 提高代码质量;⑥采用 Docker 实现环境一致性;⑦运用 Arthas 和 JProfiler 进行线上诊断和性能调优。 阅读建议:根据个人或团队的需求选择适合的工具,深入理解每种工具的功能特点,并在实际开发中不断实践和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值