2013-2014 ACM-ICPC Pacific Northwest Regional Contest B.Bones’s Battery
题意略。
思路:
这个题目求的是第一个可行解,由此想到用二分试探的方式来解决。
现在讲讲怎么验证该解是否合理:
先用floyd求出两两之间的最短距离。
dp[ i ][ j ]表示,i 到 j 至少要充几次电,如果dist[ i ][ j ] <= 当前规定的试探值,那么令dp[ i ][ j ] = 1,否则赋值为INF,表示不知道要充几次电,
这个要靠第二次Floyd来更新。在第二次跑完Floyd之后,看dp数组中的最大值,如果最大值小于k,那么说明合法。
详见代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL F = 0x3f; LL dist[][];
LL dp[][];
int n,k,m; void init(){
memset(dist,F,sizeof(dist));
memset(dp,F,sizeof(dp));
} void floyd1(){
for(int i = ;i < n;++i)
dist[i][i] = ;
for(int k = ;k < n;++k)
for(int i = ;i < n;++i)
for(int j = ;j < n;++j)
dist[i][j] = min(dist[i][j],dist[i][k] + dist[k][j]);
} void floyd2()
{
for(int i = ;i < n;++i)
dp[i][i]=;
for(int k = ;k < n;++k)
for(int i = ;i < n;++i)
for(int j = ;j < n;++j)
dp[i][j] = min(dp[i][j],dp[i][k] + dp[k][j]);
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{ scanf("%d%d%d",&n,&k,&m);
init();
int u,v,d;
for(int i = ;i<m;i++)
{
scanf("%d%d%d",&u,&v,&d);
dist[u][v] = dist[v][u] = d;
}
floyd1();
LL l = ,r = INF;
LL ans = r;
LL mid;
while(l <= r)
{
mid = (l + r)>>;
for(int i = ;i < n;++i)
for(int j = ;j < n;++j)
dp[i][j] = dist[i][j] <= mid ? : INF;
floyd2();
LL d = ;
for(int i = ;i < n;++i)
for(int j = ;j < n;++j)
if(dp[i][j] > d) d = dp[i][j];
if(d <= k){
ans = mid;
r = mid - ;
}
else{
l = mid + ;
}
}
printf("%lld\n",ans);
}
return ;
}
2013-2014 ACM-ICPC Pacific Northwest Regional Contest B.Bones’s Battery的更多相关文章
- 2018 ICPC Pacific Northwest Regional Contest I-Inversions 题解
题目链接: 2018 ICPC Pacific Northwest Regional Contest - I-Inversions 题意 给出一个长度为\(n\)的序列,其中的数字介于0-k之间,为0 ...
- Contest Setting 2018 ICPC Pacific Northwest Regional Contest dp
题目:https://vj.69fa.cn/12703be72f729288b4cced17e2501850?v=1552995458 dp这个题目网上说是dp+离散化这个题目要对这些数字先处理然后进 ...
- ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków
ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków Problem A: Rubik’s Rect ...
- 2018-2019 ACM-ICPC Pacific Northwest Regional Contest (Div. 1)
2018-2019 ACM-ICPC Pacific Northwest Regional Contest (Div. 1) 思路: A Exam 思路:水题 代码: #include<bits ...
- 2016-2017 ACM-ICPC Pacific Northwest Regional Contest (Div. 1) Solution
A:Alphabet Solved. 签. #include<bits/stdc++.h> using namespace std; ]; ]; int main(){ scanf(); ...
- 2015-2016 ACM-ICPC Pacific Northwest Regional Contest (Div. 2) S Surf
SurfNow that you've come to Florida and taken up surng, you love it! Of course, you've realized that ...
- 2018-2019 ACM-ICPC Pacific Northwest Regional Contest C Contest Setting(DP)
比赛链接:Contest Setting C题 题意:$n$道题目,每道题目难度为$ai$,选择$k$道难度不同的题目,有多少种选择方案.$1<=k<=n<=1000,1<=a ...
- 2016-2017 ACM-ICPC Pacific Northwest Regional Contest (Div. 2) 题解
[题目链接] A - Alphabet 最长公共子序列.保留最长公共子序列,剩余的删除或者补足即可. #include <bits/stdc++.h> using namespace st ...
- 2018-2019 ACM-ICPC Pacific Northwest Regional Contest (Div. 1) Solution
A:Exam Solved. 温暖的签. #include<bits/stdc++.h> using namespace std; ; int k; char str1[maxn], st ...
随机推荐
- 小白学python-day04-运算符、while循环相关
今天是day04.以下是学习总结. 但行努力,莫问前程. ----------------------------------------------------------------------- ...
- webpack基础知识
一.基础 1 安装 npm i -g webpack webpack-cli // 推荐安装至本地 npm i -D webpack webpack-cli 2 webpck基础使用 2.1 webp ...
- HTML--CSS样式表--格式与布局
一:position:fixed 锁定位置(相对于浏览器的位置),例如有些网站的右下角弹窗 例如: <head> <title>网页标题</title> <s ...
- python课堂整理13---函数的作用域及匿名函数
name = 'alex' def foo(): name = 'jinling' def bar(): print(name) return bar a = foo() print(a) 阅读上述代 ...
- 记一次远程CMD开发过程
开发初衷: 有些同学电脑老是要出问题,但又不是什么大问题,通常几句cmd就能搞定.之前解决方案有2:一是远程演示,我口述别人操作:一是我写个cmd脚本,但毕竟不在本机不好调试.(吐槽一下常用的远程控制 ...
- 第15个算法-实现 Trie (前缀树)(LeetCode)
解法代码来源 :https://blog.csdn.net/whdAlive/article/details/81084793 算法来源:力扣(LeetCode)链接:https://leetcode ...
- MyBatis在Spring环境下的事务管理
MyBatis的设计思想很简单,可以看做是对JDBC的一次封装,并提供强大的动态SQL映射功能.但是由于它本身也有一些缓存.事务管理等功能,所以实际使用中还是会碰到一些问题--另外,最近接触了JFin ...
- 洛谷 P5150 题解
题面 因为 n=lcm(a,b)n = lcm(a, b)n=lcm(a,b) ,可以得出: a 和 b 的质因数都是 n 的质因数 对于 n 的每个质因数 x ,在 n 中的次数为 y ,那么 ...
- 用泛型写Redis缓存与数据库操作工具类
功能描述: 先从缓存获取数据,如果缓存没有,就从数据库获取数据,并设置到缓存中,返回数据. 如果数据库中没有数据,需要设置一个缓存标记flagKey,防止暴击访问数据库,用缓存保护数据库. 当删除缓存 ...
- 关于AJAX的跨域问题
最近过年的这几天在做毕业设计的时候遇到了一个关于AJAX的跨域问题,本来我是想要用一下聚合数据平台提供的天气预报的接口的,然后做一个当地的天气情况展示,但是在使用AJAX的时候,被告知出现错误了. 这 ...