题目链接: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. Yii2 中cookie的用法(1)

    Yii使用 yii\web\Cookie对象来代表每个cookie,yii\web\Request 和 yii\web\Response 通过名为’cookies’的属性维护一个cookie集合, 前 ...

  2. iOS:ODRefreshControl

    https://github.com/Sephiroth87/ODRefreshControl Important note if your project doesn’t use ARC: you ...

  3. android代码规范和studio配置CodeStyle

    studio配置CodeStyle可以很好的帮助我们检测代码规范性,保持大家的代码统一,来看看怎么配置和使用吧 代码规范,自己公司的一套 代码规范 一.      简介 A.    目的 本文提供一整 ...

  4. 如何通过SSH及其Client 批量分发文件和执行管理命令

    一.前提:已经配置好root和hadoop用户的无密码的SSH访问 二.直接上代码 ##复制单个文件[hadoop@nn1 hadoop]$ for ip in 102 103 104 111 112 ...

  5. Objective-C 语法之 Debug 表达式

    main.m #import <Foundation/Foundation.h> #import "TestClass.h" int main(int argc, co ...

  6. redis 的hash数据类型

    hash的常用命令 1.hset hset key field value 将哈希表key中的域field的值设为value 如果key不存在,一个新的哈希表被创建并进行HSET操作 如果field是 ...

  7. 使用Eclipse构建Maven项目

    http://blog.csdn.net/qjyong/article/details/9098213

  8. Get started with ros -- 1

    原创博文:转载请标明出处(周学伟):http://www.cnblogs.com/zxouxuewei/tag/ 一.Introduction: 机器人操作系统(ROS)是使机器人系统的不同部分能够发 ...

  9. Eclipse------使用Debug As时报错java.lang.IllegalStateException: Failed to read Class-Path attribute from manifest of jar file:/XXX

    报错信息: java.lang.IllegalStateException: Failed to read Class-Path attribute from manifest of jar file ...

  10. Java实现循环体的过滤器

    编写程序,利用continue语句实现循环体过滤器,过滤“老鹰”字符串,并做相应的处理,但是放弃continue语句之后的所有代码.即若遇到“老鹰”字符串则进行特定处理,然后使用continue语句跳 ...