[codility]Grocery-store
http://codility.com/demo/take-sample-test/hydrogenium2013
用Dijkstra求最短路径,同时和D[i]比较判断是不是能到。用了优先队列优化,复杂度是(m+n)*log(n)。同时,写Dijkstra的时候一般要用dist数组,这里只拿它做访问标示。中间有个坑就是两个点之间可以多条路径,fail了半天。
#include <queue>
#include <functional> #define pp pair<int,int>
int solution(const vector<int> &A, const vector<int> &B, const vector<int> &C, const vector<int> &D) {
// write your code in C++98
int N = A.size();
int M = D.size();
vector<vector<int> > graph;
graph.resize(M);
for (int i = 0; i < M; i++) {
graph[i].resize(M, -1);
}
for (int i = 0; i < N; i++) {
graph[A[i]][B[i]] = (graph[A[i]][B[i]] == -1 ? C[i] : min(graph[A[i]][B[i]], C[i]));
graph[B[i]][A[i]] = (graph[B[i]][A[i]] == -1 ? C[i] : min(graph[B[i]][A[i]], C[i]));
} vector<int> dist(M, -1);
priority_queue<pp, vector<pp>, greater<pp> > que;
que.push(make_pair(0, 0));
while (!que.empty()) {
pp p = que.top();
que.pop();
if (dist[p.second] == -1) {
dist[p.second] = p.first;
}
else {
continue;
}
if (p.first <= D[p.second]) return p.first;
for (int i = 0; i < graph[p.second].size(); i++) {
if (graph[p.second][i] != -1) {
que.push(make_pair(graph[p.second][i] + p.first, i));
}
}
}
return -1;
}
[codility]Grocery-store的更多相关文章
- Thymeleaf3.0内容
Thymeleaf简介 什么是Thymeleaf Thymeleaf是网站或者独立应用程序的新式的服务端java模板引擎,可以执行HTML,XML,JavaScript,CSS甚至纯文本模板. Thy ...
- 加州大学伯克利分校Stat2.2x Probability 概率初步学习笔记: Final
Stat2.2x Probability(概率)课程由加州大学伯克利分校(University of California, Berkeley)于2014年在edX平台讲授. PDF笔记下载(Acad ...
- What’s the difference between data mining and data warehousing?
Data mining is the process of finding patterns in a given data set. These patterns can often provide ...
- Machine and Deep Learning with Python
Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstiti ...
- Amazon DynamoDB 概览
1. 什么是Amazon DynamoDB DynamoDB 是一种快速.全面受管的 NoSQL 数据库服务,它能让用户以简单并且经济有效地方式存储和检索任何数据量,同时服务于任何程度的请求流量.所有 ...
- 初步认识Thymeleaf:简单表达式和标签。(一)
本文只适用于不会Java对HTML语言有基础的程序员们,是浏览了各大博客后收集整理,重新编辑的一篇文章,希望能对大家有所帮助. 对于Thymeleaf,网上特别官方的解释无非就是:网站或者独立应用程序 ...
- English - Mosquitos
Smith's house is full of mosquitos. Every night they bite him. He can not sleep because the mosquito ...
- SpringBoot入门:新一代Java模板引擎Thymeleaf(理论)
Spring Boot 提供了spring-boot-starter-web来为Web开发予以支持,spring-boot-starter-web为我们提供了嵌入的Tomcat以及SpringMVC的 ...
- 初步认识thymeleaf:简单表达式和标签(一)
初步认识Thymeleaf:简单表达式和标签.(一) 本文只适用于不会Java对HTML语言有基础的程序员们,是浏览了各大博客后收集整理,重新编辑的一篇文章,希望能对大家有所帮助.最后本文如果有哪 ...
- Thymeleaf 3.0 专题
http://www.thymeleaf.org/doc/articles/layouts.html thymeleaf的初次使用(带参请求以及调用带参js方法) 之前对于前端框架接触较少,第一次接触 ...
随机推荐
- N_F1_APPROVE
package nc.bs.pub.action; import java.util.ArrayList; import java.util.Hashtable; import java.util.L ...
- C#3.0 LINQ 操作符
Table 类: public class DemoDataContext : DataContext { public DemoDataContext (string cxString) : bas ...
- 解决IllegalStateException: Can not perform this action after onSaveInstanceState:
今天做项目中的支付宝功能,是在fragment中做的,在支付成功后,想切换到支付成功的页面. 结果就报错了IllegalStateException: Can not perform this act ...
- Android的自动对焦
1,什么是自动对焦? ---安卓的自动对焦的概念是指能够在指定的位置计算出准确的焦点位置. 这个就好像是传统意义上的手动对焦.但是google是这个意思. 2.什么是追焦? ----安卓的追焦是指FO ...
- .bak文件在英文版的sqlserver2014如何生成和恢复
生成bak备份文件 1.选择数据库 2.右击选择task 3.选择backup 4.
- Java的基础概念
JDK (Java Development Kit) Java Developer Kit contains tools needed to develop the Java programs, an ...
- php ticks 调试应用
declare(ticks=1); register_tick_function('do_profile'); register_shutdown_function('show_profile'); ...
- IOS-UI-UIDynamic(二)
UIPushBehavior :推动效果 UIAttachmentBehavior:附着效果 UISnapBehavior:迅速移动效果 一.重要的属性 UIPushBehavior :推动效果 ty ...
- html-----017
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 转 Java中Filter、Servlet、Listener的学习
1.Filter的功能filter功能,它使用户可以改变一个 request和修改一个response. Filter 不是一个servlet,它不能产生一个response,它能够在一个requ ...