HDU 2448 Mining Station on the Sea(费用流)

【白话系列】倍增算法 【序言】         我认为吧,所有能够优化复杂度的算法都是神奇的,所有能够化繁琐为形象的文字都是伟大的。一直觉得倍增算法是个很神奇的东西,所以决定写点东西纪念一下它。但是作为一个非常不称职的OIER,我非常讨厌在看别人的算法解析时整版的i,j,k等我看到鼠标就惯性移到右上角的符号语言,所以我想用最形象的方式来纪念它。 【一】         从前,有一只可爱得不得了的小白兔,它想从 阅读详情

题目地址:HDU 2448

求n次最短路,将n艘船到各港口的最短路求出来,然后用最短路当费用,跑一次费用流。

代码如下:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm>

using namespace std;
const int INF=0x3f3f3f3f;
int head[400], cnt, source, sink, a[400], flow, cost;
int d[400], f[400], vis[400], cur[400];
struct node
{
    int u, v, cap, cost, next;
}edge[1000000];
void add(int u, int v, int cap, int cost)
{
    edge[cnt].v=v;
    edge[cnt].cap=cap;
    edge[cnt].cost=cost;
    edge[cnt].next=head[u];
    head[u]=cnt++;

    edge[cnt].v=u;
    edge[cnt].cap=0;
    edge[cnt].cost=-cost;
    edge[cnt].next=head[v];
    head[v]=cnt++;
}
int spfa()
{
    memset(vis,0,sizeof(vis));
    memset(d,INF,sizeof(d));
    f[source]=INF;
    cur[source]=-1;
    d[source]=0;
    queue<int>q;
    q.push(source);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(d[v]>d[u]+edge[i].cost&&edge[i].cap)
            {
                d[v]=d[u]+edge[i].cost;
                f[v]=min(f[u],edge[i].cap);
                cur[v]=i;
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    if(d[sink]==INF) return 0;
    flow+=f[sink];
    cost+=f[sink]*d[sink];
    for(int i=cur[sink];i!=-1;i=cur[edge[i^1].v])
    {
        edge[i].cap-=f[sink];
        edge[i^1].cap+=f[sink];
    }
    return 1;
}
void mcmf()
{
    flow=cost=0;
    while(spfa()) ;
    printf("%d\n",cost);
}
int head1[400], cnt1, dp[400][400];
struct node1
{
    int u, v, w, next;
}bian[400];
void add1(int u, int v, int w)
{
    bian[cnt1].v=v;
    bian[cnt1].w=w;
    bian[cnt1].next=head1[u];
    head1[u]=cnt1++;
}
void spfa(int x)
{
    memset(vis,0,sizeof(vis));
    memset(dp[x],INF,sizeof(dp[x]));
    dp[x][x]=0;
    queue<int>q;
    q.push(x);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=head1[u];i!=-1;i=bian[i].next)
        {
            int v=bian[i].v;
            if(dp[x][v]>dp[x][u]+bian[i].w)
            {
                dp[x][v]=dp[x][u]+bian[i].w;
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
}
int main()
{
    int n, m, k, p, i, j, u, v, w;
    while(scanf("%d%d%d%d",&n,&m,&k,&p)!=EOF)
    {
        memset(head,-1,sizeof(head));
        memset(head1,-1,sizeof(head1));
        cnt1=0;
        cnt=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        while(k--)
        {
            scanf("%d%d%d",&u,&v,&w);
            add1(u,v,w);
            add1(v,u,w);
        }
        while(p--)
        {
            scanf("%d%d%d",&u,&v,&w);
            add1(v,u+m,w);
        }
        for(i=1;i<=n;i++)
        {
            spfa(a[i]);
        }
        source=0;
        sink=2*n+1;
        for(i=1;i<=n;i++)
        {
            add(source,i,1,0);
            add(i+n,sink,1,0);
            for(j=1+m;j<=m+n;j++)
            {
                if(dp[a[i]][j]!=INF)
                {
                    add(i,j-m+n,1,dp[a[i]][j]);
                    //printf("%d %d %d\n",i,j-m+n,dp[a[i]][j]);
                }
            }
        }
        mcmf();
    }
    return 0;
}


LaTeX双栏浮动结构实战:用dblfloatfix解决跨栏图表排版难题 本文深入探讨了LaTeX双栏浮动结构在学术论文排版中的常见问题,特别是IEEE会议论文中的跨栏图表定位难题。通过介绍dblfloatfix包的整合解决方案,帮助用户实现精准的底部定位和浮动顺序保持,提升排版效率与质量。 阅读详情

相关推荐

NI-HIL学习开篇

自我介绍下,四年零部件厂工作经验,一年(至今)主机厂工作经验,负责BMS/VCU方向软硬件测试。 学习目的:把握新能源汽车的测试发展趋势,顺应历史的洪流。 学习目标:1.掌握HIL开发测试流程中的IO配置、开环验证、闭环模型开发及调试、自动化测试 2.掌握第三方模型的搭建及simulink关联仿真(2019.05.21新增:需巩固练习matlab基础——2...

masonson的专栏 4185

【HDU】 2448 Mining Station on the Sea 费用流

Mining Station on the Sea Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2123    Accepted Submission(s): 642 Problem Description The ocean

poursoul 782

保姆级教程:将YOLOv8模型封装成Docker镜像,集成到CVAT实现自动标注

本文提供了一份详细的保姆级教程,指导如何将YOLOv8模型封装成Docker镜像并集成到CVAT平台,实现自动化标注。通过环境配置、模型服务化封装、Docker镜像构建与优化、CVAT平台集成等步骤,帮助开发者高效完成计算机视觉项目的自动标注工作流,显著提升标注效率。

weixin_28692817的博客 418

hdu 2448 Mining Station on the Sea【网络费用流】

Mining Station on the Sea Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2371    Accepted Submission(s): 732 Problem Description The o

letterwuyu的专栏 771

SAP Workflow审批功能实例详细过程

SAP Workflow审批功能实例详细过程

陈送平的博客 6586

从 JDK8 到 JDK17:CRMEB 框架升级

【摘要】CRMEB开源框架已成功从JDK8升级至JDK17,带来显著性能提升:并发吞吐量提高35%,响应延迟降低40%,资源占用减少25%。升级方案解决87个技术卡点,支持模块化扩展和微服务转型,兼容SpringBoot3.0等新技术。提供基础版到企业级的升级服务,包含代码重构指导和性能调优。经金融级验证的方案可帮助电商平台实现日均10万+UV承载,支付成功率提升至99.7%。现以低于市场30%的价格提供全周期技术支持,助力企业抢占技术先机。

weixin_39782540的博客 702

hdu2448 费用流

http://acm.hdu.edu.cn/showproblem.php?pid=2448 和那个在地图上回家的差不多~~~ station之间是无向的,port与station是有向的,因为进去就不能出来。 费用流建图,即可 #include #include #include #include #define inf 0x3f3f3f3f #define MAXN 3000

skyline 963

由浅入深理解Latent Diffusion/Stable Diffusion(5):利用预训练模型快速开始自己的科研任务

从零开始训练一个stable diffusion大概需要16万美元和1个月的时间,这对于一般的科研工作者来说难以承受,所以如何利用预训练模型快速开始自己的科研训练是一个非常有价值的问题。这篇博客将演示如何利用预训练模型快速开始自己的科研任务,希望对大家有所帮助!

沉迷单车的追风少年 3376

hdu 2448 Mining Station on the Sea

最小费用最大流。 多源多汇点,把源点S 到 每个船所在的station建一条容量为1,cost为0的单向边。 把每个port到 汇点T 建一条容量为1,cost为1的但单向边。 注意station之间的边的容量要建为inf。。 而且port 和 station之间是单向边

小媛在努力~ 1260

threejs中使用Reflector实现出倒影效果,并实现背景透明的效果(Reflector镜面背景透明, 倒影弱化)

在项目中遇到要实现倒影效果,threejs给我们提供了Reflector,可以轻松完成镜面效果,实现倒影。但背景不透明;并且反射出的倒影太亮了;网上没搜到解决方案, 自己修改了下源码实现了想要的效果,做个记录...

weixin_40856652的博客 5726

HDU 2448 Mining Station on the Sea 费用流

//author: CHC //First Edit Time: 2014-10-12 14:30 //Last Edit Time: 2014-10-12 15:03 #include #include #include #include #include #include #include #include #include #include #include using

inf_force 785

高通SDX35:SGMII接口PHY RTL8211FSI-CG驱动适配

高通SDX35 RTL8211F SGMII

不秃头的程序猿Gabriel的博客 793

hdu 2448 Mining Station on the Sea(最短路+费用流)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2448  题意:给你一个由N个港口和M个海上油田构成的连通无向图(给出了图中所有的边和权值),现在给你N个船所在的油田编号,问你让这N条船,每条都回到1个港口去(每个港口最多只能容纳一条船),问你这N条船行走的总距离最短是多少? 解题思路:首先可以用Floyd算法,求出油田到港口的最短路径。由于一个

hexianhao的博客 637

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

HDU_2448_Mining Station on the Sea(最短路 + 最小费用流)

题意:有m个油田、n条渔船以及n个港口,n条船分别停在n个油田上,给出油田与油田之间的距离,油田与港口之间的距离,现在你要把船开回到港口去,一个港口只能容纳一条船,问所有船行驶的总路程最少是多少。 分析:最短路 + 最小费用流。 构图: 加入超级源点s(0),从s指向每条船 i 连一条边,容量为1,费用为0; 加入超级汇点t(2 * n + 1),从每个港口 i + n 指向t连边,容量为1,费用为0; 最短路求出每条船到每个港口的最短距离,从该条船向每个港口连边,容量为最短路径长度。 最后跑一

一点也不二 846

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

HDU 2448 Mining Station on the Sea 海上的矿站(费用流)

点击打开链接   Mining Station on the Sea Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1941    Accepted Submission(s): 578 Problem Descr

代码改变世界,keep coding。 1870

Mining Station on the Sea HDU - 2448(费用流 || 最短路 && hc)

Mining Station on the Sea Time Limit: 5000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3565Accepted Submission(s): 1108 Problem Description The o...

babing2770的博客 157

HDU 2448 Mining Station on the Sea(最小费用流+spfa,超了n次的题)

Mining Station on the Sea Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2572    Accepted Submission(s): 775 Problem Description T

青山绿水之辈 专栏 948

hdu 2448 Mining Station on the Sea【费用流】本代码思路仅供参考

Mining Station on the Sea Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3066    Accepted Submission(s): 923 Problem Description T

mengxiang000000 769

hdu 2448 Mining Station on the Sea(KM匹配//费用流)

Mining Station on the Sea Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2051    Accepted Submission(s): 617 Problem Description The o

LSS 617

[HDU 2448] Mining Station on the Sea

题目描述: 有N个海港,N个船只,M个矿藏点,有K条海路连接矿藏点,每条路都有一个权值,并且海路双向,有P条路连接矿藏点和海港,只能从矿藏点驶向海港,每条路有一个权值,N条船初始在N个矿藏点上,现在要求你求出N个船进入N个海港的最小代价,保证有解。 题目分析: 不就是个费用流吗. 首先,我们从源点向每个有船只的矿藏点连一条容量为1,权值为0的边 然后根据矿藏点之间的路径信息,连接容量为INF,权值...

HT008的Blog 239

HDU 2448 Mining Station on the Sea 最短路+KM

题目:http://acm.hdu.edu.cn/showproblem.php?pid=2448 题意:有n个港口n条船,m个采矿站,船只能在能够通信的采矿站之间或者能够通信的港口和采矿站之间航行,给你能够通信的单位之间的距离,现在有n条船全都要从采矿站返回港口,每个港口只能容纳一条船且船进去后不能出来,求n条船航行的距离最小和。 思路:对于每条船求一次最短路,然后建二分图,它和每个港口的最

弱智就要多努力 632

HDU2448 Mining Station on the Sea Floyd算法+KM算法

Problem Address:http://acm.hdu.edu.cn/showproblem.php?pid=2448   【题意】   给定m个station的无向连接图, 再给定m个station上任意的n个vessel(n 再给定n个port,同时给出station到port的有向连接图, 要求n个vessel回到n个port的最小权值和。 一个port只能容纳一个ve

Human_CK的专栏 727

hdu 2448 Mining Station on the Sea km算法+最短路

Mining Station on the Sea Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3081    Accepted Submission(s): 932 Problem Description T

Kblacksmith的博客 604

HDU 2448 Mining Station on the Sea km

#include<cstdio> #include<string> #include<cstring> #include<iostream> #include<map> using namespace std; const int maxn = 105; const int INF = (1<&lt...

amily05200的博客 141
上一篇: HDU 5056 Boring count(窗口滑动法)
下一篇: HDU 1856 More is better(并查集+离散化)
Aerolite坠落
博客等级 码龄13年 203粉丝 640原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值