题意略。

思路:

这个题目求的是第一个可行解,由此想到用二分试探的方式来解决。

现在讲讲怎么验证该解是否合理:

先用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的更多相关文章

  1. 2018 ICPC Pacific Northwest Regional Contest I-Inversions 题解

    题目链接: 2018 ICPC Pacific Northwest Regional Contest - I-Inversions 题意 给出一个长度为\(n\)的序列,其中的数字介于0-k之间,为0 ...

  2. Contest Setting 2018 ICPC Pacific Northwest Regional Contest dp

    题目:https://vj.69fa.cn/12703be72f729288b4cced17e2501850?v=1552995458 dp这个题目网上说是dp+离散化这个题目要对这些数字先处理然后进 ...

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

  4. 2018-2019 ACM-ICPC Pacific Northwest Regional Contest (Div. 1)

    2018-2019 ACM-ICPC Pacific Northwest Regional Contest (Div. 1) 思路: A Exam 思路:水题 代码: #include<bits ...

  5. 2016-2017 ACM-ICPC Pacific Northwest Regional Contest (Div. 1) Solution

    A:Alphabet Solved. 签. #include<bits/stdc++.h> using namespace std; ]; ]; int main(){ scanf(); ...

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

  7. 2018-2019 ACM-ICPC Pacific Northwest Regional Contest C Contest Setting(DP)

    比赛链接:Contest Setting C题 题意:$n$道题目,每道题目难度为$ai$,选择$k$道难度不同的题目,有多少种选择方案.$1<=k<=n<=1000,1<=a ...

  8. 2016-2017 ACM-ICPC Pacific Northwest Regional Contest (Div. 2) 题解

    [题目链接] A - Alphabet 最长公共子序列.保留最长公共子序列,剩余的删除或者补足即可. #include <bits/stdc++.h> using namespace st ...

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

随机推荐

  1. php--学习封装类 (一)(操作mysql数据库的数据访问)

    <?php class DBDA //定义一个类 { //定义成员变量,不能直接定义,前面要加上public或者是private public $host = "localhost&q ...

  2. 给定一个IP地址,转化为二进制32位,再转化为十进制,写出一个方法让其十进制转为IP地址

    十进制是已知的数值 第一种方法: <script type="text/javascript"> var num=2148140545; var str=num.toS ...

  3. 动态规划_Apple Catching_POJ-2385

    It and ) in his field, each full of apples. Bessie cannot reach the apples when they are on the tree ...

  4. tomcat配置启动文件

    修改tomcat到指定文件夹 conf -> server.xml <Host name="localhost" appBase="webapps" ...

  5. Windows上切换java8和java11

    Windows上安装了java8和java11,时不时要切换,于是思考写行命令解决.思路是修改java_home变量.我的java_home变量是设置在系统级别的. 修改环境变量有2个命令,set和s ...

  6. 【JDK】JDK源码分析-AbstractQueuedSynchronizer(1)

    概述 前文「JDK源码分析-Lock&Condition」简要分析了 Lock 接口,它在 JDK 中的实现类主要是 ReentrantLock (可译为“重入锁”).ReentrantLoc ...

  7. 关于dfs的套路

    void dfs(答案, 搜索层数, 其他参数) { if (层数==maxdeep) { 更新答案 return; } (剪枝) for(下一层可能的状态){ 更新全局变量表示的状态的变量 dfs( ...

  8. Activiti6系列(4)- 三个war包的数据源及密码修改

    一.activiti-app修改数据源和密码 1.使用sublimetext工具打开tomcat,方便进行配置文件的修改. 找到被解压的war包,activiti-app/WEB-INF/classe ...

  9. Cocos经典游戏教程之仿皇室战争

    版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...

  10. android——Fragment

    谷歌官方文档的介绍: https://developer.android.com/guide/components/fragments.html#Design Fragment 表示 Activity ...