题目链接: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. Mybatis中#和$区别(带脑图)

    零.引言 使用 #{name} 的时候,MyBatis会进行预编译,防止SQL注入的问题(官方话) 用一个通俗一点的例子来解释,比如有如下MyBatis的SQL语句 21.#{}和${}的区别.png ...

  2. RabbitMQ学习笔记(一):安装及Springboot集成

    前言 MQ,即消息队列Message Queue的缩写. RabbitMQ 是MQ的一种,就像招商银行是银行的一种一样.主要是用来实现应用程序的异步和解耦,同时也能起到消息缓冲,消息分发的作用. 消息 ...

  3. Tomcat域名绑定

    域名绑定与虚拟目录设置: conf/server.xml 的修改方式如下: 单个域名绑定: 原始: <Engine name="Catalina" defaultHost=& ...

  4. 多个table 相同col 的 设置相同width

    不同table 中的col 虽然设置了width 相同,但在IE8.9中长度仍不统一.经检查是不同table使用了不同的colspan ,删除colspan全部使用<td></td& ...

  5. GCT之数学公式(三角函数)

  6. 17 HTTP编程入门

    http请求原理 http请求原理我就不多说了,网上一搜就能搜索到,下面我注意是记录下http模块的使用方法 http 模块 HTTP-server hello world 我们使用HandleFun ...

  7. WebGL常用数学公式

    1.三角函数 坐标轴采用右手法则,沿Z轴的逆时针方向为正角度,假设原始点为p(x,y,z),a是X轴旋转到点p的角度,r是从原始点到p点的距离.用这两个变量计算出点p的坐标,等式如下: x = rco ...

  8. Python打包-py2exe

    上篇文章讲了pyinstaller,可以打包成包含Windows, Linux, Mac OS X, FreeBSD, Solaris and AIX等操作系统下的可执行文件,如果只针对Windows ...

  9. jquery.form 和MVC4做无刷新上传DEMO

    jquery.form 和MVC4做无刷新上传DEMO HTML: <script src="~/Scripts/jquery-1.10.2.min.js"></ ...

  10. Go中error类型的nil值和nil

    https://my.oschina.net/chai2010/blog/117923