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

一开始我求最短路的时候我把港口直接加到图中,然后发现进了港口就不能出来了,所以连接港口的边就要从双向边改成单向边…………这也搞得我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. 得到本地应用程序的EXE的路径

    实现效果: 知识运用: Application类的ExecutablePath属性 //获取启动了应用程序的可执行文件的路径和可执行文件的名称    public static string Exec ...

  2. 两个有序数列,求中间值 Median of Two Sorted Arrays

    原题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...

  3. 【转】Java8学习笔记(1) -- 从函数式接口说起

    http://blog.csdn.net/zxhoo/article/details/38349011 函数式接口 理解Functional Interface(函数式接口,以下简称FI)是学习Jav ...

  4. angular设置反向代理

    本地调试,需要用到服务器的api,发现chrome安全问题,需要解决跨域问题.现给出解决方案: 1.增加proxy.conf.json文件 位置与package.json文件同级(可指定) 2.pac ...

  5. js中读取解析json数据

    在数据传输流程中,json是以文本,即字符串的形式传递的,而JS操作的是JSON对象,所以,JSON对象和JSON字符串之间的相互转换是关键. JSON字符串:       'var str1 = ' ...

  6. Feign-手动创建FeignClient

    前言 在<Feign-请求不同注册中心的服务>中,提到,如果需要请求不同注册中心的服务,可以设置@FeignClient的url属性. 这种做法有个缺点,需要服务消费者,配置各个环境的ur ...

  7. Mybatis学习记录(3)

    1.输出映射和输入映射 Mapper.xml映射文件定义了操作数据库的sql,每个sql就是一个statement,映射文件是mybatis的核心. (1)parameterType(输入类型)   ...

  8. 功能强大的CURL

      linux下的curl,有着非同一般的魔力,有人称它为下载工具,我更倾向于叫它“文件传输工具”因为它好像无所不能.从常见的 FTP, HTTP, TELNET, 等协议,还支持代理服务器,cook ...

  9. mac 升级EI Capitan后遇到c++转lua时遇到libclang.dylib找不到的错

    升级EI Capitan后,打包lua脚本时,会报这个错: LibclangError: dlopen(libclang.dylib, 6): image not found. To provide ...

  10. js函数式编程(二)-柯里化

    这节开始讲的例子都使用简单的TS来写,尽量做到和es6差别不大,正文如下 我们在编程中必然需要用到一些变量存储数据,供今后其他地方调用.而函数式编程有一个要领就是最好不要依赖外部变量(当然允许通过参数 ...