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

MIKE水动力笔记2_水动力基础理论知识 本文所讲内容是水动力基础理论知识,所参考的主要书籍是《水动力学和水质》(第二版,季振刚著,海洋出版社)。 阅读详情

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
The ocean is a treasure house of resources and the development of human society comes to depend more and more on it. In order to develop and utilize marine resources, it is necessary to build mining stations on the sea. However, due to seabed mineral resources, the radio signal in the sea is often so weak that not all the mining stations can carry out direct communication. However communication is indispensable, every two mining stations must be able to communicate with each other (either directly or through other one or more mining stations). To meet the need of transporting the exploited resources up to the land to get put into use, there build n ports correspondently along the coast and every port can communicate with one or more mining stations directly.

Due to the fact that some mining stations can not communicate with each other directly, for the safety of the navigation for ships, ships are only allowed to sail between mining stations which can communicate with each other directly. 

The mining is arduous and people do this job need proper rest (that is, to allow the ship to return to the port). But what a coincidence! This time, n vessels for mining take their turns to take a rest at the same time. They are scattered in different stations and now they have to go back to the port, in addition, a port can only accommodate one vessel. Now all the vessels will start to return, how to choose their navigation routes to make the total sum of their sailing routes minimal. 

Notice that once the ship entered the port, it will not come out!
 

Input
There are several test cases. Every test case begins with four integers in one line, n (1 = <n <= 100), m (n <= m <= 200), k and p. n indicates n vessels and n ports, m indicates m mining stations, k indicates k edges, each edge corresponding to the link between a mining station and another one, p indicates p edges, each edge indicating the link between a port and a mining station. The following line is n integers, each one indicating one station that one vessel belongs to. Then there follows k lines, each line including 3 integers a, b and c, indicating the fact that there exists direct communication between mining stations a and b and the distance between them is c. Finally, there follows another p lines, each line including 3 integers d, e and f, indicating the fact that there exists direct communication between port d and mining station e and the distance between them is f. In addition, mining stations are represented by numbers from 1 to m, and ports 1 to n. Input is terminated by end of file.

 

Output
Each test case outputs the minimal total sum of their sailing routes.
 

Sample Input
3 5 5 6 1 2 4 1 3 3 1 4 4 1 5 5 2 5 3 2 4 3 1 1 5 1 5 3 2 5 3 2 4 6 3 1 4 3 2 2
 

Sample Output
13

题意:n个港口,n条船,m个矿场,船只能在有连边的两个地区之间航行,给了港口之间的边的权值,给了港口到某些矿场之间边的权值,刚开始给了船在哪n个矿场,船进了港口就无法出来。问最少的权值使船全部进港口。

思路:建图过程,港口之间建无向边,矿场到港口之间建有向边(不能出港口),跑每个起点的最短路,建立每个起点和每个能到的港口之间的最短路图(边的权值是负的最短路值),跑一边km即可。

ps:其实每道权值匹配的题都卡费用流我是很不服气的。

/*
 * Author:  ktmzgl
 * Created Time:  2016/10/10 16:29:52
 * File Name: F:\Vim\hdu_2448_km+shortestway.cpp
 */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <time.h>
using namespace std;
const int maxint = -1u>>1;
using namespace std;
const int N=310;
const int INF=0x3f3f3f3f;  
int nx,ny;//两边的点数
int g[N][N];//二分图描述
int w[N][N];
int linker[N],lx[N],ly[N];//y中各点匹配状态,x,y中的点标号
int slack[N];
bool visx[N],visy[N];
int n,m,k,p;
int st[N];
int v[N];
int d[N];
void dij(int s,int n)
{
	memset(v, 0, sizeof(v));//矩阵存储
for(int i = 0; i < n; i++) d[i] = (i==s ? 0 : INF);
for(int i = 0; i < n; i++) {
  int x, m = INF;
  for(int y = 0; y < n; y++) if(!v[y] && d[y]<=m) m = d[x=y];
  v[x] = 1;
  for(int y = 0; y < n; y++) d[y] = min(d[y], d[x] + w[x][y]);
}
}

/*  KM算法
 *   复杂度O(nx*nx*ny)
 *  求最大权匹配
 *   若求最小权匹配,可将权值取相反数,结果取相反数
 *  点的编号从0开始
 */

