Bones’s Battery Submit

【题目链接】Bones’s Battery Submit

【题目类型】二分+Floyd

&题意:

有n个点,m条边,从点ui到点vi的费电量是di,要求对于任意的起点s和终点e,必须能遍历过所有的点一遍,并且充电的次数不超过k次(在任何一个点都可以充满电),问电池容量最小是多少?

&题解:

首先求这种题,用二分还是可以想到的,那怎么判断是否可行就比较难了;

首先用floyd求出每2点之间的最短路,之后在写出一个dp数组,根据求出的dis最短距离,来求出从任意的u点到任意的v点在走过全图的情况下最少冲了多少次电,如果其中充电次数最多的小于等于k,那么就可行.

【时间复杂度】O(n^3log1e11)

&代码:

#include <cstdio>
#include <iostream>
#include <set>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
using ll=long long;
const int maxn= 1e3 +9;
ll dis[maxn][maxn],dp[maxn][maxn];
int n,k,m;
void floyd(ll arr[][maxn])
{
for(int i=0;i<n;i++) arr[i][i]=0;
for(int k=0;k<n;k++)
for(int i=0;i<n;i++)
for(int j=0;j<n;j++){
if(arr[i][k]==LINF||i==k) continue;
arr[i][j]=min(arr[i][j],arr[i][k]+arr[k][j]);
}
}
void init()
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++){
dis[i][j]=LINF;
}
}
bool ok(ll cap)
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++){
if(dis[i][j]==LINF) return false;
dp[i][j]=dis[i][j]<=cap?1:LINF;
}
floyd(dp);
// for(int i=0;i<n;i++){
// for(int j=0;j<n;j++){
// printf("%lld ",dp[i][j]);
// }
// puts("");
// }
for(int i=0;i<n;i++)
for(int j=0;j<n;j++){
if(dp[i][j]>k) return false;
}
return true;
}
int main()
{
// ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
// freopen("E:1.txt","r",stdin);
int T;scanf("%d",&T);
while(T--){
scanf("%d%d%d",&n,&k,&m);
init();
for(int i=0;i<m;i++){
int u,v;
ll d;
scanf("%d%d%lld",&u,&v,&d);
dis[u][v]=dis[v][u]=d;
}
floyd(dis);
ll l=0,r=1e11+9,mid;
while(l<=r){
mid=(l+r)/2;
if(ok(mid)) r=mid-1;
else l=mid+1;
}
printf("%lld\n",l);
}
return 0;
}

CSU 1817 Bones’s Battery Submit(二分+Floyd)的更多相关文章

  1. HDU 4606 Occupy Cities ★(线段相交+二分+Floyd+最小路径覆盖)

    题意 有n个城市,m个边界线,p名士兵.现在士兵要按一定顺序攻占城市,但从一个城市到另一个城市的过程中不能穿过边界线.士兵有一个容量为K的背包装粮食,士兵到达一个城市可以选择攻占城市或者只是路过,如果 ...

  2. POJ 2112 Optimal Milking (二分 + floyd + 网络流)

    POJ 2112 Optimal Milking 链接:http://poj.org/problem?id=2112 题意:农场主John 将他的K(1≤K≤30)个挤奶器运到牧场,在那里有C(1≤C ...

  3. 2013-2014 ACM-ICPC Pacific Northwest Regional Contest B.Bones’s Battery

    题意略. 思路: 这个题目求的是第一个可行解,由此想到用二分试探的方式来解决. 现在讲讲怎么验证该解是否合理: 先用floyd求出两两之间的最短距离. dp[ i ][ j ]表示,i 到 j 至少要 ...

  4. 网络流--最大流--POJ 2139(超级源汇+拆点建图+二分+Floyd)

    Description FJ's cows really hate getting wet so much that the mere thought of getting caught in the ...

  5. poj2112 二分+floyd+多源多汇最大流

    /*此题不错,大致题意:c头牛去k个机器处喝奶,每个喝奶处最多容纳M头牛,求所有牛中走的最长路的 那头牛,使该最长路最小.思路:最大最小问题,第一灵感:二分答案check之.对于使最长路最短, 用fo ...

  6. BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛 网络流 + 二分 + Floyd

    Description FJ's cows really hate getting wet so much that the mere thought of getting caught in the ...

  7. Gym 101655:2013Pacific Northwest Regional Contest(寒假自训第13场)

    A .Assignments 题意:给定距离D,以及N个飞机的速度Vi,单位时间耗油量Fi,总油量Ci.问有多少飞机可以到达目的地. 思路:即问多少飞机满足(Ci/Fi)*Vi>=D  ---- ...

  8. BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛( floyd + 二分答案 + 最大流 )

    一道水题WA了这么多次真是.... 统考终于完 ( 挂 ) 了...可以好好写题了... 先floyd跑出各个点的最短路 , 然后二分答案 m , 再建图. 每个 farm 拆成一个 cow 点和一个 ...

  9. POJ 2112—— Optimal Milking——————【多重匹配、二分枚举答案、floyd预处理】

    Optimal Milking Time Limit:2000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Sub ...

随机推荐

  1. shell之使用paste命令按列拼接多个文件

    试验文件: [root@db03 shell-script]# cat text1.txt 1 2 3 4 5 [root@db03 shell-script]# cat text2.txt orac ...

  2. An optimizer that trains as fast as Adam and as good as SGD. https://www.luolc.com/publications/ad…

    设立3个指针pa.pb和pc,其中pa和pb分别指向La表和Lb表中当前待比较插入的结点,而pc指向Lc表中当前最后一个结点:若pa->data<=pb->data,则将pa所指结点 ...

  3. Type Operators instanceof is used to determine whether a PHP variable is an instantiated object of a certain class/a class that implements an interface

    w 0-instanceof is used to determine whether a PHP variable is an instantiated object of a certain cl ...

  4. PHP之二维数组根据某个下标排序

    function arraySortByElements($array2sort,$sortField,$order,$iscount=false) { $functionString=' if (' ...

  5. odoo中Python实现小写金额转换为大写金额

    自动将小写的金额数值转换为大写,方便记录 class project_parm(models.Model): def IIf(self, b, s1, s2): if b: return s1 els ...

  6. LeetCode 617 Merge Two Binary Trees 解题报告

    题目要求 Given two binary trees and imagine that when you put one of them to cover the other, some nodes ...

  7. tensorflow入门笔记(四) tf.summary 模块

    模块内的函数: tf.summary.audio(name, tensor, sample_rate, max_outputs=3, collections=None, family=None) 输出 ...

  8. selenium+xpath在不同层级的写法

    总结:定位虽然用Inndex定位最快,但是定位最好不要用浏览器自带定位xpath,尽量不要用Index,否则写的UI自动化脚本的定位元素,需要重新维护.代价太大. 一:不在同一层级,可以用[./..] ...

  9. iOS APP 在前台时弹出本地通知

    iOS10 之后使用才有效果 1.在 AppDelegate.m 文件里面添加下面的方法. - (void)userNotificationCenter:(UNUserNotificationCent ...

  10. Nor Flash 驱动框架

    框架入口源文件: lcd.c (可根据入口源文件,再按着框架到内核走一遍) 内核版本:linux_2.6.22.6      硬件平台:JZ2440 以下是驱动框架: 以下是驱动代码 s3c_nor_ ...