【转载】【最短路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 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
Source
参考博客:
http://www.voidcn.com/article/p-msnnxvzx-da.html
题目大意:有n条船,n个港口,m个点,k条无向边,p条有向边,
其中第一行输入为n条船停靠在的点,现在这些船需要回到港口,而且每个港口只能挺一条船。
k条无向边,表示两个点之间的距离,
p条有向边,表示港口到某个点的距离。
每个船都需要回到港口,问最小距离花费。
思路:
1、因为总点数并不是很大,所以我们可以使用Floyd跑出亮点间最短路。
2、然后设定a[i][j]表示第i个船到第j个港口需要花费的最小消耗,那么我们通过Floyd跑出来的结果对应建图。
3、因为我们要求的是最小匹配,那么我们使用一个极大值(03f3f3f3f-a[i][j])得到一个新的a[i][j],使得原来的较小值变成了较大值,那么此时跑得的最大匹配即最小匹配,那么ans=n*极大值-最大值匹配。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define ll __int64
int map[][];
int match[*];
int a[][];
int lx[*];
int ly[*];
int vx[*];
int vy[*];
int at[];
int low;
int n,m,kk,pp;
int find(int u)
{
vx[u]=;
for(int i=;i<=n;i++)
{
if(vy[i]==)continue;
int tmp=lx[u]+ly[i]-a[u][i];
if(tmp==)
{
vy[i]=;
if(match[i]==-||find(match[i]))
{
match[i]=u;
return ;
}
}
else if(tmp<low)low=tmp;
}
return ;
}
void KM()
{
memset(match,-,sizeof(match));
memset(lx,,sizeof(lx));
memset(ly,,sizeof(ly));
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
int u=at[i];
a[i][j]=map[u][j+m];
a[i][j]=0x3f3f3f3f-a[i][j];
lx[i]=max(lx[i],a[i][j]);
}
}
for(int i=;i<=n;i++)
{
while()
{
low=0x3f3f3f3f;
memset(vx,,sizeof(vx));
memset(vy,,sizeof(vy));
if(find(i))break;
for(int j=;j<=n;j++)
{
if(vx[j])lx[j]-=low;
}
for(int j=;j<=n;j++)
{
if(vy[j])ly[j]+=low;
}
}
}
ll sum=;
for(int i=;i<=n;i++)
{
sum+=a[match[i]][i];
}
printf("%I64d\n",(ll)n*(ll)0x3f3f3f3f-sum);
}
int main()
{
while(~scanf("%d%d%d%d",&n,&m,&kk,&pp))
{
memset(map,0x3f3f3f3f,sizeof(map));
for(int i=;i<=n;i++)
{
scanf("%d",&at[i]);
}
for(int i=;i<=kk;i++)
{
int x,y,w;
scanf("%d%d%d",&x,&y,&w);
if(w<map[x][y])
{
map[x][y]=w;
map[y][x]=w;
}
}
for(int i=;i<=pp;i++)
{
int x,y,w;
scanf("%d%d%d",&x,&y,&w);
if(map[y][x+m]>w)
{
map[y][x+m]=w;
}
}
for(int i=;i<=n+m;i++)
{
for(int j=;j<=n+m;j++)
{
for(int k=;k<=n+m;k++)
{
map[j][k]=min(map[j][i]+map[i][k],map[j][k]);
}
}
}
KM();
}
}
KM+flody
【转载】【最短路Floyd+KM 最佳匹配】hdu 2448 Mining Station on the Sea的更多相关文章
- Mining Station on the Sea (hdu 2448 SPFA+KM)
Mining Station on the Sea Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- 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 (Jav ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- hdu 2448(KM算法+SPFA)
Mining Station on the Sea Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- HDU 1533 KM算法(权值最小的最佳匹配)
Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU 2426 Interesting Housing Problem(二分图最佳匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=2426 题意:每n个学生和m个房间,现在要为每个学生安排一个房间居住,每个学生对于一些房间有一些满意度,如果满意度 ...
- 二分图带权匹配、最佳匹配与KM算法
---------------------以上转自ByVoid神牛博客,并有所省略. [二分图带权匹配与最佳匹配] 什么是二分图的带权匹配?二分图的带权匹配就是求出一个匹配集合,使得集合中边的权值之和 ...
- 二分图匹配之最佳匹配——KM算法
今天也大致学了下KM算法,用于求二分图匹配的最佳匹配. 何为最佳?我们能用匈牙利算法对二分图进行最大匹配,但匹配的方式不唯一,如果我们假设每条边有权值,那么一定会存在一个最大权值的匹配情况,但对于KM ...
- hdu2255 奔小康赚大钱 二分图最佳匹配--KM算法
传说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子.这可是一件大事,关系到人民的住房问题啊.村里共有n间房间,刚好有n家老百姓,考虑到每家都要有房住(如果有老百姓没房子住 ...
随机推荐
- nginx做反向代理时出现302错误
现象:nginx在使用非80端口做反向代理时,浏览器访问发现返回302错误 详细现象如下: 浏览器请求登录页: 输入账号密码点击登录: 很明显登录后跳转的地址少了端口号. 原因:proxy.conf文 ...
- 阿里云服务器配置https(总结)
阿里云服务器配置https(总结) 一.总结 一句话总结: 1.下载https证书(可以在阿里云上) 2.在服务器上面开启443端口 3.配置apache服务器,443的加ssl,让80的重定向到44 ...
- jquery页面滚动到指定id
//jquery页面滚动到指定id $body = (window.opera) ? (document.compatMode == "CSS1Compat" ? $('html ...
- C#-片段-插入片段:Visual C#
ylbtech-C#-片段-插入片段:Visual C# 1.返回顶部 ·#if #if true #endif ·#region #region MyRegion #endregion · 2.返回 ...
- IO流的标准处理代码
FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream("aaa.t ...
- Pattern 和 Matcher
作用:应用这个 Pattern 和 Matcher 可以完成字符串获取功能 使用: // 获取模式器对象 Pattern p = Pattern.compile("a*b") ; ...
- 论文翻译 DOTA:A Large-scale Dataset for Object Detection in Aerial Images
简介:武大遥感国重实验室-夏桂松和华科电信学院-白翔等合作做的一个航拍图像数据集 摘要: 目标检测是计算机视觉领域一个重要且有挑战性的问题.虽然过去的十几年中目标检测在自然场景已经有了较重要的成就 ...
- ISO/IEC 9899:2011 条款6.2.8——对象的对齐
6.2.8 对象的对齐 1.完整的对象类型具有对齐要求,对齐要求是对该类型对象可以在哪个地址进行分配的放置限制.一个对齐是一个实现定义的整数值,表示一个给定对象可以分配在相继两个地址之间跨多少字节的位 ...
- osgViewer::Viewer::Windows
osg自带窗口去掉边框 #ifdef _WIN32 #include <Windows.h> #endif // _WIN32 #include<iostream> #incl ...
- spring boot配置文件、日志配置和代码的多环境配置
一般项目都逃不掉开发.测试和生产这三套环境,如果每次给这三套环境打包都去改配置,累死不说,还一不留心就出差错.倒不如每套环境各给一套配置来的轻松.上代码: 1.通用配置放在application.pr ...