bool DFS(int x)
{
    visx[x] = true;
    for(int y = 0; y < ny; y++)
    {
        if(visy[y])continue;
        int tmp = lx[x] + ly[y] - g[x][y];
        if(tmp == 0)
        {
            visy[y] = true;
            if(linker[y] == -1 || DFS(linker[y]))
            {
                linker[y] = x;
                return true;
            }
        }
        else if(slack[y] > tmp)
            slack[y] = tmp;
    }
    return false;
}
int KM()
{
    memset(linker,-1,sizeof(linker));
    memset(ly,0,sizeof(ly));
    for(int i = 0;i < nx;i++)
    {
        lx[i] = -INF;
        for(int j = 0;j < ny;j++)
            if(g[i][j] > lx[i])
                lx[i] = g[i][j];
    }
    for(int x = 0;x < nx;x++)
    {
        for(int i = 0;i < ny;i++)
            slack[i] = INF;
        while(true)
        {
            memset(visx,false,sizeof(visx));
            memset(visy,false,sizeof(visy));
            if(DFS(x))break;
            int d = INF;
            for(int i = 0;i < ny;i++)
                if(!visy[i] && d > slack[i])
                    d = slack[i];
            for(int i = 0;i < nx;i++)
                if(visx[i])
                    lx[i] -= d;
            for(int i = 0;i < ny;i++)
            {
                if(visy[i])ly[i] += d;
                else slack[i] -= d;
            }
        }
    }
    int res = 0;
    for(int i = 0;i < ny;i++)
        if(linker[i] != -1)
            res += g[linker[i]][i];
    return res;
}

int main()
{
	while(scanf("%d%d%d%d",&n,&m,&k,&p)!=EOF)
	{
		for(int i=0;i<n;i++)
		{
			scanf("%d",&st[i]);
			st[i]--;
		}
		memset(w,INF,sizeof(w));
		memset(g,0,sizeof(g));
		for(int i=0;i<k;i++)
		{
			int x,y,z;
			scanf("%d%d%d",&x,&y,&z);
			w[x-1][y-1]=z;
			w[y-1][x-1]=z;
		}
		for(int i=0;i<p;i++)
		{
			int x,y,z;
			scanf("%d%d%d",&x,&y,&z);
			w[y-1][x-1+m]=z;
		}
		for(int i=0;i<n;i++)
		{
			dij(st[i],n+m);
			/*cout<<"start"<<st[i]<<endl;
			for(int k=0;k<m+n;k++)
		{
			cout<<k<<" "<<d[k]<<endl;
		}*/
			for(int j=m;j<m+n;j++)
			{
				if(d[j]<INF)
					g[i][j-m]=-d[j];
			}
		}
		nx=n,ny=n;
		
		cout<<-KM()<<endl;
	}
    return 0;
}



基于STM32设计的智能厨房环境监测报警系统_426 随着天然气、液化石油气等可燃性气体在家庭中的广泛应用,燃气泄漏所引发的安全问题日益突出。针对传统家用可燃气体报警器功能单一、信息获取受限以及缺乏远程监测能力等不足,设计了一种基于STM32单片机的家用可燃气体报警器系统。 阅读详情

相关推荐

基于STM32和K210的自动驾驶小车设计

自动驾驶技术正在迅速发展,成为了汽车行业和智能交通领域的热点之一。随着科技的发展,STM32和K210这两款芯片为自动驾驶小车提供了优秀的硬件支持。本文我们将学习如何使用STM32和K210打造一个简单的自动驾驶小车。提示:以下是本篇文章正文内容,下面案例可供参考通过这篇文章,掌握了如何使用STM32和K210构建一个简单的自动驾驶小车。通过了解基本的电路设计,并编写了STM32和K210的代码。通过这个例子,可以更好地理解如何在嵌入式系统中使用STM32和K210,以及如何实现自动驾驶小车的简单功能。

Kyle_Chan_zMH的博客 2227

HDU 2448 Mining Station on the Sea(Floyd+最优匹配)

HDU 2448 Mining Station on the Sea(Floyd+最优匹配) http://acm.hdu.edu.cn/showproblem.php?pid=2448 题意:        给你一个由N个港口和M个海上油田构成的连通无向图(给出了图中所有的边和权值),现在给你N个船所在的油田编号,问你让这N条船,每条都回到1个港口去(每个港口最多只能容纳一条船),问你这N

