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

  1. Thymeleaf3.0内容

    Thymeleaf简介 什么是Thymeleaf Thymeleaf是网站或者独立应用程序的新式的服务端java模板引擎,可以执行HTML,XML,JavaScript,CSS甚至纯文本模板. Thy ...

  2. 加州大学伯克利分校Stat2.2x Probability 概率初步学习笔记: Final

    Stat2.2x Probability(概率)课程由加州大学伯克利分校(University of California, Berkeley)于2014年在edX平台讲授. PDF笔记下载(Acad ...

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

  4. Machine and Deep Learning with Python

    Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstiti ...

  5. Amazon DynamoDB 概览

    1. 什么是Amazon DynamoDB DynamoDB 是一种快速.全面受管的 NoSQL 数据库服务,它能让用户以简单并且经济有效地方式存储和检索任何数据量,同时服务于任何程度的请求流量.所有 ...

  6. 初步认识Thymeleaf:简单表达式和标签。(一)

    本文只适用于不会Java对HTML语言有基础的程序员们,是浏览了各大博客后收集整理,重新编辑的一篇文章,希望能对大家有所帮助. 对于Thymeleaf,网上特别官方的解释无非就是:网站或者独立应用程序 ...

  7. English - Mosquitos

    Smith's house is full of mosquitos. Every night they bite him. He can not sleep because the mosquito ...

  8. SpringBoot入门:新一代Java模板引擎Thymeleaf(理论)

    Spring Boot 提供了spring-boot-starter-web来为Web开发予以支持,spring-boot-starter-web为我们提供了嵌入的Tomcat以及SpringMVC的 ...

  9. 初步认识thymeleaf:简单表达式和标签(一)

    初步认识Thymeleaf:简单表达式和标签.(一)   本文只适用于不会Java对HTML语言有基础的程序员们,是浏览了各大博客后收集整理,重新编辑的一篇文章,希望能对大家有所帮助.最后本文如果有哪 ...

  10. Thymeleaf 3.0 专题

    http://www.thymeleaf.org/doc/articles/layouts.html thymeleaf的初次使用(带参请求以及调用带参js方法) 之前对于前端框架接触较少,第一次接触 ...

随机推荐

  1. JavaScript高级程序设计(八):基本概念--操作符

    操作符包括:算术操作符.位操作符.关系操作符和相等操作符. 一元操作符 1.只能操作一个值得操作符,即递增和递减操作符: 2.递增(++)和递减(--)操作符包括:前置型和后置型.前置型位于要操作的变 ...

  2. Warning: Attempt to present * on * which is already presenting *

    Warning: Attempt to present (要被presented的控制器)  on (哪个控制器来presenting) which is already presenting (已经 ...

  3. C#多线程同步

    在编写多线程程序时无可避免会碰到线程的同步问题.什么是线程的同步呢? 举个例子:假如在一个公司里面有一个变量记录某人T的工资count=100,有两个主管A和B(即工作线程)在早一些时候拿了这个变量的 ...

  4. 06链队列_LinkQueue--(栈与队列)

    #include "stdio.h" #include "stdlib.h" #include "io.h" #include " ...

  5. 05_XML的解析_02_dom4j 解析将信息封装到对象中

    [person.xml]要解析的内容 <?xml version="1.0" encoding="UTF-8"?> <students> ...

  6. 九度OJ 1442 A sequence of numbers

    题目地址:http://ac.jobdu.com/problem.php?pid=1442 题目描述: Xinlv wrote some sequences on the paper a long t ...

  7. OpenJudge 2738 浮点数加法

    1.链接地址: http://bailian.openjudge.cn/practice/2738 2.题目: 总时间限制: 1000ms 内存限制: 65536kB 描述 求2个浮点数相加的和 题目 ...

  8. 关于php的认识和介绍

    php的介绍: 什么是php? <1>:php是一个编程语言 <2>:php是处理php编程语言的一个软件.php语言必须运行在php软件上. 为什么要学习php? php可以 ...

  9. Entity Framework 插入数据 解决主键非自增问题

    http://blog.csdn.net/educast/article/details/8632806 与Entity Framework相伴的日子痛并快乐着.今天和大家分享一下一个快乐,两个痛苦. ...

  10. bootstrap table 行号 显示行号 添加行号 bootstrap-table 行号

    思想:借助bootstrap-table 本身的index属性, 巧妙的的通过formatter 实现 { field: 'Number', title: 'Number', formatter: f ...