G.亲戚来了

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
 
描述

Bob 一家人要去下馆子,为什么呢?因为他姑姑的大爷的叔叔的孙子的表叔的婶婶的儿子来了,亲戚来了当然要下馆子,可是Bob家在偏僻的小山屯,饭店在城里啊

距离老远了。。。。。

于是他们决定坐车去,可是家里面就有一辆车啊,还是个拖拉机。。。。。。

并且,山路不好走啊,不能过超过这条路的载客量,于是不得不再回去一趟。。。。。。

比如,在下面的地图,假设Bob家在1号村庄,饭店在7号村庄,其中一条边表示给条路上的最大载客量

现在Bob要将他的亲戚以及家人99人(不包含Bob)送到城里面,选择的最好路线是1->2->4->7

并且往返5次。。。。。现在我们请你帮忙计算Bob将亲戚以及家人送到城镇里面所用的最少往返次数。。。

 
输入
输入包含若干组数据,每组数据的第一行有两个整数n(n<=100)和r,分别表示村庄的数量,和道路的数量,接下来的R行每行有三个整数
u,v,w;表示u号村庄到v号村庄有一条路以及这条路的最大载客量为w,
随后的一行三个数x,y,d,表示Bob的家在x号村庄,饭店在y号村庄以及Bob和他亲戚的总人数
输出
输出最少的往返的次数,如果到达不了请输出-1;
样例输入
7 10
1 2 30
1 3 15
1 4 10
2 4 25
2 5 60
3 4 40
3 6 20
4 7 35
5 7 20
6 7 30
1 7 99
样例输出
5
上传者
ACM_王亚龙

解题:试了几种姿势,发现求最短路比较好,不过要注意起点等于终点这种坑爹情况。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc{
int to,w,next;
arc(int x = ,int y = ,int z = -){
to = x;
w = y;
next = z;
}
};
arc e[maxn*maxn];
int head[maxn],tot,n,m,S,T,d[maxn];
bool done[maxn];
void add(int u,int v,int cost){
e[tot] = arc(v,cost,head[u]);
head[u] = tot++;
}
void dijkstra(){
for(int i = ; i <= n; ++i){
d[i] = ;
done[i] = false;
}
priority_queue< pii,vector< pii >,less< pii > >q;
d[S] = INF;
q.push(make_pair(d[S],S));
while(!q.empty()){
int u = q.top().second;
q.pop();
if(done[u]) continue;
done[u] = true;
for(int i = head[u]; ~i; i = e[i].next){
if(d[e[i].to] < min(d[u],e[i].w)){
d[e[i].to] = min(d[u],e[i].w);
q.push(make_pair(d[e[i].to],e[i].to));
}
}
}
}
int main(){
int u,v,w;
while(~scanf("%d %d",&n,&m)){
memset(head,-,sizeof(head));
for(int i = tot = ; i < m; ++i){
scanf("%d %d %d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
scanf("%d %d %d",&S,&T,&w);
if(S == T){
puts("");
continue;
}
dijkstra();
if(d[T] <= ) puts("-1");
else{
double tmp = w*1.0/(d[T]-);
printf("%.0f\n",ceil(tmp));
}
}
return ;
}

最新文章

  1. 《图解Spark:核心技术与案例实战》介绍及书附资源
  2. 一个小时快速搭建微信小程序教程
  3. mysql新建用户的方法
  4. objc@interface的设计哲学与设计技巧
  5. 如何使用不同参数组合生成独立的TestCase函数(Python)
  6. 网络编程socket基本API详解(转)
  7. hadoop分布式部署(2014-3-8)
  8. keditor_php图片上传
  9. .NET中ToString()的用法
  10. 初级SQL开发汇总指南
  11. 去除tableView上面的黑色部分 解决办法
  12. IIC接口下的24C02 驱动分析
  13. Vue项目搭建及原理一
  14. x264源代码简单分析:x264_slice_write()
  15. 利用cocos2d-x实现CandyCrushSaga消除功能
  16. easyui commobox省市区县三级联动
  17. C#操作DbCommand类
  18. Temporal Segment Networks
  19. 多线程Thread
  20. 最长公共前缀(java实现)

热门文章

  1. Vue学习之路第十篇:简单计算器的实现
  2. laravel报错:MassAssignmentException
  3. Mybatis动态代理实现函数调用
  4. java整型byte,short,int,long取值范围大小
  5. 数据库范式1NF 2NF 3NF BCNF(实例)通俗易懂的讲解
  6. Qt之二维码扫描
  7. [ReactVR] Add Shapes Using 3D Primitives in React VR
  8. Excel数据导入___你hold住么(一)
  9. spring RestTemplate 实例(NameValuePair)
  10. iOS开发之十万个为什么&amp;lt;1&amp;gt;