• 07-图6 旅游规划


    07-图6 旅游规划(25 分)

    其实这题感觉用二维数组方便一些,但为了锻炼和熟悉建表所以选择了链表实现。

    有了一张自驾旅游路线图,你会知道城市间的高速公路长度、以及该公路要收取的过路费。现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径。如果有若干条路径都是最短的,那么需要输出最便宜的一条路径。

    输入格式:

    输入说明:输入数据的第1行给出4个正整数N、M、S、D,其中N(2N500)是城市的个数,顺便假设城市的编号为0~(N1);M是高速公路的条数;S是出发地的城市编号;D是目的地的城市编号。随后的M行中,每行给出一条高速公路的信息,分别是:城市1、城市2、高速公路长度、收费额,中间用空格分开,数字均为整数且不超过500。输入保证解的存在。

    输出格式:

    在一行里输出路径的长度和收费总额,数字间以空格分隔,输出结尾不能有多余空格。

    输入样例:

    4 5 0 3
    0 1 1 20
    1 3 2 30
    0 3 4 10
    0 2 2 20
    2 3 1 20
    

    输出样例:

    3 40

      1 #include<iostream>
      2 #include<vector>
      3 using namespace std;
      4 #define MaxNv 500
      5 vector<int> cost(MaxNv,0);
      6 vector<int> dist(MaxNv,250001);
      7 vector<int> collected(MaxNv,0);
      8 struct node{
      9 int v1,v2;
     10 int len,cost;
     11 };
     12 using ptrtonode=node*;
     13 struct enode{
     14 int v;
     15 int length;
     16 int cost;
     17 enode* next;
     18 };
     19 using edge=enode*;
     20 using firstlist=edge[MaxNv];
     21 struct graph{
     22 int start,end;
     23 int Nv;
     24 int Ne;
     25 firstlist headlist;
     26 };
     27 using Graph=graph*; 
     28 Graph createGraph(){
     29 Graph gra=new graph();
     30 cin>>gra->Nv>>gra->Ne>>gra->start>>gra->end;
     31 for(int i=0;i<MaxNv;i++)
     32 gra->headlist[i]=NULL;
     33 return gra;
     34 }
     35 void Insertedge(Graph gra,ptrtonode pnode){
     36 edge e=new enode();
     37 e->v=pnode->v2; 
     38 e->length=pnode->len; 
     39 e->cost=pnode->cost;
     40 e->next=gra->headlist[pnode->v1];
     41 gra->headlist[pnode->v1]=e;
     42 e=new enode();
     43 e->v=pnode->v1;
     44 e->length=pnode->len;
     45 e->cost=pnode->cost;
     46 e->next=gra->headlist[pnode->v2];
     47 gra->headlist[pnode->v2]=e;
     48 }
     49 Graph BuildGraph(){
     50 int v1,v2;
     51 Graph gra=createGraph();
     52 ptrtonode pnode=new node();
     53 for(int i=0;i<gra->Ne;i++){
     54     cin>>pnode->v1>>pnode->v2>>pnode->len>>pnode->cost;
     55     Insertedge(gra,pnode);
     56 }
     57 return gra;
     58 } 
     59 int findmin(Graph gra){
     60 int min=3000000; int v=-1;
     61 for(int i=0;i<gra->Nv;i++){
     62 if(collected[i]==0&&dist[i]<min)
     63 {min=dist[i]; v=i;}
     64 }
     65 return v;
     66 }
     67 void Dijikstra(Graph gra,int s){
     68 while(1){
     69 int v=findmin(gra);
     70 if(v==-1) break;
     71 collected[v]=1;
     72 edge ptr=gra->headlist[v];
     73 while(ptr!=NULL&&collected[ptr->v]==0){
     74 if(dist[v]+ptr->length<dist[ptr->v]){
     75 dist[ptr->v]=dist[v]+ptr->length;
     76 cost[ptr->v]=cost[v]+ptr->cost;}
     77 else if(dist[v]+ptr->length==dist[ptr->v]&&cost[v]+ptr->cost<cost[ptr->v]){
     78 dist[ptr->v]=dist[v]+ptr->length;
     79 cost[ptr->v]=cost[v]+ptr->cost; 
     80 }
     81 ptr=ptr->next;
     82 }
     83 }
     84 }
     85 void solve(Graph gra){
     86       int s=gra->start;
     87       dist[s]=0;
     88       edge ptr=gra->headlist[s];
     89       while(ptr!=NULL){
     90       dist[ptr->v]=ptr->length;
     91       cost[ptr->v]=ptr->cost;
     92       ptr=ptr->next;
     93   }
     94   Dijikstra(gra,s);
     95   cout<<dist[gra->end]<<" "<<cost[gra->end];
     96 }
     97 int main(){
     98 Graph gra=BuildGraph();
     99 edge ptr=gra->headlist[0]; 
    100 solve(gra);
    101 return 0;
    102 }
    View Code

     

  • 相关阅读:
    [LeetCode] String to Integer (atoi) 解题报告
    [LeetCode] Spiral Matrix 解题报告
    推导基姆拉尔森公式根据日期计算星期
    gdb常用命令的用法
    利用基姆拉尔森公式根据日期计算星期
    RIM推出BlackBerry SDK 助力开发者多种应用程序开发
    ERP环境下物料清单的数据结构研究[转]
    VSTO EXCEL篇学习笔记五【原】
    高德纳传奇[转]
    PLM中BOM核心技术的研究[转]
  • 原文地址:https://www.cnblogs.com/A-Little-Nut/p/8056205.html
Copyright © 2020-2023  润新知