先根据不同的起点跑最短路,记录距离,从而建立二分图求最小匹配。

一开始我求最短路的时候我把港口直接加到图中,然后发现进了港口就不能出来了,所以连接港口的边就要从双向边改成单向边…………这也搞得我n和m分不清了……

还不如排除掉港口算最短路后再统计各艘船到各个港口的最短距离……

然后我还傻叉地用了Dijkstra来求最短路,当然TLE咯……

事实证明,Floyd就可以了嘛我智商跑哪里去了。。。

#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
#include <cctype>
#include <queue>
#define rep(i, l, r) for(int i=l; i<=r; i++)
#define clr(x, c) memset(x, c, sizeof(x))
#define N 234
#define MAX 0x3fffffff
#define ll long long
using namespace std;
int read()
{
int x=0, f=1; char ch=getchar();
while (!isdigit(ch)) { if (ch=='-') f=-1; ch=getchar(); }
while (isdigit(ch)) { x=x*10+ch-'0'; ch=getchar(); }
return x*f;
} int n, m, en, p, l[N], st[N], lx[N], ly[N], v[N][N], k[N], d[N][N], w[N];
bool vx[N], vy[N]; bool Find(int x)
{
vx[x]=1;
rep(y, 1, n)
{
if (vy[y]) continue;
int a=lx[x]+ly[y]-v[x][y];
if (!a)
{
vy[y]=1; if (!l[y] || Find(l[y])) { l[y]=x; return 1; }
}
else st[y]=min(st[y], a);
}
return false;
} inline int km()
{
clr(l, 0); rep(i, 1, n) lx[i]=-MAX;
rep(i, 1, n) rep(j, 1, n) if (lx[i]<v[i][j]) lx[i]=v[i][j]; rep(i, 1, n) if (lx[i]==-MAX) return 1;
clr(ly, 1); rep(i, 1, n) rep(j, 1, n) if (v[i][j]!=-MAX) ly[j]=0; rep(i, 1, n) if (ly[i]) return 1;
rep(i, 1, n)
{
rep(j, 1, n) st[j]=MAX;
while (1)
{
clr(vx, 0); clr(vy, 0);
if (Find(i)) break; int a=MAX;
rep(j, 1, n) if (!vy[j] && st[j]<a) a=st[j];
if (a==MAX) return 1;
rep(j, 1, n) if (vx[j]) lx[j]-=a;
rep(j, 1, n) if (vy[j]) ly[j]+=a; else st[j]-=a;
}
}
int a=0; rep(i, 1, n) a+=lx[i]+ly[i];
return a;
} int main()
{
while (~scanf("%d%d%d%d", &n, &m ,&en, &p))
{
rep(i, 1, m) rep(j, 1, m) d[i][j] = i==j?0:MAX;
rep(i, 1, n) w[i]=read();
rep(i, 1, en) { int x=read(), y=read(), z=read(); if (d[x][y]>z) d[x][y]=d[y][x]=z; }
rep(o, 1, m) rep(i, 1, m) rep(j, 1, m) if (d[i][o]+d[o][j]<d[i][j]) d[i][j]=d[i][o]+d[o][j]; rep(i, 1, n) rep(j, 1, n) v[i][j]=MAX;
rep(j, 1, p)
{
int x=read(), y=read(), z=read();
rep(i, 1, n) if (v[i][x]>d[w[i]][y]+z)
v[i][x]=d[w[i]][y]+z;
}
rep(i, 1, n) rep(j, 1, n) v[i][j]*=-1;
printf("%d\n", -km());
}
return 0;
}

  

HDU-2448 Mining Station on the Sea的更多相关文章

  1. 【转载】【最短路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 dev ...

  2. 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 ...

  3. 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 ...

  4. hdu 2448(KM算法+SPFA)

    Mining Station on the Sea Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  5. hdu 3879 Base Station 最大权闭合图

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3879 A famous mobile communication company is plannin ...

  6. HDU 3879 Base Station

    Base Station Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. Original I ...

  7. [hdu contest 2019-07-29] Azshara's deep sea 计算几何 动态规划 区间dp 凸包 graham扫描法

    今天hdu的比赛的第一题,凸包+区间dp. 给出n个点m个圆,n<400,m<100,要求找出凸包然后给凸包上的点连线,连线的两个点不能(在凸包上)相邻,连线不能与圆相交或相切,连线不能相 ...

  8. [2019HDU多校第三场][HDU 6603][A. Azshara's deep sea]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6603 题目大意:给出一个凸包,凸包内有若干个圆,要求画尽可能多的对角线使得他们两两不在凸包内相交且不与 ...

  9. HDU 3879 Base Station(最大权闭合子图)

    经典例题,好像说可以转化成maxflow(n,n+m),暂时只可以勉强理解maxflow(n+m,n+m)的做法. 题意:输入n个点,m条边的无向图.点权为负,边权为正,点权为代价,边权为获益,输出最 ...

随机推荐

  1. [论文理解] MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications

    MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications Intro MobileNet 我 ...

  2. 【转】IOS开发—IOS 8 中设置applicationIconBadgeNumber和消息推送

    在IOS7中设置applicationIconBadgeNumber不会有什么问题,但是直接在IOS8中设置applicationIconBadgeNumber会报错 因为在IOS8中要想设置appl ...

  3. iBatis for net 框架使用

    简介:ibatis 一词来源于“internet”和“abatis”的组合,是一个由Clinton Begin在2001年发起的开放源代码项目,到后面发展的版本叫MyBatis但都是指的同一个东西.最 ...

  4. Python SciPy Sparse模块学习笔记

    1. sparse模块的官方document地址:http://docs.scipy.org/doc/scipy/reference/sparse.html   2. sparse matrix的存储 ...

  5. LINQ中AsEnumerable与AsQueryable的区别

    AsEnumerable将一个序列向上转换为一个IEnumerable, 强制将Enumerable类下面的查询操作符绑定到后续的子查询当中:AsQueryable将一个序列向下转换为一个IQuery ...

  6. MySQL 5.7.20绿色版安装详细图文教程

    MySQL 5.7.20绿色版安装详细图文教程 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle旗下产品.这篇文章主要介绍了MySQL 5.7.20绿色版安装 ...

  7. 01_9_ServletContext

    01_9_ServletContext 1. 例子 public void doGet(HttpServletRequest request, HttpServletResponse response ...

  8. Linux运维笔记--第三部

    第三部 3. Linux系统文件重要知识初步讲解 # ls  -lhi   (i: inode,每个文件前的数字代表文件身份ID:h: human 人类可读) 显示:25091 -rw-r--r-- ...

  9. 『jQuery』.html(),.text()和.val()的概述及使用--2015-08-11

    如何使用jQuery中的.html(),.text()和.val()三种方法,用于读取,修改元素的html结构,元素的文本内容,以及表单元素的value值的方法   本节内容主要介绍的是如何使用jQu ...

  10. NOIP模拟赛 czy的后宫6

    czy的后宫6 题目描述 众所周知的是丧尸czy有很多妹子(虽然很多但是质量不容乐观QAQ),今天czy把n个妹子排成一行来检阅.但是czy的妹子的质量实在……所以czy看不下去了.检阅了第i个妹子会 ...