代码改变世界

[九度OJ]1008.最短路径问题

2013-09-04 19:44  庸男勿扰  阅读(346)  评论(0编辑  收藏  举报

原题链接:http://ac.jobdu.com/problem.php?pid=1008

题目描述:
给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。
输入:
输入n,m,点的编号是1~n,然后是m行,每行4个数 a,b,d,p,表示a和b之间有一条边,且其长度为d,花费为p。最后一行是两个数 s,t;起点s,终点t。n和m为0时输入结束。
(1<n<=1000, 0<m<100000, s != t)
输出:
输出 一行有两个数, 最短距离及其花费。
样例输入:
3 2
1 2 5 6
2 3 4 5
1 3
0 0
样例输出:
9 11
来源:2010年浙江大学计算机及软件工程研究生机试真题
题解:
  这是一道简单的最短路径问题,唯一多出的是在最短路径基础上还有最小代价的限制。采用邻接表+迪杰斯特拉算法实现。
  1 #include <cstdio>
  2 #include <cstring>
  3 #include <iostream>
  4 #include <limits.h>
  5 #include <stdlib.h>
  6 using namespace std;
  7  
  8 #define MAX_VERTEX_NUM 1001
  9 #define MAX_ARC_NUM 200001
 10  
 11 /*******图的邻接表表示法**********/
 12 typedef struct ArcNode
 13 {
 14     int adjvex;    //该弧所指向顶点的位置
 15     struct ArcNode *nextarc;   //指向下一条弧的指针
 16     int dist;
 17     int cost;
 18 }ArcNode;
 19  
 20 typedef struct VNode
 21 {
 22     int dist,cost;//起始点到当前这个点的距离和花费
 23     ArcNode *firstarc; //指向第一条依附该顶点的弧的指针
 24 }VNode,AdjList[MAX_VERTEX_NUM];
 25  
 26 typedef struct
 27 {
 28     AdjList vertices;//顶点数组   
 29     int vexnum;
 30     int arcnum;   //当前图的顶点数和弧数
 31 }ALGraph;
 32  
 33 bool final[1001];
 34 int a,b,d,c;
 35 int s,t;
 36  
 37 int findNearest(ALGraph &G)
 38 {
 39         int i;
 40         int min = INT_MAX;
 41         int res = -1;
 42         int cnt=0;
 43         int arr[1001];
 44         for(i = 0; i<G.vexnum; i++)
 45               if(G.vertices[i].dist<min && !final[i])
 46               {
 47                     min = G.vertices[i].dist;
 48                     arr[cnt] = i;
 49                     cnt++;
 50               }
 51         min = INT_MAX;
 52         if(cnt>0)
 53         {
 54             for(i = 0; i<cnt; i++)
 55                     if(G.vertices[arr[i]].cost<min)
 56                     {   
 57                         min = G.vertices[arr[i]].cost;
 58                         res = arr[i];
 59                     }
 60         }
 61         return res;
 62 }
 63 void dij(ALGraph &G)
 64 {
 65     int i,w,j;
 66     int min;
 67     int near;
 68     ArcNode *p,*q;
 69     int v;
 70     for(i = 0; i<G.vexnum; i++)
 71         G.vertices[i].dist = G.vertices[i].cost = INT_MAX;
 72          
 73     G.vertices[s-1].dist = G.vertices[s-1].cost = 0;
 74      
 75     for (i=1; i<G.vexnum;i++) //更新n-1次
 76     {
 77         min = INT_MAX;      
 78         near = findNearest(G);
 79         if(near==-1)
 80              break;
 81         final[near] = true;
 82          
 83         for (p=G.vertices[near].firstarc; p!=NULL; p=p->nextarc)
 84         {    
 85              v = p->adjvex;
 86              if(G.vertices[near].dist+p->dist<G.vertices[v].dist && !final[v])//更新节点
 87              {
 88                   G.vertices[v].dist = G.vertices[near].dist+p->dist;
 89                   G.vertices[v].cost = G.vertices[near].cost+p->cost;
 90              }
 91              else if(G.vertices[near].dist+p->dist == G.vertices[v].dist && !final[v])//如果存在多条最短路径,更新最小花费节点
 92              {
 93                    if(G.vertices[near].cost+p->cost<G.vertices[v].cost)
 94                         G.vertices[v].cost = G.vertices[near].cost+p->cost;
 95              }
 96         }
 97     }//for           
 98 }
 99  
100 int main()
101 {
102     // freopen("dij.in","r",stdin);
103      //freopen("dij.out","w",stdout);
104      ALGraph G;
105      ArcNode *p,*q;
106      int vexnum,arcnum;
107      //G = (ALGraph*)malloc(sizeof(ALGraph));
108      while(scanf("%d%d",&vexnum,&arcnum)!=EOF && (vexnum!=0 || arcnum!=0) )
109      {
110          G.vexnum = vexnum;
111          G.arcnum = arcnum;
112          memset(final,0,sizeof(final));
113           for(int i=0; i<G.vexnum; i++)
114                   G.vertices[i].firstarc = NULL;
115           for(int i=0; i<G.arcnum; i++)
116           {
117                scanf("%d%d%d%d",&a,&b,&d,&c); 
118          
119                 if (G.vertices[a-1].firstarc==NULL)
120                 {
121                     G.vertices[a-1].firstarc = (ArcNode *)malloc(sizeof(ArcNode));
122          
123                     G.vertices[a-1].firstarc->adjvex = b-1;
124                     G.vertices[a-1].firstarc->dist = d;
125                     G.vertices[a-1].firstarc->cost = c;
126                     G.vertices[a-1].firstarc->nextarc = NULL;
127                 }
128                 else
129                 {
130                     for (p=G.vertices[a-1].firstarc; p->nextarc!=NULL; p=p->nextarc);
131          
132                     q = (ArcNode*)malloc(sizeof(ArcNode));
133                     q->adjvex = b-1;
134                     q->dist = d;
135                     q->cost = c;
136                     q->nextarc = NULL;
137          
138                     p->nextarc = q;
139                 }
140  
141           }
142           scanf("%d%d",&s,&t);  
143           dij(G);
144           printf("%d %d\n",G.vertices[t-1].dist,G.vertices[t-1].cost);                                         
145      }
146      return 0;  
147 }    
View Code