code 1213

机器学习之性能度量

废话不说,本文分为两个部分: 第一部分,我对周志华教授《机器学习》这本书中性能度量知识点中疑难点的学习感悟;第二部分,我感觉该书2.3节 性能度量 中最后部分的代价曲线表述有误,苦于没找到联系教授的正确方式,故希望与广大网友一起探讨。

hustqb的博客 4634

【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

Unity通用Buff系统,Buff编辑器(1,理论篇)(技能系统之Buff篇)

逻辑层根据游戏的规则和设计,控制Buff的生命周期、影响目标对象的属性或行为,以及处理Buff的交互和冲突。Buff堆叠和冲突:有些游戏中允许同类型的Buff叠加,以增加效果的强度,而有些游戏中可能不允许Buff叠加,只能由更强力的Buff来覆盖较弱的Buff。灵活个性化:每个游戏对象拥有自己的BuffManager,可以实现个体对象的独特Buff效果,增加游戏的多样性和个性化。自由控制:每个对象独立管理自己的Buff,可以自由地激活、更新和移除Buff效果,提供更精细的控制和逻辑实现。

u010082265的博客 4699

GD32F407工程模板搭建全记录:从零整理固件库、创建文件夹到编译通过

本文详细记录了GD32F407工程模板的搭建过程,从固件库整理、目录规划到Keil环境配置,帮助开发者快速搭建可维护的嵌入式工程结构。重点介绍了开发环境搭建、工程目录设计、Keil配置及常见编译问题解决方案,适合嵌入式开发者参考。

weixin_29164497的博客 126

HEU Mining Station on the Sea(最小费用最大流)

常规题目; 最小费用最大流,如果是无向图的话,一定用邻接表去做。 代码: //最小费用最大流,注意建图的edge都是双向的 #include #include #include #include using namespace std; const int INF = 0x3fffffff; const int N = 350; int S = 0, E; int n, m, k,

AClion的专栏 1254

绕过卡巴斯基通过RPC控制lsass注入DLL

参考国外的XPN以及3gstudent大佬的文章: https://blog.csdn.net/xiangshen1990/article/details/104872566 https://gist.github.com/xpn/93f2b75bf086baf2c388b2ddd50fb5d0 原理:正常的ssp扩展(dll)可以加载到lsass进程中去,比如kerberos验证都是通...

2ed 3528

Mining Station on the Sea (Dijktra + KM)

题目链接 /* * @Author: Achan * @Date: 2018-10-29 19:10:05 * @Last Modified by: Achan * @Last Modified time: 2018-10-29 21:48:44 */ #include&amp;lt;bits/stdc++.h&amp;gt; #include&amp;lt;algorithm&amp;gt; #include&amp;lt;i...

Harukaze 267

来自工业界的知识库 RAG 服务(二),RagFlow 源码全流程深度解析

前面介绍过有道 QAnything 源码解析,通过深入了解工业界的知识库 RAG 服务,得到了不少调优 RAG 服务的新想法。因此本次趁热打铁,额外花费一点时间,深入研究了另一个火热的开源 RAG 服务RagFlow的完整实现流程,希望同样有所收获。首先依旧可以先从框架图入手,与常规的 RAG 架构进行一些比较可以看到右侧知识库被明显放大,同时最右侧详细介绍了文件解析的各种手段,比如OCR等,这些在常规的 RAG 中可能会作为一个不起眼的。

易迟的专栏 2万+

告别手绘!用Aseprite+Unity Tilemap,从AI生成到2D游戏地图的保姆级搭建流程

本文详细介绍了如何利用Aseprite和Unity Tilemap从AI生成素材到搭建2D游戏地图的完整流程。涵盖AI生成素材的预处理、图层管理、动画制作,以及Unity中的Tilemap系统配置和性能优化技巧,帮助开发者高效创建像素风游戏地图。

weixin_29015483的博客 607

Mining Station on the Sea 【HDU - 2448】【KM算法+多源最短路】

