题目链接: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 页面渲染方法解析

    render渲染.renderPartial渲染部分.renderContent.renderAjax.renderFile ① render显示view和layout ② renderPartial ...

  2. 文字描边css

    -webkit-text-stroke: 3.3px #2A75BF; -webkit-text-fill-color:#fff; 该方法在安卓端貌似不支持,显示为一团 或: -webkit-text ...

  3. Android程序中有多个Activity时的可全部退出方法

    下面是代码.该方法的原理是用一个list记录每次启动的acitivity,在退出时候循环退出改list中保存的acitivity,这样就做到了所有的acitivity完美退出.   先写一个类继承Ap ...

  4. 代码记录——phase16,block36

    36*36 -2,-2扩展 代码有问题,有时能运行有时报错. HRESULT RotateZoom(PBYTE pbSrc,int iWidth,int iHeight,double dbRotate ...

  5. GetLastError结果列表

    程序执行出现错误,使用GetLastError可以初步得到错误的大致原因,方便程序员来分析,错误码对应含义总结如下,方便查看! MSDN System Error Codes: https://doc ...

  6. Tensorflow同时加载使用多个模型

    在Tensorflow中,所有操作对象都包装到相应的Session中的,所以想要使用不同的模型就需要将这些模型加载到不同的Session中并在使用的时候申明是哪个Session,从而避免由于Sessi ...

  7. 申请LINE 帐号的所有方法

    如果你打算用LINE 交朋友或是做行销,你就必须先要拥有一个甚至许多个LINE 帐号.在本篇文章中,将分享给你目前申请LINE 帐号的所有可能方法. 目前LINE 公司允许大家使用以下二种身分申请LI ...

  8. 用highcharts展现你的数据

    摘要: 前面已经分享过图表插件,今天在来将下如何使用highcharts来绘制图表.highcharts支持在线定制,你可以选择你所需要的模块,然后点击build就会生成一个js文件链接,右键保存到本 ...

  9. [转] NGUI自适应

    很多做移动终端开发的童鞋都可能遇到一个问题,就是如何自适应其实NGUI已经能帮我们实现,下面就告诉大家怎么整这个自适应.1,create a new ui 2,uiroot下添加uipanel(scr ...

  10. [SLAM] Little about SLAM

    Books from Zhihu: 幽默一把 看完Gonzalez:嗯,好像很好玩的样子,我也来搞一搞.看完Price:什么鬼,怎么这么多公式,公式看不懂肿么破.看完Szeliski:原来用一千页的书 ...