[codeforces 852 D] Exploration Plan 解题报告 (二分+最大匹配)
题目链接:http://codeforces.com/problemset/problem/852/D
题目大意:
有V个点,N个队伍,E条边,经过每条边有个时间,告诉你初始N个队伍的位置,求至少有K个队伍在不同的点的最短时间
题解:
我们二分答案时间,显然具有单调性。先floyd预处理两点之间的最短路,二分答案后把每个人和他可以走到的点连起来,跑一次最大匹配。若最大匹配数大于等于k,说明当前答案小了,否则大了
(每个匹配相等于一个人和他最终去的位置)
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cstdio>
using namespace std; const int N=+;
const int inf=1e9+;
const int Inf=;
int v,e,n,k;
int pos[N],d[N][N],mp[N][N],match[N],vis[N];
inline int read()
{
char ch=getchar();
int s=,f=;
while (ch<''||ch>'') {if (ch=='-') f=-;ch=getchar();}
while (ch>=''&&ch<='') {s=(s<<)+(s<<)+ch-'';ch=getchar();}
return s*f;
}
void floyd()
{
for (int k=;k<=v;k++)
for (int i=;i<=v;i++)
for (int j=;j<=v;j++) d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
}
bool dfs(int x)
{
for (int i=;i<=v;i++)
{
if (vis[i]||!mp[x][i]) continue;
vis[i]=;
if (match[i]==-||dfs(match[i]))
{
match[i]=x;
return true;
}
}
return false;
}
int check(int l)
{
memset(match,-,sizeof(match));
for (int i=;i<=n;i++)
for (int j=;j<=v;j++)
mp[i][j]=(d[pos[i]][j]<=l);
int ans=;
for (int i=;i<=n;i++)
{
memset(vis,,sizeof(vis));
if (dfs(i)) ans++;
}
return ans;
}
int main()
{
v=read();e=read();n=read();k=read();
for (int i=;i<=v;i++)
for (int j=;j<=v;j++) if (i!=j) d[i][j]=inf;
for (int i=;i<=n;i++) pos[i]=read();
for (int i=,u,v,w;i<=e;i++)
{
u=read();v=read();w=read();
d[u][v]=d[v][u]=min(d[u][v],w);
}
floyd();
int l=,r=Inf,ans=Inf;
while (l<r)
{
int mid=l+r>>;
if (check(mid)>=k) r=mid,ans=min(ans,r);
else l=mid+;
}
if (ans==Inf) puts("-1");
else printf("%d\n",ans);
return ;
}
[codeforces 852 D] Exploration Plan 解题报告 (二分+最大匹配)的更多相关文章
- Codeforces Educational Round 92 赛后解题报告(A-G)
Codeforces Educational Round 92 赛后解题报告 惨 huayucaiji 惨 A. LCM Problem 赛前:A题嘛,总归简单的咯 赛后:A题这种**题居然想了20m ...
- codeforces 476C.Dreamoon and Sums 解题报告
题目链接:http://codeforces.com/problemset/problem/476/C 题目意思:给出两个数:a 和 b,要求算出 (x/b) / (x%b) == k,其中 k 的取 ...
- Codeforces Round #382 (Div. 2) 解题报告
CF一如既往在深夜举行,我也一如既往在周三上午的C++课上进行了virtual participation.这次div2的题目除了E题都水的一塌糊涂,参赛时的E题最后也没有几个参赛者AC,排名又成为了 ...
- codeforces 557B. Pasha and Tea 解题报告
题目链接:http://codeforces.com/problemset/problem/557/B 题目意思:有 2n 个茶杯,规定第 i 个茶杯最多只能装 ai 毫升的水.现在给出 w 毫升的水 ...
- codeforces 507B. Amr and Pins 解题报告
题目链接:http://codeforces.com/problemset/problem/507/B 题目意思:给出圆的半径,以及圆心坐标和最终圆心要到达的坐标位置.问最少步数是多少.移动见下图.( ...
- codeforces 500B.New Year Permutation 解题报告
题目链接:http://codeforces.com/problemset/problem/500/B 题目意思:给出一个含有 n 个数的排列:p1, p2, ..., pn-1, pn.紧接着是一个 ...
- Codeforces Round #380 (Div. 2) 解题报告
第一次全程参加的CF比赛(虽然过了D题之后就开始干别的去了),人生第一次codeforces上分--(或许之前的比赛如果都参加全程也不会那么惨吧),终于回到了specialist的行列,感动~.虽然最 ...
- codeforces 483B Friends and Presents 解题报告
题目链接:http://codeforces.com/problemset/problem/483/B 题目意思:有两个 friends,需要将 cnt1 个不能整除 x 的数分给第一个friend, ...
- codeforces B. Xenia and Ringroad 解题报告
题目链接:http://codeforces.com/problemset/problem/339/B 题目理解不难,这句是解题的关键 In order to complete the i-th ta ...
随机推荐
- USACO 2.1 Hamming Codes
Hamming CodesRob Kolstad Given N, B, and D: Find a set of N codewords (1 <= N <= 64), each of ...
- 基于QMP实现对qemu虚拟机进行交互
本文详解QMP,包含qmp.hmp.qemu-guest-agent的介绍.工作原理.配置方法.范例 小慢哥的原创文章,欢迎转载 目录 ▪ QMP介绍 ▪ QMP语法 ▪ 单独使用qemu,启用QMP ...
- ubuntu下无法将iNode绑定到侧边栏的解决办法
title: ubuntu下无法将iNode绑定到侧边栏的解决办法 toc: false date: 2018-09-01 17:43:52 categories: methods tags: ubu ...
- spring Ioc Aop整合
之前用DWP项目做spring的IOC,xml总是提示有问题,之后改用maven通过. 之后将这一块的内容补充. 仔细考虑一下spring 的IOC是无处不在的,演示Aop也需要依赖spring的IO ...
- Combox程序中自定义数据源
有combox控件,命名为cbxPutStatus,在程序中为其手工定义数据源,并绑定 private void POrderSplitFrm_Load(object sender, EventArg ...
- Hibernate框架学习(九)——Criteria语法
一.语法 1.基本查询 2.条件查询 3.分页查询 4.排序查询 5.统计查询 二.离线查询 1.非离线的Criteria 2.离线的Criteria 3.演示 public class Demo2 ...
- RAP开发入门-运行第一个HelloWorld(二)
环境搭建好了之后我们就可以照惯例运行第一个helloworld程序了. (ps:这里钉几个资料吧 官网开发指导:http://help.eclipse.org/indigo/index.jsp?top ...
- X Macro
30年前我念大学时从一个朋友那里学来的一个技巧. 它是汇编语言的一个宏,但很容易转换为C语言宏. 我一直在使用它,但有意思的是我还从没在别人的代码中看到过.现在该我把这个小技巧传递下去了. 让我们举个 ...
- Kattis - Speed Limit
Speed Limit Bill and Ted are taking a road trip. But the odometer in their car is broken, so they do ...
- Hihocoder1458-Parentheses Matching(stack,vector)
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Given a string of balanced parentheses output all the matchin ...