题目链接 题意: 给你N个港口,有N艘船,它们分别在不同的矿厂,现在呢,它们要返回港口了,并且每个港口只能停靠一只船,而且呢船只要到了港口就不可以再往外跑了。现在给出K条边,是矿厂与矿厂之间的无向路径,然后呢有P条边,是港口与矿厂之间的无向路径。 思路: 在这里,我们需要把“进了港口就不能再出来了”这个关键点用好,我们肯定可以确定的是船只能在矿厂和矿厂之间移动,所以我们不妨...

既然弱小,就只顾变强就是了 268

PCL DBSCAN密度聚类【2026最新版】

点云密度聚类的PCL代码实现,与matlab自带的密度聚类算法进行对比验证,结果一致。3万多个点能再2秒左右运行出结果,算法效率尚可。

点云侠的博客 5690

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

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

弱智就要多努力 632

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

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

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

【转载】【最短路Floyd+KM 最佳匹配】hdu 2448 Mining Station on the Sea

Mining Station on the Sea Problem Description The ocean is a treasure house of resources and the development of human society comes to depend more and more on it. In order to develop and utiliz...

weixin_30609287的博客 127

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

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

hdu 2448 Mining Station on the Sea(最短路径+KM)

<br /> <br /> <br />http://acm.hdu.edu.cn/showproblem.php?pid=2448<br /> <br />题目大意:海上有M个油田,有N个的船只和港口,N<=M,N艘船停在N个油田,油田和油田之间有距离,油田和港口之间也有距离。现在呢要你把船开会港口,每个港口只能停一艘船,问你怎么停可以使船只行驶的距离最短呢。<br /> <br />解题思路:先把船只所停的油田作为起点,做N次的最短路径,分别求出,从源点到港口的最短路径,这个就作为它们的权值,之后KM算

话别深秋 2122

HDU 2448 Mining Station on the sea(KM算法)

一开始两种放个矩阵里,不知道那里错了,一直调不对,放一个矩阵就好了。#include #include #include #include using namespace std; typedef long long LL; const int maxn=200+5; const int inf=(1<<30); int w[maxn][maxn]; int lx[maxn],ly[maxn];

qq_33901573的博客 236

HDU 2448 Mining Station on the Sea(KM最大匹配+floyd)

HDU 2448 Mining Station on the Sea 题目链接 题意:给定n个港口,m个油田,k行表示油田之间连边关系,p行表示港口和油田之间连边关系,都是无向边,在给定n个船起始在的油田位置,求每个船要开进一个港口,最短的总距离。注意,港口只能进入一次,进入就不能出来了 思路:先把油田和港口都连边,利用floyd求出最短路,注意由于港口不能做中转,所以枚举的时候

Remilia's 753

HDU 2448 Mining Station on the Sea

预处理轮船到各个港口的最短路径,然后KM解之即可。 /*HDU 2448*/ #include #include #include #include #include #include #include #include #include #include using namespace std; const int maxn = 310; const int INF =

Wall_F的专栏 1069

hdu 2448 Mining Station on the Sea

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

小媛在努力~ 1260

Mining Station on the Sea (hdu 2448 SPFA+KM)

题意:有m个海上基站,n个港湾,现在有n只船在n个基站里,基站与基站之间有通讯的船才可以走这条路,告诉基站之间的距离,基站与港湾的距离,现在船要回到港湾,一个港湾只能停靠一只船,而且一旦进去就不能出来了,求所有船都回到港湾要走的最短距离之和。 思路:先用最短路求出每个船的起始点到每个港湾的最短距离,并且连边,然后求二分图的最小权匹配,用KM算法。费用流也可以做,但我姿势不够优美超时了。。。

@you! 963

HDU 2448 — Mining Station on the Sea 最优匹配+Floyd

原题:http://acm.hdu.edu.cn/showproblem.php?pid=2448 题意: 第一行给出n、m、k、p; n表示有n个港口和n艘船;m表示有m个油田;k表示油田之间的k条无向边;p表示港口到油田的p条有向边; 第二行给出n艘船所在油田的位置; 下面k行给出k条边的信息;接下去p行给出p条边的信息; 问将这n艘船开回港口(一个港口最多容纳一艘船)的最短路程;

吧唧吧唧 553
上一篇: hdu 5927 Auxiliary Set dfs
下一篇: 莫队算法理解
Kblacksmith
博客等级 码龄11年 30粉丝 81原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值