题目链接:http://ac.jobdu.com/problem.php?pid=1008

详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus

参考代码:

//
// 1008 最短路径问题.cpp
// Jobdu
//
// Created by PengFei_Zheng on 19/04/2017.
// Copyright © 2017 PengFei_Zheng. All rights reserved.
// #include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cmath>
#include <vector> #define MAX_SIZE 1010 using namespace std; int n, m; struct E{//the adjective matrix
int next;
int len;
int cost;
}; vector<E> edge[MAX_SIZE]; int dis[MAX_SIZE];//dis[i] keep the length from start to [i] so and cost[i],mark[i]
int cost[MAX_SIZE];
bool mark[MAX_SIZE]; int main(){
while(scanf("%d%d",&n,&m)!=EOF){
if(==n && ==m) break;
for(int i = ; i <= n ; i++){
edge[i].clear();//clear the matrix
}
for(int i = ; i <= m ; i++){
int a, b, len, cost;
scanf("%d%d%d%d",&a,&b,&len,&cost);
E tmp;
tmp.len = len;
tmp.cost = cost;
tmp.next = b;
edge[a].push_back(tmp);//the node a has those info and the next node is b
tmp.next = a;
edge[b].push_back(tmp);//the node b has those info and the next node is a
} int s, t;
scanf("%d%d",&s,&t);//start node and end node for(int i = ; i <= n ; i++){//init
dis[i] = -;
mark[i] = false;
cost[i] = ;
} dis[s]=;
mark[s] = true;//whether this node is in the aggregate
int newP = s;//new dot needed to add
for(int i = ; i < n ; i++){
for(int j = ; j <edge[newP].size(); j++){//all the nodes in the newP
//get info
int next = edge[newP][j].next;
int len = edge[newP][j].len;
int cos = edge[newP][j].cost; if(mark[next] == true) continue;//already in the aggreagte
//can't reach or has a less length or has smaller cost
if(dis[next]==- || dis[next] > dis[newP]+len || (dis[next]==dis[newP]+len && cost[next] > cost[newP]+cos)){
dis[next] = dis[newP] + len;
cost[next] = cost[newP] + cos;
}
}
int min = ;
for(int j = ; j <= n ; j++){//find a new node to add
if(mark[j]==true) continue;//not in the aggregate
if(dis[j] == -) continue;//can't reach
if(dis[j]<min){
min = dis[j];
newP = j;
}
}
mark[newP] = true;
}
printf("%d %d\n",dis[t],cost[t]);
}
return ;
}
/**************************************************************
Problem: 1008
User: zpfbuaa
Language: C++
Result: Accepted
Time:10 ms
Memory:1552 kb
****************************************************************/

题目1008:最短路径问题(最短路径问题dijkstra算法)的更多相关文章

  1. 数据结构(C#):图的最短路径问题、(Dijkstra算法)

    今天曾洋老师教了有关于图的最短路径问题,现在对例子进行一个自己的理解和整理: 题目: 要求:变成计算出给出结点V1到结点V8的最短路径 答: 首先呢,我会先通过图先把从V1到V8的各种路径全部计算下来 ...

  2. 最短路径 - 迪杰斯特拉(Dijkstra)算法

    对于网图来说,最短路径,是指两顶点之间经过的边上权值之和最少的路径,并且我们称路径上的第一个顶点为源点,最后一个顶点为终点.最短路径的算法主要有迪杰斯特拉(Dijkstra)算法和弗洛伊德(Floyd ...

  3. 图的最短路径---迪杰斯特拉(Dijkstra)算法浅析

    什么是最短路径 在网图和非网图中,最短路径的含义是不一样的.对于非网图没有边上的权值,所谓的最短路径,其实就是指两顶点之间经过的边数最少的路径. 对于网图,最短路径就是指两顶点之间经过的边上权值之和最 ...

  4. 最短路径-迪杰斯特拉(dijkstra)算法及优化详解

    简介: dijkstra算法解决图论中源点到任意一点的最短路径. 算法思想: 算法特点: dijkstra算法解决赋权有向图或者无向图的单源最短路径问题,算法最终得到一个最短路径树.该算法常用于路由算 ...

  5. 最短路径问题 HDU - 3790 (Dijkstra算法 + 双重权值)

    参考:https://www.cnblogs.com/qiufeihai/archive/2012/03/15/2398455.html 最短路径问题 Time Limit: 2000/1000 MS ...

  6. Dijkstra算法求单源最短路径

    Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店 ...

  7. 单源最短路径(1):Dijkstra 算法

    一:背景 Dijkstra 算法(中文名:迪杰斯特拉算法)是由荷兰计算机科学家 Edsger Wybe Dijkstra 提出.该算法常用于路由算法或者作为其他图算法的一个子模块.举例来说,如果图中的 ...

  8. 非负权值有向图上的单源最短路径算法之Dijkstra算法

    问题的提法是:给定一个没有负权值的有向图和其中一个点src作为源点(source),求从点src到其余个点的最短路径及路径长度.求解该问题的算法一般为Dijkstra算法. 假设图顶点个数为n,则针对 ...

  9. pta7-7旅游规划(dijkstra算法)

    题目链接:https://pintia.cn/problem-sets/1101307589335527424/problems/1101314114762387456 题意:给n给城市,m条公路,公 ...

  10. Dijkstra算法模拟讲解

    dijkstra算法,是一个求单源最短路径算法 其算法的特点为: 层层逼进,有点类似宽度搜索的感觉 其需要的数据结构为:                  int map[N][N] 所有点之间的权表 ...

随机推荐

  1. Php反转字符串函数

    From: http://blog.csdn.net/happy664618843/article/details/5861138

  2. Easyradius对接WayOs维盟小区版XQ教程

    维盟极众多人的意见,然后又推出来一个新版本,唉~~~以前也是只有PC,有人给大量建议后,就再出个ISP,直接价格翻倍,再后来大家要桥接的功能又推出一个S9,这日子啥时候是个头啊. 小区版最主要的亮点就 ...

  3. Opengl的gl_NormalMatrix

    原文地址:http://blog.csdn.net/ichild1964/article/details/9728357 参考:http://www.gamedev.net/topic/598985- ...

  4. 启动zookeeper时出现的问题

    zkEnv.cmd @echo off REM Licensed to the Apache Software Foundation (ASF) under one or more REM contr ...

  5. Android 获取内存信息

    由于工作需要,研究了一下android上获取内存信息的方法,总结如下: 1.SDK获取 在Java层利用API获取很简单,直接使用ActivityManager.MemoryInfo类即可,代码如下: ...

  6. JUC回顾之-ArrayBlockingQueue底层实现和原理

    ArrayBlockingQueue的原理和底层实现的数据结构 : ArrayBlockingQueue是数组实现的线程安全的有界的阻塞队列,可以按照 FIFO(先进先出)原则对元素进行排序. 线程安 ...

  7. Android学习笔记——保存文件(Saving Files)

              本人邮箱:JohnTsai.Work@gmail.com,欢迎交流讨论.                 欢迎转载,转载请注明网址:http://www.cnblogs.com/J ...

  8. react-router实现tab页面切换,并解决选中样式首页始终选中问题

    import React, {Component} from 'react'; import { BrowserRouter as Router, Route, NavLink } from &quo ...

  9. perforce 学习资源

    这两天,要将depot切换到stream 使用,收集了很多perforce的学习资源,在这里记录一下: perforce 官网:  www.perforce.com perforce参考手册:  ht ...

  10. springboot+swagger2案例

    1.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...