题目1008:最短路径问题(最短路径问题dijkstra算法)

题目链接: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(0==n && 0==m) break;
        for(int i = 1 ; i <= n ; i++){
            edge[i].clear();//clear the matrix
        }
        for(int i = 1 ; 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 = 1 ; i <= n ; i++){//init
            dis[i] = -1;
            mark[i] = false;
            cost[i] = 0;
        }
         
        dis[s]=0;
        mark[s] = true;//whether this node is in the aggregate
        int newP = s;//new dot needed to add
        for(int i = 1 ; i < n ; i++){
            for(int j = 0 ; 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]==-1 || 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 = 2121212121;
            for(int j = 1 ; j <= n ; j++){//find a new node to add
                if(mark[j]==true) continue;//not in the aggregate
                if(dis[j] == -1) 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 0;
}
/**************************************************************
    Problem: 1008
    User: zpfbuaa
    Language: C++
    Result: Accepted
    Time:10 ms
    Memory:1552 kb
****************************************************************/

 

转载于:https://www.cnblogs.com/zpfbuaa/p/6736356.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值