题目链接: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. Win10无法使用内置管理员用户打开edge解决方案

    https://jingyan.baidu.com/article/4f7d5712d23f1b1a2119274b.html

  2. python实现原地刷新方式输出-可用于百分比进度显示输出

    方式1: import sys sys.stdout.write('\r' + '你的输出详情') sys.stdout.flush() 方式2: print('\r' + '你的输出详情', end ...

  3. iptables filter表小案例

    案例1:把80端口,22端口,21端口放行 22端口指定IP访问,其它IP拒绝. shell脚本实现: [root@centos7 ~]# vim /usr/local/sbin/iptables.s ...

  4. XToDo未完成内容标记管理器

    下载地址:https://github.com/trawor/XToDo 跟VVDocumenter规范注释生成器的安装方式一样: 下载开源工程在Xcode重新编译运行会自动安装此插件,重启Xcode ...

  5. 【转】maven同时使用maven-surefire-report-plugin和maven-surefire-plugin默认将执行两次test

    https://issues.apache.org/jira/browse/SUREFIRE-753 Here the pom.xml snippet how i configured the rep ...

  6. Greenplum-cc-web监控软件安装

    一环境列表 操作系统 centos6.5  64 Greenplum版本: greenplum-db-4.3.5.3-build-2-RHEL5-x86_64.tar Greenplum集群环境搭建: ...

  7. 存储过程打印超过8000的VARCHAR字符的问题

    DECLARE @info NVARCHAR(MAX) --SET @info to something big PRINT CAST(@info AS NTEXT) 这样就可以输出超过8000的字符 ...

  8. Go 语言机制之逃逸分析

    https://blog.csdn.net/weixin_38975685/article/details/79788254   Go 语言机制之逃逸分析 https://blog.csdn.net/ ...

  9. flume使用之exec source收集各端数据汇总到另外一台服务器

    转载:http://blog.csdn.net/liuxiao723846/article/details/78133375 一.场景一描述: 线上api接口服务通过log4j往本地磁盘上打印日志,在 ...

  10. DB索引、索引覆盖、索引优化

    ###########索引########### @see   http://mp.weixin.qq.com/s/4W4iVOZHdMglk0F_Ikao7A 聚集索引(clustered inde ...